Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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'

Posted

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!

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...