Sign in to follow this  

what the hell is variant?

This topic is 4815 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 really wanna know what is variant.I know that it is a type that contains everything.but why do we want to use it?where can I find more information about it?Thanks.

Share this post


Link to post
Share on other sites
[google] - variant + "data type"
from msdn home
Quote:
Variant data type
A special data type that can contain numeric, string, or date data as well as user-defined types and the special values Empty and Null. The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length), and can store any character text. The VarType function defines how the data in a Variant is treated. All variables become Variant data types if not explicitly declared as some other data type.

Share this post


Link to post
Share on other sites
Quote:
Original post by jimywang
but why do we want to use it?


It goes in hand with implicit variable definition (using the variable without declaring it beforehand). It's oftentimes easier to use a variant rather than messing with choosing the right data type for the job.. especially for new programmers. It also speeds up writing code and design. I don't use them in my VB code because they're more costly and obfuscate the meaning of certain parts of code (especially if I haven't seen it in awhile and didn't comment much).

Share this post


Link to post
Share on other sites
Quote:
Original post by evolutional
It's VB's way of allowing a typeless object, one that can be assigned any value, string or object. You can create psuedo variant types by using union under C/C++ if you really need to.


another alternative boost::variant.

Share this post


Link to post
Share on other sites
Or boost::any if you really don't know what you might have (boost::variant requires you to specify all possible types in advance). Useful in a message system, if you want to attach some data to a message and don't want to restrict the types used.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are void pointers considered variants? Couldn't you use it to pull void data from a buffer and read the first chunk of it to determine the type before creating the correct object for it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Are void pointers considered variants? Couldn't you use it to pull void data from a buffer and read the first chunk of it to determine the type before creating the correct object for it?

You could get a standard that defined that a block of memory would have an identifier that lets you know what type the memory represents. Then again, you'd be redefining what a variant already does :)

Share this post


Link to post
Share on other sites

This topic is 4815 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this