Hating to kick a dead dog, but let me ask another typedef / data dictionary question...

B

Bailey.Hudson

C# is supposed to be an improvement over C++, and I've read a zillion
debates on using "using", no "typedef", no "include" to no avail. So
how can I implement data dictionary-like functionality in C#? I must
be missing something.

Let's say I want to create a class "Animal" that contains a field
"nLegs" that contains the number of legs, and "nEyes" that contains the
number of eyes. I want all of my code to declare variables that contain
the number of legs by the same type, and variables that count the
number of eyes by a (possibly) different type. (Or forget classes..
let's say that I just want to declare all of my eye-counter and
leg-counter variables consistently throughout all of my programs.) Now,
I initially think "int" will work for the number of legs and eyes. In
C++, of course, I put:

typedef int LEGS;
typedef int EYES;

in an #include'd .h file. Then in my code, I put:

#include <mytypes.h>
:
LEGS nLegs;
EYES nEyes;

and so on.

But sometime later I discover that there are animals with fractional
legs (go with me on this...) so I want to change all of my leg-counting
variables to float. So when I discovered the half-legged animals, I
just changed over to:

typedef float LEGS;

in one place (my .h file), and didn't have to worry about synchronizing
a zillion instances throughout all my modules.

How can I achieve the very same functionality in C#? Of course, my
project uses several source files, so just pasting the same bunch of
"using"s at the top of each one won't cut it. A class isn't really what
I want, either, I don't think. Ideas??

....R
 
B

Brendan Green

public class AnimalBase
{
protected int nLegs;
protected int nEyes;
}

public class AnimalInstance : AnimalBase
{
public AnimalInstance()
{
this.nLegs = 2;
this.nEyes = 2;
}
}

Then you discover that Animals have fractional legs. Change the AnimalBase
class.
 
R

Rob

Brendan said:
public class AnimalBase
{
protected int nLegs;
protected int nEyes;
}
public class AnimalInstance : AnimalBase
{
public AnimalInstance()
{
this.nLegs = 2;
this.nEyes = 2;
}
}

Then you discover that Animals have fractional legs. Change the AnimalBase
class.

But that doesn't help anywhere else. Forget classes; bad example on my
part. Let's say I have a program that uses lots of variables that count
legs. So I might have;

int nLegs;
int maxLegs, minLegs;
int rightLegs, leftLegs;
int frontLegs, backLegs;
:

and so on. And then I find out that legs need to be float instead of
int. Now what? I get to hunt down all of the leg-counting variables and
change them from int to float. Arg. I'd like to use:

using LEG_TYPE System.int32;

but I've have to re-code it in all my modules, which is precisely what
I'm trying to avoid (and in an app with lots of these, a real pain for
readibility). In lieu of #include, how can I make a single definition
in one place that will let me get the equivalent of typedef? Thanks!

....R
 
G

Greg Young

Unfortunately there is not a good way to handle this. I as well would love
to see typedefs come into the language.

It is important to note though that using and typedef work the same way (in
this simple case); the difference is in C/C++ you were including the same
file into many other files ... the crutch is that C# doesn't support include
fiiles. It is not that tough to write a pre-build include handler (which is
what I ended up doing)

In some situations you could however use generics to ease the pain of this a
bit (even going so far as putting in a lookup for the type) but imo this is
not very effective nor does it provide identical behavior to a typedef (more
so attacking the problem from a different direction)

Also I believe F# supports such a construct.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 

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