Results 1 to 4 of 4

Thread: I have a function in VB6 where I am trying to return a string from the function

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    4

    Post I have a function in VB6 where I am trying to return a string from the function

    So here is a code snippet. The error I am getting is "Function call on left side of assignment must return Variant or Object". I changed the function return to a variant data type, but that did not help.

    I am trying to generate a random string using function calls. Because it is a large number of sub tables I want to maintain the tables separated like so instead of in a single long series of code for ease of maintainability.

    If this is not viable, can anyone suggest an alternative method of doing this?

    Private Function GenAstStrategicResouce() As Variant

    Dim X As Integer

    X = Int((200 * Rnd) + 1)

    If X < 10 Then
    GenStrategicResouce = "Bose-Einstein Condensates"
    ElseIf X < 20 Then
    GenStrategicResouce = "Diamonds"
    End If

    End Function

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,041

    Re: I have a function in VB6 where I am trying to return a string from the function

    Code:
    Private Function GenAstStrategicResouce() As Variant
    
    Dim X As Integer
    
    X = Int((200 * Rnd) + 1)
    
    If X < 10 Then
    GenStrategicResouce = "Bose-Einstein Condensates"
    ElseIf X < 20 Then
    GenStrategicResouce = "Diamonds"
    End If
    
    End Function
    See bolded. Your names are mismatched, the variable name needs to match the function name.

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,178

    Re: I have a function in VB6 where I am trying to return a string from the function

    Also if you want to return a string you should probably change Variant to String on that first line.

    A side from that is the intention not to return anything 90% of the time? If not then you may want to look at the logic a bit.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,260

    Re: I have a function in VB6 where I am trying to return a string from the function

    Yes, just to follow-up on Data's comment, if X >= 20 then you'll return an Empty type Variant. And it's not at all clear that that's what you'd want. If the function were declared as a String, then you'd return a zero-length-string. Or, another alternative is to add an Else with a GenAstStrategicResouce = vbNullString statement in it.

    And, as Option stated, fix your name-mis-matches.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

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