rculver9056 Posted April 3, 2010 Posted April 3, 2010 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 Quote
Administrators PlausiblyDamp Posted April 3, 2010 Administrators Posted April 3, 2010 In C# you can do this by creating conversion operators - http://msdn.microsoft.com/en-us/library/xhbhezf4(v=VS.71).aspx gives a quick starting point on how to convert from a byte to a custom type - should be enough to get you started. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rculver9056 Posted April 3, 2010 Author Posted April 3, 2010 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' Quote
Administrators PlausiblyDamp Posted April 4, 2010 Administrators Posted April 4, 2010 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rculver9056 Posted April 4, 2010 Author Posted April 4, 2010 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! Quote
Administrators PlausiblyDamp Posted April 4, 2010 Administrators Posted April 4, 2010 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.