How to "define" a struct

  • Thread starter Thread starter PLS
  • Start date Start date
P

PLS

I have a struct containing a few private members and a bunch of
properties. When I try to use the structure like this

ABC item;
item.property1 = 1;
item.property2 = 2;
...

In total, the properties set will set every private variable.

Yet the compiler gives me "Use of unassigned local variable" in the
first property set.

I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

Why do I get this error and what can I do about it?

Thanks,
++PLS
 
[...]
I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

That's a false conclusion. All types have a default constructor. But
local variables don't automatically use the default constructor. The
default constructor for your struct would only be automatic if the
struct appeared, for example, in a class instance.
Why do I get this error and what can I do about it?

You can initialize your variable:

ABC item = new ABC();

Pete
 
Doesn't "new" create on the GC heap? I thought structs were value types
and appeared on the stack. In this case, I WANT the struct on the stack
if possible.

++PLS

[...]
I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

That's a false conclusion. All types have a default constructor. But
local variables don't automatically use the default constructor. The
default constructor for your struct would only be automatic if the
struct appeared, for example, in a class instance.
Why do I get this error and what can I do about it?

You can initialize your variable:

ABC item = new ABC();

Pete
 
Doesn't "new" create on the GC heap?

No, though I share your concern. :) I've complained about this syntax
before, but no one seems to agree with me.
I thought structs were value types
and appeared on the stack. In this case, I WANT the struct on the stack
if possible.

Your struct is a value type, and like all value types it will be on the
stack when declared as a local variable.

I'm not really sure what exactly happens when you use "new" with a
value type. For sure, it runs the specified constructor; what I don't
know is whether a temporary instance is created and then copied to the
declared instance being initialized, or if the compiler is smart enough
to just initialize the declared instance.

Either way, you wind up with just your declared instance, stored
wherever is appropriate according to the declaration. In this case,
that's going to be on the stack.

Yes, it's kind of confusing. :)

Pete
 
No. Maybe value types have.

You're right...I didn't express myself very well there. My point is
that having a default constructor is not a sufficient condition for a
local variable being considered to be "definitely assigned" (which is
what the OP) is talking about, as even non-struct value types (int,
bool, etc.) have default constructors but aren't automatically
"definitely assigned" as a result.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top