473,408 Members | 1,735 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,408 software developers and data experts.

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

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

Jul 26 '06 #1
3 1108
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.

<Ba***********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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

Jul 26 '06 #2
Rob
Brendan Green wrote:
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

Jul 26 '06 #3
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

"Rob" <Ba***********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Brendan Green wrote:
>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

Jul 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
6
by: j.ignatius | last post by:
I'm new to C# and come from a C++ background. C++ allows you to define new types very easily like this: typedef char array25_t; which defines a type called array25_t that contains up to 25...
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
2
by: Együd Csaba | last post by:
Hi All, I'm wonder if there is any possibility to kick out automatically stuck in queries after say 10 minutes or so? I mean some kind of queries which calls eg. buggy functions with dead loops...
3
by: Bailey.Hudson | last post by:
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...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
4
by: subramanian100in | last post by:
Consider the following: Class Test { public: Test(Test_int arg) : val(arg) { } typedef int Test_int; private:
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
3
by: clanjenkins | last post by:
Hi I have a dictionary object (let's call it _do) I am using to cache some values for use in a multi-threaded program. I have a single controlling parent thread, with a timer function _doTimer()...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
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...
0
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...

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.