C# and typedef

Started by
130 comments, last by TangentZ 19 years, 11 months ago
I''ve tried searching around for this problem but haven''t found anything useful. In C#, is there something equivalent to C/C++''s typedef? I''m creating a container class and want to make a typedef of the value type.

class Container
    {
    // The following will not compile under C#

    public typedef long  ValueType;  // Can be double or something else

    }
// end


Container.ValueType myVar;  // Use the same type to declare myVar

Is there any way to do something like this? For those who are familiar with C++ STL, it''s very routine to do so:

class Container
    {
    public:
        typedef int value_type;  // Can be double or something else

        std::vector<value_type>::size_type mySize;
    }
// end

Or do I have to wait for Generic C# (C# 2.0)? Will it even have that capability? This is very frustrating. Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Advertisement
I work for a company using C# as our main dev language, and as far as i know typedef is NOT THERE ... which sucks so bad it''s crazy ...

My boss has actually told me to write up my complaint about this and submit it to him so he can forward it on to his microsoft contact ... if you can come up with a succint way to explain why we need typedefs then it may make my case better .. or better still, write your own complaint email and send it to a developer support address, so they''ll get more complaints.
There is an alternative, although note you would have to have this at the top of every file:

using MyType = System.Int64; 
________________________________________________Chris McGuirkLead Programmer - Axiom 3D EngineC# gaming, the way of the future!http://www.axiom3d.org
If all you''re doing is using typedef to mask the fact that it''s a member variable (which the code looks to be doing), then use properties. That''s what they''re for.

As for typedef in general, I''ve never had a use for it while programming in C#. C# has very clean syntax, and if you''re making some ugly syntax with it, then you''re doing something wrong.
typedef is an obfuscation tool, nothing more.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
No, I want to declare a variable as the same type used in
the Container, whatever that is.

I tried this:

class Container    {    private Type LongType = typeof(long);    public Type Long { get { return LongType; } }    }// endContainer.LONG myLongVar;  // Compile Error!!!


Xai: I feel your pain...

wyrd: I''m hiding the data type *AND* declaring variables
outside of the class so they are compatible. Is that very
unusual? I don''t think so. I do that all the time in C++.
A Type cannot be used to declare a new variable.

leedgitar: I want to declare inside the class scope, not
global at the top of the file.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
I still don''t get what you''re trying to do, or why. In your example, just use long. Why would you need to return a Type object to mask the fact that it''s a long? I could see why in C++ you''d want to define a LONG and use that as your 32 bit integer, but in C# there''s no need for such sillyness, just use int and it''s 32 bits no matter what.

In any case, you can dynamically instantiate Types at runtime using reflection.

Perhaps if you''d be more clear as to what you''re trying to do (as in your actual application and not some random example that holds no purpose), we''d be able to offer up an elegant solution that doesn''t require typedef trickery.
Hmmm... I thought I was pretty clear on what I''m trying to do.

I''m designing a kind of Container. It contains a specific
type of values. Whether it''s byte, int, long, float, double,
I haven''t decided yet. I want to be able to use a "typedef"
defined by the container so that I write outside code and
declare varibles of that type.

This way, when I later decide to change the type, I can do
so without changing outside code I''ve already written.

In C++, that''s extremely easy.

class MyContainer    {    public:        typedef int value_type;        std::vector<value_type> values;    };// end// Outside codeMyContainer::value_type myVar = 5;   // No problemMyContainer.add(myVar);


I don''t know any more elegant than that. Extremely simple
and effective. I can change "int" to "long" or "short"
without changing the outside code.

*THAT* is what I want.

If you think that''s a useless example, think OpenGL vertices.
They can be stored as GL_BYTE, GL_INT, GL_FLOAT, GL_DOUBLE..etc.
I want to test with different data types.

Why is it so hard in C#? Hmm...

And, no, I''m not asking for templates (generics). My
container will use only one specific type when I''m done.

I don''t know, wyrd, do you have a good solution to my problem?


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
using MyInt = System.Int32;   


Bah. There ya go.

The real application for this is when you have an ambiguous class, like Microsoft.DirectX.Direct3D.Device and Microsoft.DirectX.DirectSound.Device. You give an alias like Device3d or DeviceSound or something to fix the ambiguity.

But you can use it on value types as well, so whatever floats your boat.

BTW - you HAVE to use this at the namespace scope. You're probably OK with that.

[edited by - Nypyren on January 12, 2004 12:50:10 AM]
C# is not C++. Stop trying to write C++ code in C#.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement