473,499 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array in struct

I want to save integer value in array(int key[3]) of struct array (Element[10).
But runtime error occured. (=> Source Code)
How I use array of struct?
I am using dotnet 1.1.

=> Source Code ::

using System;

struct Element
{
public int[] key;
public int link;
Element(int init)
{
init = 0;
key = new int[3];
link = init;
}
};

class Test
{
public static void Main(string[] args)
{
Element[] t = new Element[10];

t[1].key[0] = 1; //error
Console.WriteLine("{0}",t[1].key[0]);
}
}

Thank you.
Nov 25 '05 #1
3 2468

c8prog wrote:
I want to save integer value in array(int key[3]) of struct array (Element[10).
But runtime error occured. (=> Source Code)
How I use array of struct?
I am using dotnet 1.1.

=> Source Code ::

using System;

struct Element
{
public int[] key;
public int link;
Element(int init)
{
init = 0;
key = new int[3];
link = init;
I hope this isn't your real code - "init" is effectively unused.
}
};


The semi-colon here is unnecessary - and suggests you're thinking in
C++ :)

<snip test class>

The problem is that when you do
Element[] t = new Element[10];

that calls the parameterless constructor for your struct, which just
sets all members to their default values (null, 0, false, etc).

You need to do:
for (int i=0; i < t.Length; i++)
{
t[i] = new Element(0); // Or whatever
}

*Then* you'll be able to access the individual key elements.
One alternative is to make Key a property instead of a public field (a
good idea anyway, IMO) which can initialise itself automatically if
it's null...

Jon

Nov 25 '05 #2
Thank you.

It is easy to initialize a value in link of Element struct.
But it is difficult to to initialize a value in int[] key field of Element
struct.
I want to save a value in int[] key field of Element struct.

New Source Code.

using System;

struct Element
{
public int[] key;
public int link;
};

class Test
{
public static void Main(string[] args)
{
Element[] t = new Element[10];

for(int i=0;i<t.Length;i++)
{
for(int j=0;j<3;j++)
{
t[i].key[j] = j;//error
Console.WriteLine("{0}",t[i].key[j]);
}
}
}
}

Nov 25 '05 #3
c8prog <c8****@discussions.microsoft.com> wrote:
It is easy to initialize a value in link of Element struct.
But it is difficult to to initialize a value in int[] key field of Element
struct.
No, it's really not.
I want to save a value in int[] key field of Element struct.
And that's fine - but you'll need to initialize the array first.
New Source Code.

using System;

struct Element
{
public int[] key;
public int link;
};

class Test
{
public static void Main(string[] args)
{
Element[] t = new Element[10];

for(int i=0;i<t.Length;i++)
{
for(int j=0;j<3;j++)
{
t[i].key[j] = j;//error
Console.WriteLine("{0}",t[i].key[j]);
}
}
}
}


Yes, there would be an error there - because t[i].key is null at that
point. If, however, you change Element to:

struct Element
{
int[] key;
public int link;

public int[] Key
{
get
{
if (key==null)
{
key = new int[3];
}
return key;
}
}
}

Then you could use:
t[i].Key[j] = j;

Is there any particular reason you're using a struct, by the way?

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

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

Similar topics

1
6390
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
10
4087
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
6
4172
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
10
6662
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
5
6292
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
7
3153
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
3853
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
20
2292
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
5
3773
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
2922
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
7012
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
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
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,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.