🔒 Locked
Variable trouble (VB)
Advertisement
Original Post
Is there any way to declare a variable as both a Variant and a user defined type! if not, is there a way i could get around it? I appreciate any help...
o_O
First off, if you''re using Variants then you''re doing something wrong.
You can''t declare a variable as more than one type. However, a variant CAN hold any type. So you can put that UDT into a variant. You can also put a variant into the UDT, but if the variant doesn''t hold your UDT, you''ll encounter a runtime error.
____________________________________________________________
Direct3D vs. OpenGL
First off, if you''re using Variants then you''re doing something wrong.
You can''t declare a variable as more than one type. However, a variant CAN hold any type. So you can put that UDT into a variant. You can also put a variant into the UDT, but if the variant doesn''t hold your UDT, you''ll encounter a runtime error.
____________________________________________________________
Direct3D vs. OpenGL
What''s wrong with Variants? Anyway, I need to declare an array with a user defined number of blocks, and I don''t see any other way to do it besides with ReDim...any ideas?
What exactly are you asking?... Variant doesn''t mean variant size, it means variant type .
If you want to resize an array (presumably of you UDT?), that is exactly what ReDim is for. If you wish to keep the current contents intact, use ReDim Preserve.
Variants should never be used where there is the option to use any other "concrete" type. This is because they are fat (60 bytes min, IIRC), and slow (because they must be coerced to and from other types when used in operations). They can also be unsafe and error-prone, as VB may make some scary assumptions if you don''t specify what type to coerce them to.
If you want to resize an array (presumably of you UDT?), that is exactly what ReDim is for. If you wish to keep the current contents intact, use ReDim Preserve.
Variants should never be used where there is the option to use any other "concrete" type. This is because they are fat (60 bytes min, IIRC), and slow (because they must be coerced to and from other types when used in operations). They can also be unsafe and error-prone, as VB may make some scary assumptions if you don''t specify what type to coerce them to.
Variants: These are shunned because they are very slow in comparison to other data types. They also don''t have much of an equivalent to other data types in other languages, which makes it annoying for others to read.
The solution: For your problem, I would probably make a udt something like this:
Type udtMyType
TypeInUse As Long
TheUDT As udt ''0
ALong As Long ''1
ADouble As Long ''2
AString As Long ''3
End Type
The TypeInUse element tells you what part of the variable to use. ALong, ADouble and AString are a replacement for the variant (you probably won''t need all 3, so get rid of one of them if necessary). It will be a little bit of extra code, but it will be faster.
Promit:
This page might be of help, basically, it includes various things to replace the default (slow) VB functions:
http://www.vb2themax.com/SearchKey.asp?PageID=Optimize
in particular:
http://www.vb2themax.com/ItemBank.asp?PageID=CodeBank&Cat=120#CAT
there is a VB5 version of split (a VB6 only function). It will probably be faster (even though I''ve never tested it). Actually, I think there might be a webpage with lots of different versions of Split written for speed. I didn''t google very much, so you might want to have a look for yourself. Search for VB string optimization and hopefully you will find something.
Trying is the first step towards failure.
The solution: For your problem, I would probably make a udt something like this:
Type udtMyType
TypeInUse As Long
TheUDT As udt ''0
ALong As Long ''1
ADouble As Long ''2
AString As Long ''3
End Type
The TypeInUse element tells you what part of the variable to use. ALong, ADouble and AString are a replacement for the variant (you probably won''t need all 3, so get rid of one of them if necessary). It will be a little bit of extra code, but it will be faster.
Promit:
This page might be of help, basically, it includes various things to replace the default (slow) VB functions:
http://www.vb2themax.com/SearchKey.asp?PageID=Optimize
in particular:
http://www.vb2themax.com/ItemBank.asp?PageID=CodeBank&Cat=120#CAT
there is a VB5 version of split (a VB6 only function). It will probably be faster (even though I''ve never tested it). Actually, I think there might be a webpage with lots of different versions of Split written for speed. I didn''t google very much, so you might want to have a look for yourself. Search for VB string optimization and hopefully you will find something.
Trying is the first step towards failure.
Psyberia... an array is type-safe (ie can only contain elements of one type), and a collection is not. If you are going to use a collection, you should write a wrapper class around it which makes it type-specific. However, this is major overkill if all you wanted in the first place was a dynamic array of integers 
[edited by - Bad Monkey on June 20, 2002 3:11:11 AM]
[edited by - Bad Monkey on June 20, 2002 3:11:11 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement