overloading of functions in vb.net

pavansagar

Member
Joined
Jan 11, 2006
Messages
8
Programming Experience
Beginner
I have a problem with the following code.please clarify.In the 5th line from statement I am getting the problem.I don't want to change the code if it is not wrong with this.

Public
Class first
Public num As Int16 = 0
Public name As String = "default"
Public Function assign(ByVal x As Int16, ByVal y As String)
num = x
name = y
Console.WriteLine("with 2 parameters")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function
Public Function assign(ByVal x As Int16)
num = x
name = "default"
Console.WriteLine("with 1 int parameter")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function
Public Function assign(ByVal y As String)
num = 0
name = y
Console.WriteLine("with 1 string parameter")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function
Public Function assign()
num = 0
name = "default"
Console.WriteLine("with 0 parameters")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function
End
Class

Module
Module1
Sub Main()
Dim obj1 As New first
obj1.assign(2, "abc")
obj1.assign("bdsfd")
obj1.assign(1) 'here I am getting the error in this line
obj1.assign()
Console.ReadLine()
End Sub
End
Module



 
to overload functions you add the word "Overloads" as you declare it:
VB.NET:
Public Overloads Function Assign(Byval x As Integer) As Integer
End Function

Public Overloads Function Assign(Byval x As Single) As Integer
End Function

both have the same name (also both have to return the same type) but one takes an integer arguement and the 2nd takes a single

that maybe the problem you're having, although you should be getting an error for all but the 1st one
 
thanks

thank you sir ,for your reply. here you said that I mentioned the return types as integer for both the functions.But I have not mentioned anything. That means is it by default takes the return type as integer?
Also I have not mentioned 'single' as the type of the argument,but I mentioned it as 'string'.Even though I add the overloads keyword I am not getting the answer.Please help me if you can.

Public
Overloads Function assign(ByVal x As Int16)
num = x
name = "default"
Console.WriteLine("with 1 int parameter")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function
Public Overloads Function assign(ByVal y As String)
num = 0
name = y
Console.WriteLine("with 1 string parameter")
Console.WriteLine("num=" & num)
Console.WriteLine("name=" & name)
End Function

 
You can choose to use the "Overloads" keyword on overloaded functions if you want but you don't have to. Good practice dictates that you should, but your code will compile without it. Having said that, you must use the "Overloads" keyword on all or none of the overloaded methods. You cannot use it on some and not others.

First off, why would you tell us that you get an error and not tell us what the error is. That's like going to the doctor and telling him your sick but not what's actually wrong, then expecting a diagnosis. ALWAYS tell us what the error message is.

In this case it doesn't matter though. I can tell you that the problem is the fact that none of your overloads take the type of arguments that you are trying to pass. You are passing an Int32 object but none of your overloads take an Int32 argument. Your Int32 object would have to be implicitly converted to either an Int16 or a String. As there is no clear indication of which to choose the compiler says "I give up". You'll find that if you remove either of those overloads the other will be used by default and your error will disappear. I have to say though, allowing implicit conversions is lazy, bad programming. The fact that VB allows, and even encourages it, is one of the primary reasons that developers of other languages look down on VB. VB.NET allows you to disallow implicit conversions by setting Option Strict On, which every VB.NET programmer should do.

Is there a specific reason you are using the Int16 type? If you think that they are more efficient than Int32 you are wrong. They may use half the memory but on a 32-bit system there is transalation required to use 16-bit values, so what you gain on the swings you lose on the roundabouts. It is best to use the Integer or Int32 types, which are the same thing, unless you have a very specific reason for using Int16.
 
the error
VB.NET:
Overload resolution failed because no accessible 'assign' can be called without a narrowing conversion:
    'Public Function assign(y As String) As Object': Argument matching parameter 'y' narrows from 'Integer' to 'String'.
    'Public Function assign(x As Short) As Object': Argument matching parameter 'x' narrows from 'Integer' to 'Short'.
change the line
VB.NET:
obj1.assign(1)
'INTO
obj1.assign(CShort(1))
 
aniskhan said:
the error
VB.NET:
Overload resolution failed because no accessible 'assign' can be called without a narrowing conversion:
    'Public Function assign(y As String) As Object': Argument matching parameter 'y' narrows from 'Integer' to 'String'.
    'Public Function assign(x As Short) As Object': Argument matching parameter 'x' narrows from 'Integer' to 'Short'.
change the line
VB.NET:
obj1.assign(1)
'INTO
obj1.assign(CShort(1))
Don't cast an Integer literal as a Short. Make it a Short literal in the first place:
VB.NET:
 obj1.assign(1S)
 
Back
Top