Skip to main content
gamedev.net
🔒 Locked

Variable trouble (VB)

Started by Shadowflare 9 replies 1.4k views
Original Post
Shadowflare
Shadowflare
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...
munkie
munkie
You should never be using variants anyway. I can''t begin to imagine why such a thing was ever incorporated.

What are you trying to do that you need a variant? They''re merel excuses for poorly written code!
Promit
Promit
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
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Shadowflare
Shadowflare
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?
Bad Monkey
Bad Monkey
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.
Promit
Promit
I''ve found it possible to avoid Variants in all but one occasion, which has really been bothering me, BTW...
Split() returns a variant array. What BS...variant arrays are rather strange.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
psyberia
psyberia
Why not just use a collection? You can add as many values as you need to it.
~"What''s this red button do?"
ragonastick
ragonastick
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.
Trying is the first step towards failure.
Bad Monkey
Bad Monkey
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]
einstein2
einstein2
[.....gone.....]

[Edited by - einstein2 on May 15, 2010 5:42:40 AM]
Promit
Promit
Collections are not type safe unless you write a wrapper, and are just smart arrays of variants anyway.

quote:

Type udtMyType
TypeInUse As Long

TheUDT As udt ''0

ALong As Long ''1
ADouble As Long ''2
AString As Long ''3
End Type


Is that supposed to be pseudocode?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.