Results 1 to 10 of 10

Thread: VB6 - Convert Variant to Node

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    8

    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.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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:
    Code:
        nodle = Node

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    8

    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?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: VB6 - Convert Variant to Node

    Ooops... I actually mis-typed (probably due to a copy+paste failure), what I meant was this:
    Code:
        nodle = elem
    ...but you will probably need it like this:
    Code:
        Set nodle = elem

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    8

    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?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: VB6 - Convert Variant to Node

    Try adding Set at the start of the last line.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    8

    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.

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    8

    Re: VB6 - Convert Variant to Node

    It just says empty. Aren't For Each elements always Variant?

  10. #10
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 - Convert Variant to Node

    Quote Originally Posted by fstephane View Post
    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
  •  



Click Here to Expand Forum to Full Width