Results 1 to 10 of 10

Thread: [RESOLVED] Incredibly dumb question, but...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Resolved [RESOLVED] Incredibly dumb question, but...

    If I declare a function as this:

    VB Code:
    1. Public Function GetRunDate()

    it returns a variant, right? Basically I'm wondering what the diff is between the above and the below:

    VB Code:
    1. Public Function GetRunDate() As String

    I told you it was stupid; I feel really dumb asking it.

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Incredibly dumb question, but...

    Correct. Here's how it looks:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print TypeName(test1())
    5.     Debug.Print TypeName(test2())
    6. End Sub
    7.  
    8. Public Function test1()
    9.     'you can leave this empty...
    10. End Function
    11.  
    12. Public Function test2() As String
    13.     '-||-
    14. End Function

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Incredibly dumb question, but...

    Quote Originally Posted by gavio
    Correct. Here's how it looks:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print TypeName(test1())
    5.     Debug.Print TypeName(test2())
    6. End Sub
    7.  
    8. Public Function test1()
    9.     'you can leave this empty...
    10. End Function
    11.  
    12. Public Function test2() As String
    13.     '-||-
    14. End Function

    So my first example returns a variant and the second returns a string, right? I'm just wondering what the implications are for memory management; my app isn't that big but I'd like it to be as efficient as possible. Should I bother changing my functions?

  4. #4
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: Incredibly dumb question, but...

    IMO its ethics that we tend to follow.If a function doesn't have to return anything then don't worry about the return type else i think it should.

    ps:
    Public Function test2() As String
    '-||-
    End Function

    Are you sure it will return a string ??? coz i think it wouldn't.
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  5. #5
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Incredibly dumb question, but...

    Variants are very slow and I believe the largest variable type you can use in VB6.

    I'm not sure how strings are handled as return types, as they're arrays

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Incredibly dumb question, but...

    Thanks for the answers guys.

  7. #7
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Incredibly dumb question, but...

    Quote Originally Posted by litlewiki
    Are you sure it will return a string ??? coz i think it wouldn't.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. 'any type but String or Variant will cause the "Type mismatch" error
    5.  
    6.     Dim someVar As Integer 'for example
    7.         someVar = test()
    8. End Sub
    9.  
    10. Public Function test() As String
    11.     '...
    12. End Function

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Incredibly dumb question, but...

    Quote Originally Posted by disruptivehair
    If I declare a function as this:

    VB Code:
    1. Public Function GetRunDate()

    it returns a variant, right? Basically I'm wondering what the diff is between the above and the below:

    VB Code:
    1. Public Function GetRunDate() As String

    I told you it was stupid; I feel really dumb asking it.
    2nd makes debugging easier since your sure the function returns a string.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Incredibly dumb question, but...

    Quote Originally Posted by leinad31
    2nd makes debugging easier since your sure the function returns a string.
    Cool. I have a function that returns an array of strings and if I declare it 'as string' it goes nuts, so I left it as a variant but I changed the others that I could find.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Incredibly dumb question, but...

    If your receiving the array with a dynamic array of corresponding data type then the only problem you would have is when an array that has not been dimmed is returned by the function... code in the calling procedure probably expected an array with elements.

    Rather than return just an array, you could receive an array ByRef instead in the arguments list... and the function would then return Success/Failed flags in addition to processing/populating/dimensioning/etc the passed array.

    VB Code:
    1. Dim passedArray() As String
    2.  
    3. If SampleProc(passedArray) Then
    4.    debug.print passedarray(0)
    5. Else
    6.    'don;t process undimmed array to avoid error
    7. End If
    8.  
    9. '-------
    10. Public Function SampleProc(ByRef receivedArray() As String) As Boolean
    11.    Redim receivedArray(1)
    12.    receivedArray(1) = "hello"
    13.    receivedArray(1) = "world"
    14.    SampleProc = True
    15. End Function
    Last edited by leinad31; Nov 6th, 2006 at 11:37 AM.

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