473,597 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4269
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.co m>
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:

"Inconsiste nt accessibility: field type 'WhiteBoard.Com mon.NoteValue[]'
is less accessible than field
'WhiteBoard.Com mon.NoteValuesC lass.NoteValueL ist'"
Nov 15 '05 #3
Jon Skeet <sk***@pobox.co m> wrote in message news:<MP******* *************** **@news.microso ft.com>...

[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.c om> 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.co m>
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
3458
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 http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/step1.html but I don't understand the third and fourth line of the code of the above example:
37
2590
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 much (except for people whose tools depend on '@' ;-)). I.e., (not having the source or time to delve) the apparent semantics of the above is something roughly like
12
3285
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
31
2505
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 class, file, and package? To my mind, although one CAN put many classes in a file, it is better to put one class per file, for readability and maintainability. One can then create packages and libraries, using groups of files, one
669
25771
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
18
1964
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 instance of it. So how can a class be threadsafe by itself but an instance of it not be? I guess I don't get what exactly being threadsafe means. Multiple theads can use the same instance of a class?
6
3872
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 examples I've seen are creating the information that will be displayed from scratch, while I have to use my previously created classes and add a GUI to it. I'm trying to do this GUI using JLabels but it won't let me refer to my CD class methods that...
5
3259
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> /// Create an object of the given type /// A default constructor is required to be successful. /// A failure will return null. /// </summary>
20
3237
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 Copyright (C) Microsoft Corporation. All rights reserved.
0
7883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8263
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6677
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5842
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2393
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1492
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.