Constants Array of "Structs"

E

ESPNSTI

Hi,

I'm looking for a way to associate multiple "lists" of constants to each
other.
For example, I'd like a way to look up a database column's name based on its
"number" in a constants list.

I'm new to C# and my background is in Delphi, so below is an example of how
I would declare such a constant array of records in Delphi.
______________________________________________________

Type
TDBField =
Record
Number : Integer;
Name : String;
Description : String;
End;

Const
gcaDBFields : Array [0..2] Of TDBField =
(
(Number: 0; Name: ''; Description : ''),
(Number: 1000; Name: 'Field1'; Description: 'Field 1 Description'),
(Number: 2000; Name: 'Field2'; Description: 'Field 2 Description')
);
______________________________________________________

How do I accomplish the same in C#?

Thanks,
Erik
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi,

Im affraid, that C# arrays can not be declared as const.
The only solution i see:

public readonly static TDBField[] gcaDBFields = new TDBField[] {
new TDBField(....)
, new TDBField(....)
, new TDBField(....) };

This sample needs parametrized contructor for TDBField structure.

Regards
Marcin
 
S

smartkid

public readonly static TDBField[] gcaDBFields
The gcaDBFields is not really readonly, the client code can still change the
element of that array.

To get a real constant array-like object, use ReadOnlyCollection instead of
an array.


Marcin Grzêbski said:
Hi,

Im affraid, that C# arrays can not be declared as const.
The only solution i see:

public readonly static TDBField[] gcaDBFields = new TDBField[] {
new TDBField(....)
, new TDBField(....)
, new TDBField(....) };

This sample needs parametrized contructor for TDBField structure.

Regards
Marcin
Hi,

I'm looking for a way to associate multiple "lists" of constants to each
other.
For example, I'd like a way to look up a database column's name based on its
"number" in a constants list.

I'm new to C# and my background is in Delphi, so below is an example of how
I would declare such a constant array of records in Delphi.
______________________________________________________

Type
TDBField =
Record
Number : Integer;
Name : String;
Description : String;
End;

Const
gcaDBFields : Array [0..2] Of TDBField =
(
(Number: 0; Name: ''; Description : ''),
(Number: 1000; Name: 'Field1'; Description: 'Field 1 Description'),
(Number: 2000; Name: 'Field2'; Description: 'Field 2 Description')
);
______________________________________________________

How do I accomplish the same in C#?

Thanks,
Erik
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

smartkid said:
public readonly static TDBField[] gcaDBFields

The gcaDBFields is not really readonly, the client code can still change the
element of that array.

To get a real constant array-like object, use ReadOnlyCollection instead of
an array.

You're right only gcaDBFields can not be assigned in user code
but all fields are not read-only.

It's a pity, if there is no simpler declaration of constant array :-(

Thank You & Regards
Marcin
 

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