-
Jul 9th, 2013, 02:04 PM
#1
Thread Starter
New Member
VB6 - Convert Variant to Node
I am using a For Each loop to go through a Variant array in VB6. At one point, I want to convert the element of the loop (elem), which is a Variant, to a Node.
Code:
Dim elem as Variant
For Each elem In ndArray
Dim nodle As Node
nodle = CType(elem, Node)
Next
That's not the whole loop, but it gives you an idea of what I'm trying to do. When I run this code, I get an error saying "Variable not defined", which points to the 'Node' in the CType method. This is not a variable, it is a type and the method should know that since it is expecting a type.
Anyone know what's going on here? Is this conversion even possible?
Any suggestions would be greatly appreciated.
-
Jul 9th, 2013, 02:11 PM
#2
Re: VB6 - Convert Variant to Node
Welcome to VBForums
For VB6 (which doesn't have Option Strict), you should be able to replace the CType line with just this:
-
Jul 9th, 2013, 02:21 PM
#3
Thread Starter
New Member
Re: VB6 - Convert Variant to Node
When I try to do something like this, it doesn't work:
HTML Code:
Dim nod As Node
nod = ndArray(0)
So here I'm skipping the CType method and trying to put a Variant into a Node variable. The error I get now says "Object variable or with block variable not set". I know that ndArray(0) contains a valid node value.
Was this what you were suggesting?
-
Jul 9th, 2013, 02:28 PM
#4
Re: VB6 - Convert Variant to Node
Ooops... I actually mis-typed (probably due to a copy+paste failure), what I meant was this:
...but you will probably need it like this:
-
Jul 9th, 2013, 02:29 PM
#5
Thread Starter
New Member
Re: VB6 - Convert Variant to Node
I tried doing this:
Code:
Dim nod As Node
nod = ndArray(0)
So I skipped the CType method and tried putting a Variant into a Node variable. Now it's giving me this error: "Object variable or With block variable not defined". I know there is a valid Node value in ndArray(0).
Was this what you were suggesting?
-
Jul 9th, 2013, 02:46 PM
#6
Re: VB6 - Convert Variant to Node
Try adding Set at the start of the last line.
-
Jul 9th, 2013, 02:50 PM
#7
Thread Starter
New Member
Re: VB6 - Convert Variant to Node
I tried adding the 'Set':
Code:
Dim elem As Variant
For Each elem In ndArray
Dim nodle As Node
Set nodle = elem
Next
Now the error says "Object required"
Once again, I looked at the elem variable during debug and it contains a valid Node element.
Last edited by fstephane; Jul 9th, 2013 at 02:57 PM.
-
Jul 9th, 2013, 06:26 PM
#8
Re: VB6 - Convert Variant to Node
Please show us the lines (not all, but some - at least all those which differ from each other) of of your debug-output, when you run this:
For Each elem In ndArray
Debug.Print TypeName(elem)
Next
-
Jul 9th, 2013, 06:44 PM
#9
Thread Starter
New Member
Re: VB6 - Convert Variant to Node
It just says empty. Aren't For Each elements always Variant?
-
Jul 9th, 2013, 07:59 PM
#10
Re: VB6 - Convert Variant to Node
Originally Posted by fstephane
It just says empty. Aren't For Each elements always Variant?
Yeah - sure ... IEnumVariant::Next returns a Variant and choosing a VB-Variant as the Loop-Variable is a decent and fast way to go about For-Next-stuff,
especially when one is not yet sure, what "final casts" will be applied to the LoopVar.
But when it just says "Empty" - then ndArray obviously just contains empty variants (an empty Variant is a Variant, which has all 16 Bytes, all 128Bits at Zero).
Code:
Dim LoopVar As Variant, VArr(0 To 2) As Variant
'since nobody filled anything into the 3 Index-Slots of VArr, the
'SafeArray-DataAllocation (all of the 3*16Bytes) remain still initialized to Zero
For Each LoopVar In VArr
Debug.Print TypeName(LoopVar)
Next
So in your shoes, I'd start investigating, what happened to your ndArray "along the way", since in this place
and "at this time" (the time of the enumeration) I cannot really imagine, that your debugger will tell you something,
which is not identically reflected also in the Debug.Print TypeName(...) call.
Olaf
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|