Struct Help

M

^MisterJingo^

I'm new to C# (literally 2 days) and need a bit of help understanding
an error.

struct Blah
{
int x;
int y;

public int X
{
get
{
return x;
}
set
{
this.x = value;
}
}

public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
}



class MainApp
{
public static void Main()
{
Blah test;

test.Y = 1;

}
}

With the above code I get the following error:

"Use of unassigned local variable 'test'"

But I remove the data member properties in 'struct Blah' I can happily
assign to test.y.

If I use properties (get, set)do I need to initialise the struct
members first?
 
G

Guest

variables in C# when declared must be initialized.

Blah test;

should be Blah test= new Blah();
 
J

J. Verdrengh

Quote from microsoft:

When you create a struct object using the new operator, it gets created and
the appropriate constructor is called. Unlike classes, structs can be
instantiated without using the new operator. If you do not use new, the
fields will remain unassigned and the object cannot be used until all of the
fields are initialized.
 
J

Joanna Carter [TeamB]

"J. Verdrengh" <[email protected]> a écrit dans le message de [email protected]...

| When you create a struct object using the new operator, it gets created
and
| the appropriate constructor is called. Unlike classes, structs can be
| instantiated without using the new operator. If you do not use new, the
| fields will remain unassigned and the object cannot be used until all of
the
| fields are initialized.

Which translated from MSspeak means do this :

struct Blah
{
int x = 0;
int y = 0;

...
}

Joanna
 
M

^MisterJingo^

The book I was reading said structs can be declared without the "new"
operator or empty constructor call - which is where the confusion arose
from. Should this be avoided?

Thanks,

Chris
 
M

^MisterJingo^

When I try that I get the message:

"'Blah.x': cannot have instance field initializers in structs"
 
J

Jeff Louie

MJ....

As a first approximation, you can look at a structure as a "lightweight
class
with value semantics". However, unlike a class, you cannot extend a
structure,
define a destructor or initialize fields at declaration in a structure.
You cannot
define a no-arg constructor in a structure since the compiler will
insist on
doing this for you. If you do define a constructor, it is your
responsibility to
initialize all fields in the structure.

As an example, the idiom of returning a bool value and setting a
lastError
message in an instance field is not very object oriented and is not
thread
safe. Here is a prototype immutable structure aptly named BoolStruct
that is
object based and potentially thread safe.

// wraps bool result, message and source as value type
// two helper properties for bool result: B and Value
// immutable structure
public struct BoolStruct
{
public readonly bool isSuccess;
public readonly String message;
public readonly String source;
public BoolStruct(bool isSuccess,String message, String source)
{
if (message == null) {message= "";}
if (source == null) {source= "";}
this.isSuccess= isSuccess;
this.message= message;
this.source= source;
}
public bool B // helper
{
get {return isSuccess;}
}
public bool Value
{
get {return isSuccess;}
}
}

http://www.geocities.com/Jeff_Louie/OOP/oop11.htm

Regards,
Jeff
 
I

incurable1

The reason is that a 'struct' is just a value type. What you're trying
to do is the same as

int y;
int x;

x = y;

you need to assign something to 'y' before you can use it. (or in the
case of a struct you can also use new)
 
J

J. Verdrengh

Indeed, what Joanna proposes is not allowed. Either you create a structure
with the new operator, or you declare the structure as you did, but in the
latter case you first have to manually assign a value to each instance
field.

So i guess this won't give an error (not tested):
public static void Main()
{
Blah test;
test.x = 1;
test.y = 1;
 
M

Mattias Sjögren

The book I was reading said structs can be declared without the "new"
operator or empty constructor call - which is where the confusion arose
from.

You can in some cases, but then you have to manually initialize each
struct field. And to do that, they have to be accessible to you. In
this case both x and y are private so you can't access them directly,
therefore you have to use the new operator and let a constructor
handle the initializeation (all the default ctor does really is
initialize the struct's fields to their default values).


Mattias
 

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

Top