array in struct

G

Guest

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.
 
J

Jon Skeet [C# MVP]

c8prog said:
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 = 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
 
G

Guest

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.key[j] = j;//error
Console.WriteLine("{0}",t.key[j]);
}
}
}
}
 
J

Jon Skeet [C# MVP]

c8prog said:
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.key[j] = j;//error
Console.WriteLine("{0}",t.key[j]);
}
}
}
}


Yes, there would be an error there - because t.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.Key[j] = j;

Is there any particular reason you're using a struct, by the 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