0

I have a method that I want to serve as dual purpose, depending on whether the argument being passed is actually an integer or string value.

How would i check if 'info' in the following is an integer or string value?

public void Place (object info){


}

I know about overloading, but for reasons I won't go into can't change the calling method. Type 'object' is always being passed. Kind of kludgy but going to test for integer and string cast values.

3
  • 3
    What about overloading the method? Commented Dec 30, 2017 at 20:59
  • 1
    Integer.TryParse: msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx . Although, pedantically, a false result just proves it's not an int32, not that it is unparseable as any other kind of number. Anything can be a string. Do you mean it must be only interpretable as a string, or merely not interpretable as an int? P.S. Overloading as suggested by Plutonix is arguably a cleaner and clearer approach. Commented Dec 30, 2017 at 20:59
  • 2
    if(info is int) the is keyword is made for this. Commented Dec 30, 2017 at 21:01

3 Answers 3

4

Don't use object as the parameter, a better option would be to overload the method.

public void Place (string info){ ... }

public void Place (int info){ ... }

if you can't change the method signature for some reason then you'll need to do:

public void Place(object info)
{
       if(info is int){
           int result = (int)info;
           ...                   
       }
       else if (info is string){
           string result = (string)info;
           ...
       }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, I know about overloading, but for reasons I won't go into can't change the calling method. Type 'object' is always being passed. Kind of kludgy but going to test for integer and string cast values.
@eco_bach you should have put that into question from the beginning. I did that for you... please try to put all information you have into questions in the future.
4

C# 7.0 introduced pattern matching. This enables matches based on a specific class or structure. Since you can't change the signature of the method, this is the best option at the moment.

Code becomes simpler using the is expression to assign a variable if the test succeeds:

public void Place( object info )
{
    if ( info is string infoString )
    {
        // use infoString here
    }
    else if ( info is int infoInt )
    {
        // use infoInt here                
    }
}

If statements are limited to test the input to match a specific single type. If you want to test specific cases, the switch pattern matching becomes a better choice:

public void Place( object info )
{
    switch ( info )
    {
        // handle a specific case
        case string infoString when infoString.Contains( "someInfo" ):
            // do something
            break;

        case string infoString:
            // do something
            break;

        case int infoInt when infoInt > 10:
            // do something
            break;

        case int infoInt:
            // do something
            break;

        default: 
        // handle any other case
        break; 
    }
}

Comments

1

Or, make it a generic method like

public void Place<T> (T info)
{   
   // your code here
}

You can call it closing the type like

Place<int>(23) Or just Place(23)

2 Comments

Turned out OP can't change signature... Also I don't think it helps much in providing different versions of the code for Place method depending on type but rather just making type easier to obtain - still need if of some sort to check type.
@AlexeiLevenkov. that's strange ... signature can't be changed but the code can be. Well in that case as well you can use this as wrapper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.