how to define a table in c#

  • Thread starter Thread starter Priyesh
  • Start date Start date
P

Priyesh

How would i do something similar to this in C#?

enum eType
{
endTable,
};
class CTableDef
{
eType m_eType ;
public:
CTableDef(eType e):m_eType(e){}
};

#define BEGIN_MY_TABLE(name) CTableDef name[] = {

#define END_MY_TABLE() CTableDef(endTable) } ;

BEGIN_MY_TABLE(Table1)

END_MY_TABLE()

//
should translate to CTableDef Table1[] = {CTableDef(endTable) };

TIA
 
Priyesh,
You can not use "#define "a" "b"" in C#
#defines are only allowed for defining symbols

#define DEBUG
....
#if DEBUG
....
#else
...
#endif
 
Is there a way i could do it in C# using some other C# specific mechanism?
Or am i missing some other easy way to achieve the same result?


Lebesgue said:
Priyesh,
You can not use "#define "a" "b"" in C#
#defines are only allowed for defining symbols

#define DEBUG
...
#if DEBUG
...
#else
..
#endif

Priyesh said:
How would i do something similar to this in C#?

enum eType
{
endTable,
};
class CTableDef
{
eType m_eType ;
public:
CTableDef(eType e):m_eType(e){}
};

#define BEGIN_MY_TABLE(name) CTableDef name[] = {

#define END_MY_TABLE() CTableDef(endTable) } ;

BEGIN_MY_TABLE(Table1)

END_MY_TABLE()

//
should translate to CTableDef Table1[] = {CTableDef(endTable) };

TIA
 
Priyesh said:
How would i do something similar to this in C#?
#define BEGIN_MY_TABLE(name) CTableDef name[] = {
#define END_MY_TABLE() CTableDef(endTable) } ;
should translate to CTableDef Table1[] = {CTableDef(endTable) };

C# doesn't have preprocessor macros, so you will have to use a different
approach.

The purpose of all this appears to be simply defining some static arrays
("tables") in your source code, while making sure they all have a
terminating entry of some special value ("endTable"), just like use of zero
terminated char[] for strings in C.

Use of terminator values was born out of necessity in C, where an arrary
reference is nothing more than a pointer to the first of unknown number of
elements. It is completely unnecessary in C#, where all arrays have the
Length property. In your example, you can just look at Table1.Length and
you will know how many items it has. So, your first / best option is to
simply stop using the terminating entries.

If you need to retain the terminators because some existing code relies on
them, the easiest thing to do would be remembering to add them at the end of
any arrays you are going to define.
 
Thanks for your reply. I understand the example i gave might not translate
to, or encapsulate concepts in C#.
Pre-Processor macros are not possible, so this approach wouldnt float. Good
to know.

I guess what i miss then would me trying to have a feel of static data (all
the caps and underscores makes me feel at home.. :))
But that could be achieved by having a static class with an array or list

Thanks
Priyesh.

Chris Priede said:
Priyesh said:
How would i do something similar to this in C#?
#define BEGIN_MY_TABLE(name) CTableDef name[] = {
#define END_MY_TABLE() CTableDef(endTable) } ;
should translate to CTableDef Table1[] = {CTableDef(endTable) };

C# doesn't have preprocessor macros, so you will have to use a different
approach.

The purpose of all this appears to be simply defining some static arrays
("tables") in your source code, while making sure they all have a
terminating entry of some special value ("endTable"), just like use of
zero terminated char[] for strings in C.

Use of terminator values was born out of necessity in C, where an arrary
reference is nothing more than a pointer to the first of unknown number of
elements. It is completely unnecessary in C#, where all arrays have the
Length property. In your example, you can just look at Table1.Length and
you will know how many items it has. So, your first / best option is to
simply stop using the terminating entries.

If you need to retain the terminators because some existing code relies on
them, the easiest thing to do would be remembering to add them at the end
of any arrays you are going to define.
 
Back
Top