473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Initialising values in a C# struct

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?
Jan 22 '07 #1
4 1847
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
"Jay" <nospamwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
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?


Jan 22 '07 #2
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.... ) );

--
Ignacio Machin
machin AT laceupsolutions com
Jan 22 '07 #3
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" <nospamwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
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?


Jan 22 '07 #4
Jay
Thanks Michael, Peter and Ignacio for you helpful comments. I will use a constructor.

Jay

"Michael Phillips, Jr." <mp*********@nospam.jun0.c0mwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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" <nospamwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
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?



Jan 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Clacy | last post by:
Can this be done... or is there a better way to achieve the same objective? If an interface class contains only references (to yet more interface classes), then can those references be...
2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
7
by: Nidhi | last post by:
Hi all, I dont know whether this question has been raised before or not. Nevertheless, here is my question... I have a structure of around 255 elements of the same type, say int. I have an...
7
by: Mike P | last post by:
I have a class with a dozen+ properties, some of which will be set a value, and some not, depending on the constructor called. I also have a method which has only one overload and all of the...
1
by: Serj Kondryukov | last post by:
I am novice in C#. I did write on C++ before. In my current program i need to use big constant table. Each string is a some struct what contain following fields: MyEnum f1; MyEnum f2; bool f3;...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
4
by: Nick Keighley | last post by:
Hi, is this correct:- typedef struct { long frequency; } Structure; Structure my_struct = {0};
5
Natasha26
by: Natasha26 | last post by:
I was playing around with struct and realised that when i inherit from a base-class, i can no longer initialise the inherited struct the way i'm used to. struct Date{ int dd, mm, yy; } ...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.