471,348 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Struct Help

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?

Dec 4 '05 #1
9 1430
variables in C# when declared must be initialized.

Blah test;

should be Blah test= new Blah();

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"^MisterJingo^" wrote:
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?

Dec 4 '05 #2
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.
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:E3**********************************@microsof t.com...
variables in C# when declared must be initialized.

Blah test;

should be Blah test= new Blah();

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"^MisterJingo^" wrote:
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?

Dec 4 '05 #3
"J. Verdrengh" <un***@tiscali.be> a écrit dans le message de news:
43***********************@news.skynet.be...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 4 '05 #4
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

Dec 4 '05 #5
When I try that I get the message:

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

Dec 4 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Dec 4 '05 #7
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)

Dec 4 '05 #8
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 4 '05 #9
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;
test.Y = 1;

}
When I try that I get the message:

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

Dec 4 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Angelo Secchi | last post: by
5 posts views Thread by Roy Hills | last post: by
4 posts views Thread by Angus Comber | last post: by
20 posts views Thread by fix | last post: by
2 posts views Thread by Arne Styve | last post: by
28 posts views Thread by Tamir Khason | last post: by
4 posts views Thread by JR | last post: by
9 posts views Thread by sean.scanlon | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.