473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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
4 4253
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Maria Gaitani | last post by:
Hi! I am trying to programme some java native interface but I'm still in the process of research. I've seen examples such as this one...
37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
31
by: N.Davis | last post by:
I am very new to Python, but have done plenty of development in C++ and Java. One thing I find weird about python is the idea of a module. Why is this needed when there are already the ideas of...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
5
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary>...
20
by: jacob navia | last post by:
Consider this code static typedef struct { int boo; } FOO; This provokes with MSVC: ------------------------------ Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.