What is the construct for creating a static array of classes?

B

Bill

I would like to create a static array of classes (or structs) to be used in
populating name/value pairs in various WebForm drop down list boxes, but am
not quite sure of the construct (or rather to use structs instead of classes
in the array insofar as structs vs. classes appears to be controversial in
C# -- with some recommending avoiding structs altogether).

It needs to be an array something like this:

struct NoteValue
{
public byte ID;
public string Name;
}

public class NoteValues
{
public static readonly NoteValue[] NoteValuesList
{
{10, "Company"}, {15, "Location"}, {20, "Customer"};
}
}

This obviously does not compile, but should give an idea of what I'm TRYING
to accomplish.

Does anyone know the proper syntax to make this happen?
 
J

Jon Skeet

Bill said:
I would like to create a static array of classes (or structs) to be used in
populating name/value pairs in various WebForm drop down list boxes, but am
not quite sure of the construct (or rather to use structs instead of classes
in the array insofar as structs vs. classes appears to be controversial in
C# -- with some recommending avoiding structs altogether).

It needs to be an array something like this:

struct NoteValue
{
public byte ID;
public string Name;
}

public class NoteValues
{
public static readonly NoteValue[] NoteValuesList
{
{10, "Company"}, {15, "Location"}, {20, "Customer"};
}
}

This obviously does not compile, but should give an idea of what I'm TRYING
to accomplish.

Does anyone know the proper syntax to make this happen?

Firstly, you need to provide a constructor in NoteValue like:

public NoteValue (byte ID, string Name)
{
this.ID=ID;
this.Name=Name;
}

then:

public static readonly NoteValue[] NoteValuesList =
{
new NoteValue (10, "Company"),
new NoteValue (15, "Location"),
new NoteValue (20, "Customer")
};
 
B

Bill

Oops, sorry about the previous post. The actual error message is:

"Inconsistent accessibility: field type 'WhiteBoard.Common.NoteValue[]'
is less accessible than field
'WhiteBoard.Common.NoteValuesClass.NoteValueList'"
 
L

Lucean Morningside

[Snip]
Firstly, you need to provide a constructor in NoteValue like:

public NoteValue (byte ID, string Name)
{
this.ID=ID;
this.Name=Name;
}

then:

public static readonly NoteValue[] NoteValuesList =
{
new NoteValue (10, "Company"),
new NoteValue (15, "Location"),
new NoteValue (20, "Customer")
};

You could consider using a static constructor, which would looklike the
following:

public class NoteValues
{
public static readonly NoteValue[] NoteValuesList;

static NoteValues()
{
// Code to intialize NoteValuesList;
}
}

Another comment on the overall design that you are suggesting: it might be
better to turn NoteValues into a singleton object, and add a read-only indexer
to it that would expose a collection of NoteValue objects.
 
J

Jon Skeet

Lucean Morningside said:
You could consider using a static constructor, which would looklike the
following:

public class NoteValues
{
public static readonly NoteValue[] NoteValuesList;

static NoteValues()
{
// Code to intialize NoteValuesList;
}
}

What would be the advantage of that over the code that I posted? In
fact, it would potentially harm efficiency by preventing the compiler
from marking the NoteValues type as beforefieldinit.
Another comment on the overall design that you are suggesting: it might be
better to turn NoteValues into a singleton object, and add a read-only indexer
to it that would expose a collection of NoteValue objects.

That would indeed be better, as then the contents of the array couldn't
be changed (which they could with the above, even though the array
reference itself is readonly).
 

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