PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Help returning NULL objects from VB6 Property-VBForums
Results 1 to 4 of 4

Thread: Help returning NULL objects from VB6 Property

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Question Help returning NULL objects from VB6 Property

    Ok I lived VB6 long ago, but am making an enhancment to a legacy VB6 app. The problem is I don't have the syntax quite right for returning objects from a class property that have no value.

    In .NET this is trival, as returning the backing variable value and then having the caller check to see if it 'Is Nothing' works. In VB6 there (obviously) is no Return statement on Properties and the actual Return is assignment onto itself and I think this is where I am running into issues. The error I get is the famous "Object variable or With block varibale not set". So let me give the code example. This property referencing 'MyClass2' is within another class named MyClass1.

    Property Example Code:
    1. Private _mc2 As New MyClass2
    2.  
    3. Public Property Get mc2() A MyClass2
    4.    'This is where it bombs with the error mentioned above when checking if
    5.    'the value Is Nothing from the calling code
    6.    mc2 = _mc2
    7. End Property
    8.  
    9. Public Property Let mc2(ByVal Value As MyClass1)
    10.    _mc2 = Value
    11. End Property

    Here would be an example calling code that causes the above to error:

    'Say an intance of MyClass1 which contains the instance property. Assuming the mc2 Property has NO value (Is Nothing = True), I am going to test it like below:

    Example calling code Code:
    1. Dim mc1 As New MyClass1
    2. If mc1.mc2 Is Nothing Then
    3.  
    4. End If

    Any ideas on how I can test for a Property on a class that is actually an instance of a property, and check if it Is Nothing without getting the error?

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,847

    Re: Help returning NULL objects from VB6 Property

    if isnull(var) then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Talking Re: Help returning NULL objects from VB6 Property

    Nevermind I figured this out.... I always forget to use the 'SET' statement when assigning objects in VB6. So the property needed to look like this:

    Working Code Code:
    1. Public Property Get mc2() A MyClass2
    2.    'Works
    3.    Set mc2 = _mc2
    4. End Property


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

    Re: Help returning NULL objects from VB6 Property

    As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

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
  •  



Featured


Click Here to Expand Forum to Full Width