use of struct

M

Mark Broadbent

Is it only really a good idea to use a struct when all the struct data
contains value types? I was writing one which used and exposed a public
string array property but there are a couple of things with this.

i. I am guessing the string array held within the struct would still be
stored in the heap anyway.
ii. I have noticed that the struct must be initialised using the
constructor to set the public values since if I simply declare and set
each property seperatly, the compiler complains that the struct is
uninitialised.

e.g.
Page p; //Page is a simple struct
string[] s = {"one", "two", "three"};
p.ID = 1; // compiler states p is unassigned
p.Attributes = s;

I cannot see the point to struct in this instance where public reference
types are used since the struct is less flexible. The following compiles..

string[] s = {"one", "two", "three"};
Page p = new Page(1, s);

p.ID = 1; // I can now set the properties without compiler problem
p.Attributes = s;

What do you all think?

Br,

Mark.
 
C

cody

if you have struct

S
{
public int P;
public void Foo(){}
}

you can do

S s;
s.P = 1234;
s.Foo();

but you cannot do

S s;
s.Foo();
s.P = 1234;

That is because P is not a property but a field and the compiler so can
recognize when all fields are initialized and access to a method/property of
a struct is only allowed if every field is guaranteed to be initialized.
i. I am guessing the string array held within the struct would still be
stored in the heap anyway.

Yes, it is. And storing a struct in an object would store the whole struct
in the heap.
 
M

Mark Broadbent

You would still have the same behaviour (in your code) if P was a
property (for the value type) -and that is fine (I am happy with that)
*but* if you tried to use a property to access a reference type the
compiler would not like that.

e.g.

//this is ok.

public struct S
{
int p;
public int P
{get {return p;}
set {p = value;}}
public void Foo(){}
}

S s;
s.P = 1234;
Foo();

//this throws compiler unassigned error;

public struct S
{
int p;
string s;
public int P
{get {return p;}
set {p = value;}}
public int S
{get {return s;}
set {s = value;}}
public void Foo(){}
}

S s;
s.P = 1234;
s.S = "Doesnt work"; //this code should compile since all values of
struct have been set
Foo();


p.s.
Value types are by default initialised to 0 in structs (if they haven't
been set thru constructor).
 
C

cody

You would still have the same behaviour (in your code) if P was a
property (for the value type) -and that is fine (I am happy with that)
*but* if you tried to use a property to access a reference type the
compiler would not like that.

e.g.

//this is ok.

public struct S
{
int p;
public int P
{get {return p;}
set {p = value;}}
public void Foo(){}
}

S s;
s.P = 1234;
Foo();

//this throws compiler unassigned error;

public struct S
{
int p;
string s;
public int P
{get {return p;}
set {p = value;}}
public int S
{get {return s;}
set {s = value;}}
public void Foo(){}
}

S s;
s.P = 1234;
s.S = "Doesnt work"; //this code should compile since all values of
struct have been set
Foo();


p.s.
Value types are by default initialised to 0 in structs (if they haven't
been set thru constructor).

This is very strange. What could be the reason of that?
 

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