Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:24+00:00 2026-05-26T04:49:24+00:00

Okay, so I realised after writing some overloaded methods that webmethods can’t be overloaded

  • 0

Okay, so I realised after writing some overloaded methods that webmethods can’t be overloaded (by realised I mean VS threw a paddy and wouldn’t allow me to update the service references). I have tried to get around that like this:

    public string DoPing<T>(T IP)
    {

        if (typeof(T) == typeof(string))
        { 
            return DoPingString(IP);
        }
        if (typeof(T) == typeof(IPAddress))
        {
            return DoPingIP(IP);
        }
        throw new Exception("Programmer Error");
    }

But I’m getting a cannot convert from T to string/Ipadress error when I call the respective (renamed) methods.
Can someone explain why it doesn’t work, and possibly either fix it or give me an alternate solution?
Thanks in advance.

Edit: Good point, generics are probably overkill (I tried for another solution and failed before trying this).
DoPingString(string String) and DoPingIP(IPAdress Address) are the signatures.
I will give the tick to the closest answer tomorrow. I solved the problem in a separate way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:49:25+00:00Added an answer on May 26, 2026 at 4:49 am

    You’re not using generics the way it was intended. The compiler has no idea what type T is, so it won’t just let you implicitly or explicitly cast to String or IPAddress.
    Here’s a version that works:

        public string DoPing<T>(T IP)
        {
            if (typeof(T) == typeof(string))
            {
                return DoPingString((String)Convert.ChangeType(IP, typeof(String)));
            }
            if (typeof(T) == typeof(IPAddress))
            {
                return DoPingIP((IPAddress)Convert.ChangeType(IP, typeof(IPAddress)));
            }
            throw new Exception("Programmer Error");
        }
    

    However, since you’re using generics with no constraint, you gain no benefit from the generic implementation. Why not just take an object?

    public string DoPing(Object IP)
    {
        if (IP.GetType() == typeof(string))
        {
            return DoPingString((String)IP);
        }
        if (IP.GetType() == typeof(IPAddress))
        {
            return DoPingIP((IPAddress)IP);
        }
        throw new Exception("Programmer Error");
    }
    

    Of course, the simpler and easier approach would be to simply overload your function, i.e.:

    public string DoPing(string IP)
    {
        return DoPingString(IP);
    }
    public string DoPing(IPAddress IP)
    {
        return DoPingIP(IP);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so im working on this php image upload system but for some reason
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay, we know that the following two lines are equivalent - (0 == i)
Okay, I have setup up a handler for a div click but for some
Okay I do apologise that I'm out of my depth here. I'm not the
Okay, I've realized that I really have asked way too many questions without contributing
Consider a Wicket WebPage that redirects to another page (based on some logic omitted
I try to create an area onto a CDialog, where I can put some
I'm writing an application (on android) that uses both java code, and native code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.