V
vvv
Hi All,
Do we have anything in .NET which is equivalent to C++'s Typedef .
Regards,
Vasanth
Do we have anything in .NET which is equivalent to C++'s Typedef .
Regards,
Vasanth
Do we have anything in .NET which is equivalent to C++'s Typedef .
vvv said:I was trying to create wrapper classes for all the primitive
datatypes, but as vb.net doesn't support operator overloading, i was
not able to use it. Even if it supports i will have to use 'new'
keyword for declaring any variables. That is again a problem.
* "Armin Zingler said:Out of interest: Why do you need it? Neither I needed typedefs nor operator
overloading so far.
butI was trying to create wrapper classes for all the primitive datatypes,
vvv said:Thanks. It works, but as you said it is restricted to the file where it is
declared.
If you have anyother round about way to achieve this, pls let me know.
I was trying to create wrapper classes for all the primitive datatypes, but
as vb.net doesn't support operator overloading, i was not able to use it.
Even if it supports i will have to use 'new' keyword for declaring any
variables. That is again a problem.
Vasanth
Vasanth said:You would require this to achieve some kind of program dicipline and
control of data type usage.
In enterprise applications, some developers might use datatype
'Single' for Price field while others use decimal, similarly
Integer/Long for Quantity field.
If you create typedefs for those primitive datatypes like the
following (C++ syntax)
"Types.h"
Typedef Integer TQuantity;
Typedef Decimal TPrice;
"App.cpp"
# Include "Types.h"
TQuantity mQty;
TPrice mPrice;
we can force the developer to always use these typedefs through out
our application. And in future if you need to change the datatype for
Quantity field from Integer to Long, it would be easy changing it in
one place without touching other parts of our application.
* "Jay B. Harlow said:Unfortunately (or is it fortunately? ;-)) that usage of a typedef is not
supported in .NET.
That's why I included fortunately!If you have a look at C(++) code, it's easy to see why typedefs are not
a good idea. Every company defined a type with different name for the
same purpose, ending up in thousands of semantically equal types with
different names (including the company's name in the worst case).
So, in this case you will get an error if you use alias TQuantity forPublic Sub DoIt(amount As Integer)
Public Sub DoIt(amount As TQuantity)
Jay B. Harlow said:Herfried & Vasanth,That's why I included fortunately!If you have a look at C(++) code, it's easy to see why typedefs are not
a good idea. Every company defined a type with different name for the
same purpose, ending up in thousands of semantically equal types with
different names (including the company's name in the worst case).
I find they're a good idea (in C code) if properly managed, as you pointed
out they are rarely properly managed.
For the Win32 APIs I find them very handy! For most other cases I find them
more trouble then they are worth. However with System.Int16, Int32, Int64,
IntPtr the API reason largely goes away. Seeing as my APIs are in a single
Module I can do the Alias thing to make the API defs look "pretty" ;-)
With OOP (VB.NET, C++) I would define an actual type when I needed it,
rather then using TypeDef, as one of the reasons for OOP is introducing new
Types. (Don't remember specifically where I read that, but it makes sense).
This also allows giving that type custom behavior!
Note I will use typedefs in C++ when working with templates, simple because
its easier to remember the parameters to the template, ss the typedef
encapsulates the templates type parameters.
Also due to the stricter type checking (in C# & Option Strict On in VB.NET)
in .NET I suspect typedefs will not work as well as the original poster
wants.
Option Strict On
' fictitious VB.NET syntax
TypeDef TQuantity As Integer
TypeDef TPrice As Decimal
Public Sub Main
Dim i As Integer
Dim quantity As TQuantity
i = quantity
quantity = i
End Sub
If the above were allowed implicitly (as in C++) you will ultimately have
problems as changing TQuantity to Single will cause compile errors.
However if TypeDef types required explicit conversions, then changing
TQuantity to Single should not cause compile errors, as the type conversion
would deal with it.
i = CType(quantity, Integer)
quantity = CType(i, TQuantity )
In either case the TypeDef would have to be emitted as Meta Data, so
referencing the assembly allows using the TypeDef'd type. I suspect the JIT
may need to maintain Type info for the TypeDefs, however the executable code
could be (should be) shared with the underlying Type (which I understand the
JIT will do for Generics in Whidbey).
Also how does overloading on a TypeDef work?
Public Sub DoIt(amount As Integer)
Public Sub DoIt(amount As TQuantity)
Is the above allowed or are they considered different? How does the above
work if implicate conversions allowed? (does it follow the same logic that
causes 0 going to an Enum override rule?)
I'm curious, does Delphi allow TypeDefs? How does it handle conversions &
overloading?
That being said, I can see using TypeDef with Generics in Whidbey, to
simplify defining instances(?) of Generic Types. I could type def the
PersonCollection in the Person.vb file, then every place I need a collection
of People I could use PersonCollection, rather then trying to remember is
PersonCollection a Dictionary keyed by String or something else...
TypeDef PersonCollection As Dictionary<String, Person>
Dim people As PersonCollection
However I'm not sure how often I would need a 'stand alone'
PersonCollection, versus the collection simply being an attribute (field or
property) of some parent class...
Just a thought
Jay
I was thinking that, however its been to long since I used them in C/C++.As we know typedef is not a separate type, but only a synonym for another
type.
Vasanth said:Thanks Jay, Herfried & Mattias for all the inputs.
Jay
As we know typedef is not a separate type, but only a synonym for another
type. Therefore, functions that differ by typedef types only may not have
the same name.So, in this case you will get an error if you use alias TQuantity forPublic Sub DoIt(amount As Integer)
Public Sub DoIt(amount As TQuantity)
Integer type otherwise it should be fine. This is how C++ works.
Delphi allows Typedefs and I guess the above must be true for delphi also.
Vasanth
Jay B. Harlow said:Herfried & Vasanth,That's why I included fortunately!If you have a look at C(++) code, it's easy to see why typedefs are not
a good idea. Every company defined a type with different name for the
same purpose, ending up in thousands of semantically equal types with
different names (including the company's name in the worst case).
I find they're a good idea (in C code) if properly managed, as you pointed
out they are rarely properly managed.
For the Win32 APIs I find them very handy! For most other cases I find them
more trouble then they are worth. However with System.Int16, Int32, Int64,
IntPtr the API reason largely goes away. Seeing as my APIs are in a single
Module I can do the Alias thing to make the API defs look "pretty" ;-)
With OOP (VB.NET, C++) I would define an actual type when I needed it,
rather then using TypeDef, as one of the reasons for OOP is introducing new
Types. (Don't remember specifically where I read that, but it makes sense).
This also allows giving that type custom behavior!
Note I will use typedefs in C++ when working with templates, simple because
its easier to remember the parameters to the template, ss the typedef
encapsulates the templates type parameters.
Also due to the stricter type checking (in C# & Option Strict On in VB.NET)
in .NET I suspect typedefs will not work as well as the original poster
wants.
Option Strict On
' fictitious VB.NET syntax
TypeDef TQuantity As Integer
TypeDef TPrice As Decimal
Public Sub Main
Dim i As Integer
Dim quantity As TQuantity
i = quantity
quantity = i
End Sub
If the above were allowed implicitly (as in C++) you will ultimately have
problems as changing TQuantity to Single will cause compile errors.
However if TypeDef types required explicit conversions, then changing
TQuantity to Single should not cause compile errors, as the type conversion
would deal with it.
i = CType(quantity, Integer)
quantity = CType(i, TQuantity )
In either case the TypeDef would have to be emitted as Meta Data, so
referencing the assembly allows using the TypeDef'd type. I suspect the JIT
may need to maintain Type info for the TypeDefs, however the executable code
could be (should be) shared with the underlying Type (which I understand the
JIT will do for Generics in Whidbey).
Also how does overloading on a TypeDef work?
Public Sub DoIt(amount As Integer)
Public Sub DoIt(amount As TQuantity)
Is the above allowed or are they considered different? How does the above
work if implicate conversions allowed? (does it follow the same logic that
causes 0 going to an Enum override rule?)
I'm curious, does Delphi allow TypeDefs? How does it handle conversions &
overloading?
That being said, I can see using TypeDef with Generics in Whidbey, to
simplify defining instances(?) of Generic Types. I could type def the
PersonCollection in the Person.vb file, then every place I need a collection
of People I could use PersonCollection, rather then trying to remember is
PersonCollection a Dictionary keyed by String or something else...
TypeDef PersonCollection As Dictionary<String, Person>
Dim people As PersonCollection
However I'm not sure how often I would need a 'stand alone'
PersonCollection, versus the collection simply being an attribute (field or
property) of some parent class...
Just a thought
Jay
Herfried K. Wagner said:* "Jay B. Harlow [MVP - Outlook]" <[email protected]> scripsit:
Unfortunately (or is it fortunately? ;-)) that usage of a typedef is not
supported in .NET.
If you have a look at C(++) code, it's easy to see why typedefs are not
a good idea. Every company defined a type with different name for the
same purpose, ending up in thousands of semantically equal types with
different names (including the company's name in the worst case).