Initialising values in a C# struct

J

Jay

In C++ I can initialise a struct as follows:
struct fileInfo {char filename[STRLEN]; int gotFile; int ch; int ln;};
fileInfo configFile = {"", 0, 1, 1};

Is there a similar way to initialise a struct in C#? The compiler flags an error at the opening
curly brace of the second line of the following:
struct fileInfo {string filename; int gotFile; int ch; int ln;}
fileInfo configFile = {"", 0, 1, 1};

I add a constructor that takes four parameters, but that seems quite wordy. Is there a better way?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


| I add a constructor that takes four parameters, but that seems quite
wordy. Is there a better way?
|

No really, a constructor is not only the recommemted but the only way of
doing it.
It allows you todo things like

foreach( ... )
arraylist.Add( new YourStruct( param1, param2.... ) );
 
P

Peter Bradley

You might like to check out the discussion at:

http://www.thescripts.com/forum/thread224006.html

One contribution (from Andre) says:

If you go through the documentation and specs.. you'll see that it says
'structs that are not initilized cannot be accessed or used'. This
doesn't imply that the runtime would do any sort of tracking, in fact,
the compiler will plainly give you a message that there's an error and
won't compile (till you've initialized struct data before using it).
So.. it only checks for validity upon struct data being accessed. Have a
look at the following sample program:

public struct Point
{
public int x, y;

public Point(int p1, int p2)
{
x = p1;
y = p2;
}
}

class MyClass {
static void Main(string args[]) {
Point p;
Console.WriteLine("X is: {0}", p.x);

// Compiler will perform a check hede and will abort..

p.x;
Console.WriteLine("X is: {0}", p.x);

/*
if you comment out the preceding statement, the program will work and
compile. Notice I didn't initialize y and it still worked. This shows
that the compiler doesn't check for 'every' field that's not initialized
but only when a field is being accessed */

}
}

Peter
 
M

Michael Phillips, Jr.

fileInfo configFile = {"", 0, 1, 1};

The c# language does not permit structure initializers as in the line above.

You are stuck with the wordy but correct constructor that takes four
parameters.
 
J

Jay

Thanks Michael, Peter and Ignacio for you helpful comments. I will use a constructor.

Jay

Michael Phillips said:
fileInfo configFile = {"", 0, 1, 1};

The c# language does not permit structure initializers as in the line above.

You are stuck with the wordy but correct constructor that takes four
parameters.

Jay said:
In C++ I can initialise a struct as follows:
struct fileInfo {char filename[STRLEN]; int gotFile; int ch; int ln;};
fileInfo configFile = {"", 0, 1, 1};

Is there a similar way to initialise a struct in C#? The compiler flags an
error at the opening
curly brace of the second line of the following:
struct fileInfo {string filename; int gotFile; int ch; int ln;}
fileInfo configFile = {"", 0, 1, 1};

I add a constructor that takes four parameters, but that seems quite
wordy. Is there a better way?
 

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