new for struct

S

Sam Sungshik Kong

Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable 's'
//...
S s = new S();
s.I = 3; //OK


So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam
 
R

Richard Blewett [DevelopMentor]

You must explicitly initialize all fields of a struct before you invoke any methods. Properties are, under the covers, just methods (look at the IL). Now the problem is most structs will keep their state private (or at least some of it). You you cannot explicitly initialize the data for the struct other than by running a constructor - which is what new does for a struct. So, in reality, ifd you are creating custom value types (structs) you will pretty much always have to use new to select a constructor to run before you invoke any methods on them.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable 's'
//...
S s = new S();
s.I = 3; //OK


So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam
 
P

Peter Jausovec

Hi,

You can use new, because with struct the operator new just initializes the
field values to zero/null. So you can call new or manually initialize the
fields.
 
S

Sam Sungshik Kong

Thanks for the reply.

I think I understand what you said. However, I still don't understand why I
don't get an error in Point case.

I'm supposed to use it like the following.

Point p = new Point();
p.X = 3;

But if I use it like the following, it seems to be ok.

Point p;
p.X = 3;

I'm reading a book - "Programming Windows Application in C#" by Charles
Petzold.
He said that "new" is a necessary in the above case.
Otherwise, an error will occur.
But my test is against it.

Any idea?

Thanks.
Sam
 
R

Richard Blewett [DevelopMentor]

Can you post a short complete piece of code that shows this working, I've never seen this work before

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thanks for the reply.

I think I understand what you said. However, I still don't understand why I
don't get an error in Point case.

I'm supposed to use it like the following.

Point p = new Point();
p.X = 3;

But if I use it like the following, it seems to be ok.

Point p;
p.X = 3;

I'm reading a book - "Programming Windows Application in C#" by Charles
Petzold.
He said that "new" is a necessary in the above case.
Otherwise, an error will occur.
But my test is against it.

Any idea?

Thanks.
Sam
 
S

Sam Sungshik Kong

Thanks for keeping up with my post.

Here's an example.

In a form, I put a button.

partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Point p;
p.X = 3;
Text = p.X.ToString();
}
}

I used Visual C# 2005 Expression Edition Beta.
It worked fine.
Isn't it strange?

Thanks.
Sam
 
R

Richard Blewett [DevelopMentor]

This is what I mean but a short *complete* post and mentioning you were running on VS2005 might have helped ;-). I thought it might be something that had changed in the new version but I just wrote this code (short and complete).

class Program
{
static void Main(string[] args)
{
Point p;
p.X = 10;
}
}

struct Point
{
int x;
public int X
{
get { return x; }
set { x = value; }
}
}

This fails with a compiler error of unitialized variable on 2003 and 2005. Can you post a short and *complete* example which exhibits your behaviour?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thanks for keeping up with my post.

Here's an example.

In a form, I put a button.

partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Point p;
p.X = 3;
Text = p.X.ToString();
}
}

I used Visual C# 2005 Expression Edition Beta.
It worked fine.
Isn't it strange?

Thanks.
Sam
 
D

Daniel O'Connell [C# MVP]

Richard Blewett said:
This is what I mean but a short *complete* post and mentioning you were
running on VS2005 might have helped ;-). I thought it might be something
that had changed in the new version but I just wrote this code (short and
complete).

class Program
{
static void Main(string[] args)
{
Point p;
p.X = 10;
}
}

This came up some time ago in another post, the compiler doesn't seem to
mind if you use System.Drawing.Point as an uninitalized variable. I've yet
to fathom why, my best guess is that the compiler special cases some types,
which is rather unpleasent.

Has anyone tried any other variables? Is this unique to Point(which was the
struct mentioned in the last post as well) or do all framework structs
exhibit this behaviour now?

I think I should look into this.
 
S

Sam Sungshik Kong

Oh, I forgot to mention that the Point is System.Drawing.Point.

I tried the same code with VS.Net 2003 and it failed to compile.
I guess that it's an issue of version difference.

The struct that I create works the same way in both VS.Net.
Only system built-in struct makes a difference.

Thanks.

Sam
 
R

Richard Blewett [DevelopMentor]

Well System.Drawing.Size is the same

this sucks rocks - seeing as the value type is already zero'd via the

.locals init

in the IL. There is no reason not to keep the behavior consistent between system value types and user defined value types

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

This came up some time ago in another post, the compiler doesn't seem to
mind if you use System.Drawing.Point as an uninitalized variable. I've yet
to fathom why, my best guess is that the compiler special cases some types,
which is rather unpleasent.

Has anyone tried any other variables? Is this unique to Point(which was the
struct mentioned in the last post as well) or do all framework structs
exhibit this behaviour now?

I think I should look into this.
 
R

Ravichandran J.V.

You will be required to use the 'new' keyword when you wish to pass
values to the structure's overloaded constructor. (Structures cannot
have user-defined default constructors).

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
R

Richard Blewett

OK after talking about this in the comments of my blog I think I know why
this works for System.Drawing.Point but not for my custom struct.

If the struct is declared in another assembly then you seem to be able to
get away with not initializing each field before using a property. If the
struct is in the same assembly as the code using it then the compiler
generates a "Using unassigned local variable" error.

I'm not sure *why* this is the case yet but at least they are not special
casing the system assemblies.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
D

Daniel O'Connell [C# MVP]

Richard Blewett said:
OK after talking about this in the comments of my blog I think I know why
this works for System.Drawing.Point but not for my custom struct.

If the struct is declared in another assembly then you seem to be able to
get away with not initializing each field before using a property. If the
struct is in the same assembly as the code using it then the compiler
generates a "Using unassigned local variable" error.

I'm not sure *why* this is the case yet but at least they are not special
casing the system assemblies.

Yes that is something...I still suspect its a bug.

Perhaps this should be listed on the product feedback center?
 
R

Richard Blewett [DevelopMentor]

OK, I've just had this confirmed by the C# PM. This is a bug and its now been fixed.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

OK after talking about this in the comments of my blog I think I know why
this works for System.Drawing.Point but not for my custom struct.

If the struct is declared in another assembly then you seem to be able to
get away with not initializing each field before using a property. If the
struct is in the same assembly as the code using it then the compiler
generates a "Using unassigned local variable" error.

I'm not sure *why* this is the case yet but at least they are not special
casing the system assemblies.
 

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

Similar Threads

Structs and delegates 16
static field initialization 1
stupid issue with struct 1
PtrToStructure for pointer allocated in C++ 1
simple question about struct 2
Not a variable 11
use of struct 3
struct memory layout 4

Top