Hi All,
Do we have anything in .NET which is equivalent to C++'s Typedef .
Regards,
Vasanth 12 15590 Do we have anything in .NET which is equivalent to C++'s Typedef .
Not in VB.NET, no. The closest thing is that you can do
Imports SomeAlias = SomeType
and then use SomeAlias instead of SomeType. But that's pretty limited
compared to typedef.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ua**************@TK2MSFTNGP12.phx.gbl... Do we have anything in .NET which is equivalent to C++'s Typedef .
Not in VB.NET, no. The closest thing is that you can do
Imports SomeAlias = SomeType
and then use SomeAlias instead of SomeType. But that's pretty limited compared to typedef. Mattias
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
"vvv" <so*****@microsoft.com> schrieb 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.
Out of interest: Why do you need it? Neither I needed typedefs nor operator
overloading so far.
--
Armin http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
* "Armin Zingler" <az*******@freenet.de> scripsit: 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.
Out of interest: Why do you need it? Neither I needed typedefs nor operator overloading so far.
Maybe it would be usefup to "typedef" a "HWND" type for window handles,
etc., but I don't need it too. It would cause more confusion than it
increases readability.
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
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.
Vasanth
"Armin Zingler" <az*******@freenet.de> wrote in message
news:eA*************@TK2MSFTNGP10.phx.gbl... "vvv" <so*****@microsoft.com> schrieb 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. Out of interest: Why do you need it? Neither I needed typedefs nor
operator overloading so far.
-- Armin
http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
Vasanth, I was trying to create wrapper classes for all the primitive datatypes,
but
Remember that "the primitive datatypes" are structures, if you want to avoid
using "new" then consider defining your wrapper Classes as wrapper
Structures instead.
Of course be aware of the differences between a value type (Structure) and a
reference type (Class).
Note when Whidbey (VS.NET 2004) ships in the later half of 2004 we will have
operator overloading! Along with conversion operators, which allows you to
"hide" the new. http://blogs.gotdotnet.com/cambecc/p...1-127195cceeab
Out of curiosity why are you wrapping the primitive datatypes? Or should I
ask why are you wanting a typedef?
In my experience using a typedef is different then creating a wrapper. IMHO
Creating a wrapper where I would have used a typedef seems to be more pain
then gain.
Hope this helps
Jay
"vvv" <so*****@microsoft.com> wrote in message
news:eY**************@tk2msftngp13.phx.gbl... 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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message news:ua**************@TK2MSFTNGP12.phx.gbl...Do we have anything in .NET which is equivalent to C++'s Typedef .
Not in VB.NET, no. The closest thing is that you can do
Imports SomeAlias = SomeType
and then use SomeAlias instead of SomeType. But that's pretty limited compared to typedef. Mattias
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
"Vasanth" <so*****@microsoft.com> schrieb 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.
I see the advantage, but it also drove me crazy when finding out what the
real data type of a variable is. Is a WORD 16 or 32 bits for example. In
addition some types are case sensitive or conditional compilation is used,
so it was often very confusing for me - but I am not a C specialist at all.
--
Armin http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
Vasanth,
Unfortunately (or is it fortunately? ;-)) that usage of a typedef is not
supported in .NET.
You could always use MSWish to submit a request that TypeDefs be supported
in .NET. http://register.microsoft.com/mswish/suggestion.asp
I would only define "wrappers" for the primitive types if I truly needed to
define a new type. For example a Duration type that represents an Age.
Where Duration has a lot in common with Integer, however Duration has unique
constraints (>= 0) and unique behaviors. Integer would be an implementation
detail of Duration, Duration would not be a wrapper per se for Integer. I
generally consider wrappers to be proxy objects (Proxy Pattern), Duration is
not a Proxy pattern!
I would normally start Age as an integer, then using Refactoring
( http://www.refactoring.com) I would change it to the Duration type when a
Duration type was needed. Obviously when we get Operator Overloading in
Whidbey it would make implementing a Duration type easier.
Hope this helps
Jay
"Vasanth" <so*****@microsoft.com> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl... 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.
Vasanth
"Armin Zingler" <az*******@freenet.de> wrote in message news:eA*************@TK2MSFTNGP10.phx.gbl... "vvv" <so*****@microsoft.com> schrieb 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.
Out of interest: Why do you need it? Neither I needed typedefs nor operator overloading so far.
-- Armin
http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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).
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Herfried & Vasanth, 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).
That's why I included fortunately!
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 [MVP]" <hi***************@gmx.at> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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).
-- Herfried K. Wagner [MVP] <http://www.mvps.org/dotnet>
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. Public Sub DoIt(amount As Integer) Public Sub DoIt(amount As TQuantity)
So, in this case you will get an error if you use alias TQuantity for
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:#I*************@tk2msftngp13.phx.gbl... Herfried & Vasanth, 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). That's why I included fortunately!
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 [MVP]" <hi***************@gmx.at> wrote in message news:eH****************@TK2MSFTNGP12.phx.gbl... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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).
-- Herfried K. Wagner [MVP] <http://www.mvps.org/dotnet>
Vasanth, As we know typedef is not a separate type, but only a synonym for another type.
I was thinking that, however its been to long since I used them in C/C++.
:-)
Also I was questioning if a typedef should be only a synonym in .NET. As my
concern with it being a synonym or alias is you are introducing ambiguities
into your program that may compile fine today, however if you changed one of
the typedefs you will receive compile errors tomorrow. Seeing as the import
alias (available now) is per file, there is only a single file you are
impacting by changing that alias.
Just a thought
Jay
"Vasanth" <so*****@microsoft.com> wrote in message
news:OC****************@TK2MSFTNGP12.phx.gbl... 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. Public Sub DoIt(amount As Integer) Public Sub DoIt(amount As TQuantity) So, in this case you will get an error if you use alias TQuantity for 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 [MVP - Outlook]" <Ja************@msn.com> wrote in message news:#I*************@tk2msftngp13.phx.gbl... Herfried & Vasanth, 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). That's why I included fortunately!
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 [MVP]" <hi***************@gmx.at> wrote in message news:eH****************@TK2MSFTNGP12.phx.gbl... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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).
-- Herfried K. Wagner [MVP] <http://www.mvps.org/dotnet>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: joe |
last post by:
hi,
after reading some articles and faq,
i want to clarify myself what's correct(conform to standard) and
what's not?
or what should be correct but it isn't simply because compilers don't...
|
by: Mehta Shailendrakumar |
last post by:
Hi all,
Have alook at two different declarations below:
typedef unsigned char uc;
typedef void (*fptr) (void);
The first declaration has a left part which is a C keyword: "unsigned char"...
|
by: Dennis Myrén |
last post by:
Hi.
Is there any way to define an alias for a type like you could in C++:
typedef int NUMBER;
I tried using #define but that did not work.
Thank you.
--
Regards,
|
by: jacob navia |
last post by:
Hi
Suppose you have somewhere
#define BOOL int
and somewhere else
typedef BOOL int;
|
by: Christian Christmann |
last post by:
Hi,
what is the difference between a "#define" and typedef?
A "#define" is handled by the preprocessor and serves as a name
substitude whereas "typedef" is a declaration of a new data type...
| |
by: George |
last post by:
Is there a C++ typedef equivalent in C#?
Thanks,
--
George
|
by: John |
last post by:
I thought I had a fairly good understanding of typedefs until I came across
the following passage from C++ Primer, fourth edition by Stanley B. Lippman:
"The mistake is in thinking of a typedef...
|
by: rajm2019 |
last post by:
hi to all
i am new to c++ i want to know wat do we mean bye
typedef support::PtrObject<DirectoryDirectoryRef
when we are using (DirectoryRef abc);
is abc an object of template class...
|
by: Carmen Sei |
last post by:
I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.
like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |