Page 1 of 1

Virtual functions in VB?

Posted: Thu Jul 03, 2003 1:27 pm
by Proutprout
Hi

I do believe its wishfull thinking, but is it possible to make a virtual function in VB just like in C++?

I want my function to act differently depending on the type of parameter that I give her.

simplified exemple:

Public sub Bitch(strBitch as string) as string
  Bitch = strBitch + " yo!"
End sub


Public sub Bitch(intBitch as integer) as integer
  Bitch = intBitch*2
End sub

Private Sub Form_Load()

  dim strYo as string
  dim intNo as integer

  strYo = "Bitch!"
  intNo = 2

  msgbox Bitch(strYo) 'Would display "Bitch! Yo!"
  msgbox Bitch(intNo) 'Would display 4

End Sub




What is missing in this code, if what i want is actually possible? :roll:

Thank you!

Posted: Thu Jul 03, 2003 3:22 pm
by Buub
Try a Real Programming Language. :-)

If you're scripting, I'd recommend Python. If you're doing web-type development, Java is good stuff. If it's serious hard-core, just stick with C++.

Oh yeah there's C# (C-Sharp) too... (yawn)

Posted: Fri Jul 04, 2003 6:02 am
by Proutprout
;)

.... but the point is i need to modifiy an already existing VB program so I won't use any other language.

Thx anyway i guess ... hehe

Posted: Fri Jul 04, 2003 6:42 am
by lazytom
Might be possible to do it in VB.NET, no? I don't really know but I thought VB.NET was also completely object-oriented.

Posted: Fri Jul 04, 2003 7:28 am
by Ryszard
You can't do function overloading like that in VB, not within the same module anyway.

There's nothing stopping you having two Bitch functions in different modules and calling them appropriately, but you lose being able to pass in whatever data type you like without caring. You could pass in a variant I guess, and do your own type checking, but that's more problematic still.

VB.NET lets you do what you want however, but I'm guessing that's about as much use to you as a suggestion to use Python :P

Rys

Posted: Fri Jul 04, 2003 7:37 am
by Proutprout
hehe yep thats not much use to me either ;)

But still, i now know that it isn't possible. :cry:

So thx for the input! I'll try to do some type checking instead...

Posted: Fri Jul 04, 2003 5:35 pm
by IntelMole
Ryszard wrote:
There's nothing stopping you having two Bitch functions in different modules and calling them appropriately, but you lose being able to pass in whatever data type you like without caring. You could pass in a variant I guess, and do your own type checking, but that's more problematic still.


lol... I can see it now...

"Stupid bitch functions, <grumble grumble>"

Btw, don't believe the hype, C isn't just hard... it's damn near impossible :-D

If you need proof I've got some threads in this forum :-D, j/k
IntelMole

Posted: Tue Jul 08, 2003 10:10 pm
by Craig P.
Virtual functions are possible in VB. Use an interface and the "Implements" keyword.

(Aside - VB is built on COM, the entire foundation of which is virtual functions and a vtable ABI.)

However, what you've asked for is overloaded functions (a different thing), which VB does not support (nor C, for that matter). You can approximate the same thing by taking a Variant argument, though... the end result would be only slightly different, and would be resolved at runtime rather than at compile time.

e.g.
Public Function Bitch(ByRef arg As Variant) As Variant
    If IsNumeric(arg) Then
        Bitch = arg * 2
    ElseIf VarType(arg) = vtString Then 'Might not be the right constant name
        Bitch = arg & " yo!" 'Always use & to concatenate
    Else
        'Eek!
        Debug.Assert False
    End If
End Function