Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Operator Overloading in VB.Net

  Asked By: Darcy    Date: Aug 22    Category: Asp.net    Views: 1607

Does VB support operator overloading? If so, can someone point me
toward some good info on how to do this?

I'm trying to create a new type for my application so I can
distiguish between different things that get passed into an
overloaded function/method.

I want to be able to pass first name or last name into an overloaded
function but since both are strings the I cannot tell which one was
passed. Here is what I would like the code to look like.

Sub s(a As FName)
...Do something with the firstname...
End Sub

Sub s(a As LName)
...Do something with the lastname...
End Sub

Dim Var1 as FName
Dim Var2 as LName
Var1 = "Matt"
Var2 = "Smith"

This should invoke the first instance of s (the FName version) and
pass it the value "Matt"

How do I define the type FName and then be able to use the "="
operator to assign a value to it?



7 Answers Found

Answer #1    Answered By: Edfu Massri     Answered On: Aug 22

Okay, so I now know (after some searching) that VB does not support
operator overloading. Does anyone have suggestions for solving the
problem in my original post? Basically, I need to create  a new data
type that I can assign  a value to directly.

Answer #2    Answered By: Samuel Costa     Answered On: Aug 22

VB.NET does support overloading  dosnt it?

Can you give an example  of the code  you are trying to write?

Answer #3    Answered By: Dirck Jansen     Answered On: Aug 22

Reference the MSDN under Optional arguments and overloading  for VB.NET (Jan2003)

Here is optional link for Jan2003 release of MSDN if it helps

I'm no VB expert and there is too much info  in MSDN to quote here. Check it out.

Answer #4    Answered By: Calais Bernard     Answered On: Aug 22

What you are trying to do is not really for overloading  (Which can be done
with VB.NET):

Overloading is for passing different types of parameters. what I think you
want to do is set the parameters via private variables within the class....


Class yourClassName

Private m_first_name as string
Private m_last_name as string

Public Property first_name() As String
Return m_first_name
End Get
Set (ByVal Value As String0
m_first_name = Value
' or do whatever you want here...
End Set
End Property

End Class

This gives you full control what to do when someone calls
yourClassName.first_name = 'whatever'.

Answer #5    Answered By: Calvin Banks     Answered On: Aug 22

I understand that normal function/subroutine overloading  can be done
in VB.Net, but you cannot overload operators like you can in C++.
What I really want to do is redefine the string data type  so I can
differentiate between two types of strings  in an overloaded function
(please refer to the example  in my original post).

I want to be able to create  a new variable type called FName and
then use the assignment operator  (=) to assign  a string to it.

Dim A as FName
Dim B as LName

A = "Matt"
B = "Smith"

I do not, however, want to use a property/method of a class to do
this. I tried using the default attibute on a property but it said
I needed a parameter to be a default property.

Answer #6    Answered By: Ralph Murray     Answered On: Aug 22

The only way of doing this to create  a new class calls FNAME that inherits
from string, call this class FName and then setup the property functions
like in my previous post. From a design point of view you are better making
this class the actual person rather than individual elements such as surname
which should be properties of that class.

Answer #7    Answered By: Keith Marshall     Answered On: Aug 22

After checking out C# to see if it could help me with this problem
I've discovered that you cannot overload the assignment operator  in
C#. Guess I will have to use your property idea. BTW, I tried to
inherit the String class and it said that it was not inheritable.

FYI, the FName LName example  is much more simple than my actual
problem. I just wanted to know if my idea was even possible. I
guess not.

Didn't find what you were looking for? Find more on Operator Overloading in VB.Net Or get search suggestion and latest updates.