473,396 Members | 1,804 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,396 software developers and data experts.

initializer for array of struct

I have an array of 'struct' with three string fields which I would like to
initialize.
Is this possible?

public struct Role
{
string a; string b; string c;
}
...
Role[] roles = ?? ( {"1","2","3"}, {"4","5","6"} };
Nov 17 '05 #1
3 1859
ChrisA <Ch****@discussions.microsoft.com> wrote:
I have an array of 'struct' with three string fields which I would like to
initialize.
Is this possible?

public struct Role
{
string a; string b; string c;
}
...
Role[] roles = ?? ( {"1","2","3"}, {"4","5","6"} };


Not quite, no. You'd need:

public struct Role
{
string a, b, c;

public Role (string a, string b, string c)
{
this.a = a;
this.b = b;
this.c = c;
}
}

then

Role[] roles = new Role[] { new Role ("1", "2", "3"),
new Role ("4", "5", "6") };

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks. I don't know why I didn't try that. I'm used to the fairly flexible
initialization syntax from C/C++. I think I was expecting that there would
be a way to initialize each without allocating off the heap.
Nov 17 '05 #3
ChrisA <Ch****@discussions.microsoft.com> wrote:
Thanks. I don't know why I didn't try that. I'm used to the fairly flexible
initialization syntax from C/C++. I think I was expecting that there would
be a way to initialize each without allocating off the heap.


Well, if it weren't in an array, it still wouldn't be allocating on the
heap. "new" doesn't imply "heap" in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

7
by: B. Wood | last post by:
I have a small program (see below) that demonstrates an error I am getting when I build the program on a linux platform. I first initialize an unsigned char array (lines 16). I then initialize...
6
by: Clint Olsen | last post by:
What about the following is not computable? It seems that the size of foo is easily computable: typedef struct { char *name; char *data; } Foo; int main(void) {
12
by: Serve Laurijssen | last post by:
Is code like the following allowed? I am talking about the comma after the last function in the initializer. void f(void) {puts("f");} void g(void) {puts("g");} struct Funcs { void...
3
by: Old Wolf | last post by:
For the following code: struct S { char t; int flags; }; void func(void) {
2
by: aarklon | last post by:
Hi all, Recently i was reading a c book which contained a section called C pitfalls. it had a paragraph on the following lines:- ...
5
by: fred | last post by:
Hi, Can someone explain me why gcc-4.0 gives me the 'Initializer element is not constant' error with this code ? Everything seems to be constant here... #include <stdio.h> typedef struct {...
12
by: Pawel | last post by:
Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry {
4
by: Roman Mashak | last post by:
Hello, say I've a code: .... struct device_defs { unsigned int vendor; unsigned int dev; unsigned int mask; unsigned long data; };
0
by: Laurent Deniau | last post by:
I was wondering if there is any good reason (semantic) to assume that the following three code do not have the same efficiency (both space and time): struct A { int a,b,c; }; // could other...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.