467,919 Members | 1,142 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,919 developers. It's quick & easy.

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

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?
Nov 13 '05 #1
  • viewed: 4002
Share:
4 Replies
Bill <nf*@nospam.com> wrote:
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")
};

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2
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'"
Nov 15 '05 #3
Jon Skeet <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>...

[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.
Nov 15 '05 #4
Lucean Morningside <m@exquisitor.com> wrote:
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).

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Maria Gaitani | last post: by
37 posts views Thread by Bengt Richter | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
31 posts views Thread by N.Davis | last post: by
669 posts views Thread by Xah Lee | last post: by
5 posts views Thread by not_a_commie | last post: by
20 posts views Thread by jacob navia | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.