• Advertisement

Archived

This topic is now archived and is closed to further replies.

C# and typedef

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
There is an alternative, although note you would have to have this at the top of every file:

using MyType = System.Int64; 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; } }
}
// end


Container.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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 code

MyContainer::value_type myVar = 5; // No problem

MyContainer.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!

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
C# is not C++. Stop trying to write C++ code in C#.



You can just say it''s impossible. I''ll accept reality and
move on. End of problem. No need for this kind of sarcastic
comments.

All I want to know is if it''s possible to emulate (for the
lack of a better word) some C++ features that I find very
useful.

It''s not like I #define { BEGIN and #define } END and
turn my C/C++ program into Pascal...

I know I can''t do that in Java, either. I was just hoping
C# could. Wishful thinking.




Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
tangentz:
Generics = Templates. It will not solve your problem. There is no typedef equivalent (well, someone did mention a wacky alternative below) nor is there a planned typedef equivalent for C# 2.0.

I still don''t understand the need for them in your situation (nor in any situation I can think of where they''re needed in C++). Don''t you design your application before building it?

Nypyren:
using Microsoft.DirectX;
Direct3D.Device dev = new Direct3D.Device();

Sure you have to types it''s namespace, but big woop. With a typedef you''d probably name it D3DDEVICE anyway. Slightly more wordy in C#, but certainly nothing that''s going to make spaghetti out of your code.

Share this post


Link to post
Share on other sites
quote:
Original post by wyrd
I still don''t understand the need for them in your situation (nor in any situation I can think of where they''re needed in C++). Don''t you design your application before building it?



The technique he is trying to emulate is used quite extensively in the C++ STL. You will likely have used it if you have used an iterator of a standard container.


std::vector<T>::iterator iter;


iterator is simply a typedef inside the vector class. There is also value_type, reference, size_type, and many others. They are used to abstract the types the standard containers use and return, providing a common interface.

Share this post


Link to post
Share on other sites
quote:
typedef is an obfuscation tool, nothing more.

It is a useful abstraction tool.

Share this post


Link to post
Share on other sites
quote:
Original post by wyrd
I still don''t understand the need for them in your situation (nor in any situation I can think of where they''re needed in C++). Don''t you design your application before building it?



I''m sorry you don''t udnerstand the need in my situation,
which is *VERY* common in C++ programming, especially with STL.

I guess you don''t code in C++/STL very much to understand
the usefulness. That''s OK. If you only ever coded in C#,
then maybe you think the language already has everything
and doesn''t need any more additions (except Generics).

Yes, I design my application before building it. I always do
and that''s exactly what I''m doing: design which data type is
the most suitable in my container, by experimenting with
different types. How would I know otherwise? I want some
empirical evidence before I go ahead and use it extensively
in my project(s).

Do you really expect to write 500+ lines of test code which
uses one specific type (say, int), and when later the
type is changed to long, you have to change them?

(In case you don''t know my anwser, it is *NO*.)


Jingo, DrPizza: thanks for the support. I thought I was
alone in wanting better abstraction programming that I''m
so used to do.



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
C# is not C++. Stop trying to write C++ code in C#.



Agreed. You''d be better off going back to the flexibility of C++ instead of settling for the "programming in a straight-jacket" model employed by the likes of C# and Java.



--
Dave Mikesell Software & Consulting

Share this post


Link to post
Share on other sites
First of all, wonderful reply, dmikesell

I have noticed these tendencys before: you ask if C#/java or whatever language support a certain C++ feature, and instead of a clear-cut "no" you get loads of "you don''t need that. its ugly. if you need it, your design sucks" etc. It''s like people fail to aknowledge c++''s superiority...

Share this post


Link to post
Share on other sites
I should probably clarify;
I understand the point of typdef in C++, and why someone would use it in that langauge. Unfortunately I cannot think of why someone would ever use typedef in C# (which is what we''re talking about).

Regardless of my understanding of the question or the reasons of applicability, there is no typedef equivalent in C#.

quote:

Do you really expect to write 500+ lines of test code which
uses one specific type (say, int), and when later the
type is changed to long, you have to change them?



I''m saying this is something one ought to know before even laying a finger on the keyboard. If you architecture is designed well enough, shouldn''t you well know what value types are required to accomplish the given goal?

In any case, this isn''t what the topic is about and I shouldn''t of brought it up. Implementation of a design model is another thread altogether. So I apologize for that.

Share this post


Link to post
Share on other sites
I also don''t really understand the advantages to using typedef like this. I can see how it makes it easy to change a type, yes, but is that really a big advantage?

Variable''s types are declared and known at compile time, so this can only give you an abstraction before compile time. You don''t have this in C#, but you still [should] have find and replace. Find and replace will make type changes like you described very easy, and it''ll only take a few more minutes.

C# also has some Type identification builtin to the language, but its done at runtime [i think] so its not really the same. Part of reflection if you''re interested.

Personally I think typedefs make code really nasty as they confuse things to someone who''s new to the code. But I''ve also never liked STL because I think its the ugliest thing in the entire programming world. Since I never use STL though I can''t really tell you for sure that typedefs aren''t useful. Just my two cents though.


-out

Share this post


Link to post
Share on other sites
quote:
Original post by wyrd
I''m saying this is something one ought to know before even laying a finger on the keyboard. If you architecture is designed well enough, shouldn''t you well know what value types are required to accomplish the given goal?



Agreed. That''s how things work in a perfect world. You design everything once, and than implement it under the next five years without changing a single design choice.
Sadly, we live in a far from perfect world. You often need to change the existing design during the development process. I think it''s the programmers duty to make such a design change as smooth as possible.

Share this post


Link to post
Share on other sites
quote:
I can see how it makes it easy to change a type, yes, but is that really a big advantage?

It also lets you hide a type name. C# or Java would instead make you hide them behind an interface. This works right up until you want to use a value type, which can''t be hidden behind an interface.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by BillPo
Personally I think typedefs make code really nasty as they confuse things to someone who''s new to the code. But I''ve also never liked STL because I think its the ugliest thing in the entire programming world. Since I never use STL though I can''t really tell you for sure that typedefs aren''t useful. Just my two cents though.



Which is why should stick to C#, leave C++ for the big boys.



Share this post


Link to post
Share on other sites
yep, the big burly masochists.

The truth is, for application development Java and C# own C++ hands down. Myself and many others I know have found that switching to Java or C# increases your productivity immensely. Suddenly instead of worrying how to write a container for your data, or what exactly you should use to store your data, or whatever, you''re writing the application and making it work. Data is all handled nicely. .NET has a HUGE assortment of classes already made for you and they work very nicely, they have clean and clear syntax and naming, and they''re fast enough to do the job.

But this is off topic.

So apparently typedefs can hide a type... i''m not quite sure why you''d want to do this, or what is meant by this. The problem I''m running into is, well yeah, sure you can abstract/hide the type and generalize... but what about when you finally start working with data and it has to be stored in your container? You pretty much _have_ to know the type there, so you''re going to know what type your container has. So... whats the point? What am I missing?

-out

Share this post


Link to post
Share on other sites

  • Advertisement