Casting my own types

rculver9056

Newcomer
Joined
Apr 3, 2010
Messages
3
I have been playing around with numbers in c# (learning!), and I wonder how I would go about making a struct of mine act like one of the built-ins (such as byte)

With a byte, I can go '(byte)value' to get a value within the byte range, 0..255.
My struct will have values 0..47,000 - I want to be able to go '(myType)value' to get it within that range, like with the byte.
Is this anything to do with the MinValue / MaxValue properties?

Could someone please suggest some ideas...?

Thanks
 
Thanks for that!

I did forget to ask, though - How do I make a struct be a certain type?
Giving the byte as an example again, its value is whatever you put in it, but with mine it is 'blah-Struct'
 
Not entirely sure what you mean by make a struct be a certain type, if you wanted to cast to a different type then you could simply overload the conversion operator to work with different types.
 
What I mean is that if I say "int i = 3", then the value 3 is assigned to the int, which is a struct. I want my own struct to have a value, so if I can assign a value to it like that. If Microsoft can do it, I want to do it! Bah!

Seriously, though, I just need to know where to look. I tried Googling 'System.Int' but didn't see anything like what I want to do.

By the way, thanks again for that first link - Loads of info!
 
If you want to assign a value that is another type then the cast operator linked to above should do the trick.

If you want to be able to define a variable of your structure and assign a value to it then you would need to create a constructor for it. If you search these forums there should be a few examples of how to create and use a constructor.
 
Back
Top