472,142 Members | 1,086 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 2415

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Kieran Simkin | last post: by
6 posts views Thread by Eric Smith | last post: by
10 posts views Thread by Adam Warner | last post: by
7 posts views Thread by Sam | last post: by
20 posts views Thread by Cyn | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
reply views Thread by leo001 | last post: by

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.