Question:
variant data type in Visual Basic?
nb
2009-07-21 01:42:16 UTC
Explain the importance of variant data type in Visual Basic
Five answers:
MarkG
2009-07-21 10:29:05 UTC
THIS ONLY APPLIES TO VB6 !!!! Variants aren't supported in DotNET.



Well there certainly alot of disadvantages to variants especially if misused. There are some advantages to using variants in a limited fashion. Like most thing there are many different ways to do things and I am not suggesting that these are the only way. I am just trying to point out some use variants have and there unique capabilities.



You also have to ask your self why Microsoft developers spent so much time and effort to develop variant data types for VB6. So at the time there was thought that they were going to be useful. (At least until DotNet came around)



The basic advantage to variants is that they can accept any type of data. This is useful especially if you do not know the type of data you will be dealing with until runtime or you expect the type of data to change. (Collection use vaiants for example)



==================================================



An undefined variant is assigned a special value "Empty" Empty evaulates to a numeric zero or an empty string("") if its unassigned.

Empty can be useful in error checking and detecting unassigned variables.



A variant can also hold an object reference.

This can be handy if you write a (single) sub or function that uses a variant to pass different control types to process.(I suppose you can write an overloaded function instead to handle each specific control type)



Finally a variant can be Null, which can be handy in preprocessing string and serial data inputs.



If IsNull(Name) Then

MsgBox "The name is not valid"

Else

MsgBox "The name is " & Name

End If
?
2009-07-21 01:51:33 UTC
Firstly never ever use a variant data type, its big slow and really bad.

Basically a variant allows all types of data, be they integers, doubles or strings.

They are a "fail safe" sort of variable type as they will generally never through an error for being the wrong type (as they are all generic types.)



However because of this they take up more room (around 12+ bytes per variable, more if they hold a string) and are slow VB will need to determine what type of data they are holding.
?
2016-04-08 18:07:17 UTC
Using Variant vs a strict data type removes the ability of the IDE to detect code errors as you key in your code. Example: Dim myInt As Variant myInt = "A" This is valid, however if you try to do any math operations on myInt while it contains "A" then you'll get a run time error. If you define it like this: Dim myInt As Integer myInt = "A" The IDE will flag the error right then and let you know you tried to assign an invalid type to the variable.
2016-10-30 12:28:11 UTC
Vb6 Variant
Chie
2009-07-22 20:15:42 UTC
Variant is an old type in VB targeted for beginner programmers; it should generally be avoided.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...