Results 1 to 8 of 8

Thread: overloading of functions in vb.net

  1. #1
    pavansagar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Jan 2006
    Posts
    8
    Reputation
    0

    Post overloading of functions in vb.net

    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 AsString = "default"
    PublicFunction assign(ByVal x As Int16, ByVal y AsString)
    num = x
    name = y
    Console.WriteLine("with 2 parameters")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction
    PublicFunction assign(ByVal x As Int16)
    num = x
    name = "default"
    Console.WriteLine("with 1 int parameter")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction
    PublicFunction assign(ByVal y AsString)
    num = 0
    name = y
    Console.WriteLine("with 1 string parameter")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction
    PublicFunction assign()
    num = 0
    name = "default"
    Console.WriteLine("with 0 parameters")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction
    End
    Class

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




  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    976
    to overload functions you add the word "Overloads" as you declare it:
    Code:
    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
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    pavansagar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Jan 2006
    Posts
    8
    Reputation
    0

    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
    OverloadsFunction assign(ByVal x As Int16)
    num = x
    name = "default"
    Console.WriteLine("with 1 int parameter")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction
    PublicOverloadsFunction assign(ByVal y AsString)
    num = 0
    name = y
    Console.WriteLine("with 1 string parameter")
    Console.WriteLine("num=" & num)
    Console.WriteLine("name=" & name)
    EndFunction


  4. #4
    vis781's Avatar
    vis781 is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2005
    Location
    Cambridge, UK
    Posts
    2,015
    Reputation
    266
    can you post what the message is when the exception is thrown.

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,924
    Reputation
    1624
    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.

  6. #6
    vis781's Avatar
    vis781 is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2005
    Location
    Cambridge, UK
    Posts
    2,015
    Reputation
    266
    Nice one Jmcilhinney.

  7. #7
    aniskhan is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Oct 2005
    Location
    D.I.Khan
    Posts
    90
    Reputation
    112
    the error
    Code:
    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
    Code:
    obj1.assign(1)
    'INTO
    obj1.assign(CShort(1))

  8. #8
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,924
    Reputation
    1624
    Quote Originally Posted by aniskhan
    the error
    Code:
    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
    Code:
    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:
    Code:
     obj1.assign(1S)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking