Virtual functions in VB?

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Virtual functions in VB?

Postposted on Thu Jul 03, 2003 1:27 pm

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:

Code: Select all
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!
Proutprout
Gerbil
 
Posts: 64
Joined: Tue Jun 17, 2003 9:24 am
Location: Canada

Postposted on Thu Jul 03, 2003 3:22 pm

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)
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4214
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Postposted on Fri Jul 04, 2003 6:02 am

;)

.... 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
Proutprout
Gerbil
 
Posts: 64
Joined: Tue Jun 17, 2003 9:24 am
Location: Canada

Postposted on Fri Jul 04, 2003 6:42 am

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.
lazytom
Gerbil
 
Posts: 25
Joined: Thu Dec 27, 2001 7:00 pm
Location: Mountain View, CA

Postposted on Fri Jul 04, 2003 7:28 am

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
Ryszard
Gerbil
Gold subscriber
 
 
Posts: 29
Joined: Sat Apr 27, 2002 10:15 pm

Postposted on Fri Jul 04, 2003 7:37 am

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...
Proutprout
Gerbil
 
Posts: 64
Joined: Tue Jun 17, 2003 9:24 am
Location: Canada

Postposted on Fri Jul 04, 2003 5:35 pm

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
Living proof of John Gabriel's theorem
IntelMole
Grand Gerbil Poohbah
 
Posts: 3529
Joined: Sat Dec 29, 2001 7:00 pm
Location: The nearest pub

Postposted on Tue Jul 08, 2003 10:10 pm

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.
Code: Select all
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
Craig P.
Gerbil Team Leader
 
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest