473,659 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1179
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.goo glegroups.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.goo glegroups.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
4240
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
381
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 chars. Does a similar typedef also exist in C#? Or do you have to define a new class and assign member variables?
12
15633
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
2
1362
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 or something similar. Time to time I face such kind of problems which are solved automatically after some hours/days. Is there any options in config files or other settings to shorten this time?
3
1112
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 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...
12
4637
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 (*(*(*ptr2d_fptr)()))();
16
2766
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 threads, there is sound advice ( to me at any rate) not to hide pointers behind typedefs. So, may I ask the group when the use of typedefs really makes sense? Sorry if this is somewhat general, but there are no exercises ( not that I am asking for...
3
2282
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() which can kick off child threads with a new instance of class MyThreadObject in each. My parent thread has _do as a property and with each time _doTimer() gets called, it may or may not repopulate _do depending on cacheflag settings. I pass a...
0
8335
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
8851
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...
1
8528
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
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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 project—planning, coding, testing, and deployment—without 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...
0
5649
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();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
2
1976
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.