473,320 Members | 2,003 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,320 software developers and data experts.

Types depending on other types

Hi,

I want to define a local variable having a type defined somewhere else. Is
this possible in C#?
Here's a more detailed description of what I'm about:

Given a member somewhere in a class:

class C
{public int m;}
In some function I want do define a variable having exact the type of the
member defined above without actually knowing that type, somewhat like:

void f()
{C::m v;}
So if I change the class definition to:

class C
{public double m;}

the local variable v in my function f() becomes of the same type -- double.
TIA,
Axel Dahmen
Nov 16 '05 #1
4 1086
Any reason for not declaring it as object?

Telmo Sampaio

"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:#C**************@TK2MSFTNGP09.phx.gbl...
Hi,

I want to define a local variable having a type defined somewhere else. Is
this possible in C#?
Here's a more detailed description of what I'm about:

Given a member somewhere in a class:

class C
{public int m;}
In some function I want do define a variable having exact the type of the
member defined above without actually knowing that type, somewhat like:

void f()
{C::m v;}
So if I change the class definition to:

class C
{public double m;}

the local variable v in my function f() becomes of the same type -- double.

TIA,
Axel Dahmen

Nov 16 '05 #2
Axel,

Quite simply, you can't do that. What you would have to do is create a
variable of type object, and then store the contents of the field in there,
or keep track of the type through reflection.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I want to define a local variable having a type defined somewhere else. Is
this possible in C#?
Here's a more detailed description of what I'm about:

Given a member somewhere in a class:

class C
{public int m;}
In some function I want do define a variable having exact the type of the
member defined above without actually knowing that type, somewhat like:

void f()
{C::m v;}
So if I change the class definition to:

class C
{public double m;}

the local variable v in my function f() becomes of the same type -- double.

TIA,
Axel Dahmen

Nov 16 '05 #3
I won't delve into WHY you want to do this - it seems a bit fishy to me... But...

You could do this in C++ with a typedef but C# doesn't have a "typedef" keyword... You could do this in C++ using templates but C# doesn't have templates {yet}...

You could be adventurous and write code to the Whidbey Beta 2.0 .NET Framework {2+ gig download from MSDN}...
You could wait until Whidbey is released and V2.0 of the Framework is standard...

--Richard

"Axel Dahmen" wrote:
Hi,

I want to define a local variable having a type defined somewhere else. Is
this possible in C#?
Here's a more detailed description of what I'm about:

Given a member somewhere in a class:

class C
{public int m;}
In some function I want do define a variable having exact the type of the
member defined above without actually knowing that type, somewhat like:

void f()
{C::m v;}
So if I change the class definition to:

class C
{public double m;}

the local variable v in my function f() becomes of the same type -- double.
TIA,
Axel Dahmen

Nov 16 '05 #4
Thanks to all of you.

I don't want to use "object" because I think it has some drawbacks to use
it. It's not typesafe and it bears packaging as well as unnecessary
conversion overhead.

My problem was (is), that I've been using byte as a "percentage" member
property throughout my program when I realized it should be double to allow
for fractional percentage values. So I had to amend all local variables
storing this value temporarily.

For me it seem logical to write code like "use a type mentioned somewhere
else and don't care what it is as long it provides all necessary operators."
Of course this results in what an object is, but as I mentioned, I prefer
early binding instead of late binding. Reflection is nifty when dealing with
unknown objects but my code knows all the objects at compile time.

Thanks for enlighting me, Richard.

Best regards,
Axel Dahmen
------------------------------
"Richard" <Ri*****@discussions.microsoft.com> schrieb im Newsbeitrag
news:CE**********************************@microsof t.com...
I won't delve into WHY you want to do this - it seems a bit fishy to me... But...
You could do this in C++ with a typedef but C# doesn't have a "typedef" keyword... You could do this in C++ using templates but C# doesn't have
templates {yet}...
You could be adventurous and write code to the Whidbey Beta 2.0 .NET Framework {2+ gig download from MSDN}... You could wait until Whidbey is released and V2.0 of the Framework is standard...
--Richard

"Axel Dahmen" wrote:
Hi,

I want to define a local variable having a type defined somewhere else. Is this possible in C#?
Here's a more detailed description of what I'm about:

Given a member somewhere in a class:

class C
{public int m;}
In some function I want do define a variable having exact the type of the member defined above without actually knowing that type, somewhat like:

void f()
{C::m v;}
So if I change the class definition to:

class C
{public double m;}

the local variable v in my function f() becomes of the same type -- double.

TIA,
Axel Dahmen

Nov 16 '05 #5

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

Similar topics

0
by: Manlio Perillo | last post by:
Hi. I have found that some types are missing from module types: import types from types import * def check_type(obj): type_list =
4
by: Peter | last post by:
Please, could anyone tell me what differences is in using C# data types or ..NET types. For example in what situations I would have to use .NET types instead of C# types. Lets take C# bool and .NET...
0
by: Daniel | last post by:
I want to be able to define types in one schema, and then use them in other schemas. So far so good. Now I also want to be able to generate classes from these schemas and use them independantly,...
1
by: Rafael Veronezi | last post by:
Just to fix, correct me if I am wrong... With reference types (objects), if I assign an object to another, the assignment will be the address of the object, and not a copy of it's contents right?...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
11
by: Roman Mashak | last post by:
Hello, All! I'm implementing function parsing config file, which look like this: # this is comment port=10000 path=/var/run/dump.pid .... Declared the type
2
by: namo | last post by:
Hello, I have started a small simulation project to learn C++ and I am faced with the following problem : the user can choose at runtime to simulate protocol P1 or P2. P1 uses packets of type...
3
by: Reuben | last post by:
Hi, I'm creating a struct hat contains an array. Unfortunatly, all arrays are reference types, which means that when someone copies/assigns it, they have a reference to the orginal object, which...
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
0
by: rn5a | last post by:
A user control file named Address.ascx has 4 TextBoxes. The logic of this user control is encapsulated in a code-behind file named Address.ascx.vb. The code-behind also defines an Event named...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.