Jump to content
  • Advertisement
Sign in to follow this  
Estauns

[.net] Typedef or something similar?

This topic is 4876 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'm looking to wrap a short around a typedef or something similar. Basically I want to declare HANDLEs for my resources (Textures, sounds, etc.) and I just want them to be simple shorts (possibly ints in the future, I'm not sure) but I don't want to do like: struct TextureHandle { short ID } or something like that.

Share this post


Link to post
Share on other sites
Advertisement
Probably the easiest way to do this is to use a struct. This also makes it so that you can easily expand your TectureHandles in the future if need be (or even put a reference to the texture right in there with the short).

Share this post


Link to post
Share on other sites
Can't you use the using keyword to give a type an alias? (Sorry I don't have my C# compiler installed to test it out right now.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
using MyType=System.Int16;
(typing "short" instead of "System.Int16" makes the complier complain.)

Share this post


Link to post
Share on other sites
I have to do that in every file though, don't I?

I really just want a generic handle type, so that I can use it for my resources, but I don't want to allocate new structs + a new short or int.

Share this post


Link to post
Share on other sites
If the only thing you put in a new struct is an int or a short, then the struct shouldn't have any additional overhead. You can add operators to cast it to an int or short, which I bet the JIT will inline such that using your struct won't be any less efficient than using a plain int or short.

Share this post


Link to post
Share on other sites
About 3 months into .NET programming I needed this exact thing ... typedefs ... cause I'm used to C++ template and typedef programming for rapidly modeling a problem ... while still retaining the ability to replace with custom classes later as needed ...

and C# / .NET jut doesn't support it at all as far as I know.

I have since been programming in .NET for 2 years now, and still don't know how to do the equivelent thing - or anything with all the benifits and no down sides ... C# really makes me a worse coder, because all the most powerfull key features, like const correctness just aren't there ... so you cannot correctly and efficiently model a problem domain. You can either do it correct / safely. Or efficiently. But not both...

Share this post


Link to post
Share on other sites
I'd go with either a simple int or with a custom struct. The custom struct shouldn't be any different from any value type you'd be using. If you're putting handles in lists or in dictionaries/hashtables, just make sure to overload Equals() and GetHashcode().

And Xai, I also miss const in some cases. As a replacement in certain cases, I've defined a read-only interface and implemented a class that has both reading/writing members. The interface may define only a get for a particular property, while the corresponding class defines both get and set for the property.

Share this post


Link to post
Share on other sites
yeah, one reason typedef will NEVER really be in the .NET universe quite the same as it is in C++ is the whole value vs. ref type problem. It is totally impossible to refactor a type from value to ref or vice-versa without causing major semantic changes that affect the client of the type ...

But still, would be nice if they create typedefs anyway, even if you have to stay the same value/ref classification ...

For instance, if you plan to write a class around your math type later, you would have to typedef the Integer classes, NOT the int value types ... but if you intended to use a custom value type later, you would typedef the builtin value types to start with ... too bad they don't have them :(

Thanks for the readonly interface info ... That exact choice had not occured to me ... I'll look into it.

Share this post


Link to post
Share on other sites
I'm kind of glad that typedefs aren't in the language, though, mainly because everyone seems to want to create their own typedefs of the basic data types, leading to numerous different types for things such as ints and floats.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!