0

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.

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.

I tried skipping the CType method and just making nodle = elem, but I got an error saying "Object variable or With block variable not defined". I added the Set keyword in front of the expression and the error changed to "Object required"

When I debug and look at the elem variable, it appears to contain a valid Node value.

Anyone know what's going on here? Is this conversion even possible?

Any suggestions would be greatly appreciated.

user2437443
  • 2,067
  • 4
  • 23
  • 38
  • There is no "CType method." You can't just make stuff up and expect results. Or is this a function that you wrote and failed to tell us about? – Bob77 Jul 10 '13 at 16:50
  • http://msdn.microsoft.com/en-us/library/vstudio/4x2877xb.aspx – user2437443 Jul 10 '13 at 20:12
  • In case you haven't noticed, your question is about VB6 but that link is for the confusingly-named VB.Net. It doesn't apply to actual Visual Basic at all. – Bob77 Jul 10 '13 at 21:14

1 Answers1

0

Try adding a Set?

Set nodle = CType(elem , Node) 

Set is necessary if Node is an object type and nodle will contain an object reference. If you omit Set, the compiler assumes that you want to change the default property of a Node object.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • I changed my code to avoid having to convert elem at all. I no longer have the old code, so I can't test out your suggestion, sorry. – user2437443 Jul 10 '13 at 21:00