Re: Structure as object property

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Your struct must be declared at the same accessibility level as the
property that returns it. Otherwise the compiler will complain that you
are exposing a (for example) public property whose return type is (for
example) internal, and so your clients could never use the property.

However, you _could_ make all of the constructors for the struct
internal, meaning that no client code could ever instantiate a (valid)
Address. (Note, however, that client code _could declare_ an Address,
which would create the Address with all fields null / zero. They just
couldn't initialize those fields to anything meaningful.) This works
better with classes, by the way, for which making all of the
constructors internal / protected / private effectively means that the
outside world can _use_ objects of that class but could never _create_
one.

However, aside from all of this I think that you want Address to be a
class, not a struct.

> I created 4 instances of an Address object, but seem
> inefficient to me since the Address itslef doesn't really have (or need)
> any methods...

That's not really a good reason to make something a struct. Make it a
struct if it is logically a value, like an int, a double, or a
DateTime, or if you're doing massive numbers of calculations with it
and allocating / garbage collecting transient instances of it would
create performance problems (e.g. Point, Rectangle). Four addresses
doesn't really fit into that category, IMHO.

> I was thinking a structure would be more efficient, but
> thought that the different types (reference v.s. value) might cause a
> problem when passing the object around...

Yes, it will cause problems, particularly if your Address object
exposes mutable properties (with "set" methods). It's not the end of
the world, but I think that the extra thinking you'll have to do in
order to use an Address struct successfully will more than outweigh any
minor efficiency improvements that might result. Just my opinion.

.



Relevant Pages

  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)
  • Re: IAR linker: too smart
    ... I am using the IAR toolchain for our Renesas M16C based project. ... The compiler is doing exactly the right thing - if you declare variables that are never used, then the compiler and/or linker is free to remove them - they have no effect on the running of your program. ... define a big struct which contains all variables and put that struct into E2PROM_DATA ...
    (comp.arch.embedded)
  • Re: Passing an array of structuresfrom a pointer?
    ... an array of struct. ... You cannot assign a pointer to struct to a long* ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)
  • Re: HELP on NET Platform!!!
    ... C++/CLI added stack syntax for ... enum and other types are reference types such as classe. ... So when i declare the first type i allocate memory in stack,instead ... said that struct can be value types instead class are reference.So my ...
    (microsoft.public.vc.mfc)