By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.