2
public static int getInfo(string info)
    {
        string inputValue;
        int infor;
        Console.WriteLine("Information of the employee: {0}", info);
        inputValue = Console.ReadLine();
        infor = int.Parse(inputValue);
        return infor;

    }

In the above code, how can I get the name(string) and salary(int) of a person? Specifications are that I need to call the method twice for the information retrieval.

4
  • So you want your method to return either a string or an int? Commented Apr 14, 2017 at 6:49
  • 2
    Use C#7 + tuples: blogs.msdn.microsoft.com/dotnet/2016/08/24/…. Commented Apr 14, 2017 at 6:55
  • 1
    This screams XY problem (If you don't know what that means, Google it; I don't have a link handy on mobile). What are you trying to achieve with this? Commented Apr 14, 2017 at 7:34
  • Thank you guys. I have done it with another way by explicitly converting string into in main method. Commented Apr 14, 2017 at 12:06

8 Answers 8

5

You can pass it separately or better if you create a class which holds of info and the salary

 public static int getInfo(string info,int salary)

or create a class,

 public class MyInfo
    {
        public int info { get; set; }
        public int salary { get; set; }

    }

and the method signature goes as,

 public static int getInfo(MyInfo info)

If you want to return both string and int , better to change the method signature as of type class MyInfo

 public static MyInfo getInfo(MyInfo info)
Sign up to request clarification or add additional context in comments.

6 Comments

I think the OP wants his method to return the information.
@PieterWitvoet ok edited as it is. 2 more to get gold in c# ! :)
@Sajeetharan 2 more for gold! Jealous! Good luck, you'll get it fast
I cant use the classes for this particular case as the specifications says that i need method to be called twice and get the name and the salary out of one method.
@PrashantBhandari what specificiation? who suggested?
|
4

You could make it return a tuple with both values like this:

internal Tuple<int, string> GetBoth(string info)
{
    string inputValue;
    int infor;
    Console.WriteLine("Information of the employee: {0}", info);
    inputValue = Console.ReadLine();
    infor = int.Parse(inputValue);
    return new Tuple<int, string>( infor, inputValue );
}

internal void MethodImCallingItFrom()
{
    var result = GetBoth( "something" );
    int theInt = result.Item1;
    string theString = result.Item2;
}

Comments

2

For returning multiple values from a method we can use Tuples or (DataType, DataType,..).

Tuples are readonly. As a result assigning values is possible only via constructor at the time of declaration.

Below example is using multiple data types as return type. Which is both read and write enabled.

    public (string, int) Method()
    {
        (string, int) employee;
        employee.Item1="test";
        employee.Item2=40;

        return employee;
    }

You can call the above method in your main code, as shown below:

    static void Main(string[] args)
    {
        
        (string name,int age) EmpDetails = new Program().Method();
        string empName= EmpDetails.name;
        int empAge = EmpDetails.age;

        //Not possible with Tuple<string,int>
        EmpDetails.name = "New Name";             
        EmpDetails.age =  41;
    }

However, the best practice is to use class with properties, as mentioned in above answers.

1 Comment

I agree using a class with properties is the best answer, but I would have loved this (and still do) about 20 years ago when I was just figuring this out... This answer is Sweeter than YooHoo!!!
2

Here is an example in .Net 7

The method:

public (int, string) Returning2()
{
    int a = 10;
    string s = "ahsan ullah";
    return (a, s);
}

Call it from another function:

int x = Returning2().Item1;
string ss= Returning2().Item2;

Console.WriteLine(x + " " + ss);

Item1 indicating int and Item2 indicating string

Comments

0
public static void Main(string[] args)
    {
        string eName, totalSales;
        double gPay, tots, fed, sec, ret, tdec,thome;
        instructions();
        eName = getInfo("Name");
        totalSales = getInfo("TotalSales");
        tots = double.Parse(totalSales);
        gPay = totalGpay(tots);

}

public static string getInfo(string info)
    {
        string inputValue;
        Console.WriteLine("Information of the employee: {0}", info);
        inputValue = Console.ReadLine();

        return inputValue;

    }

This is what was required. Could have done with other tricks mentioned by you guys. Anyway thank you.

Comments

-1

Why dont you just return a String array with a size of 2? at the first (array[0]) there is the string and at the second (array[0]) there is the int...

public static string[] GetInfo (string info)

I hope I understood your question right ^^

2 Comments

i could have done this using TryParse with if else, but the case is that I need to call the method twice. :(
u can also make 'string stringname + Convert.ToString(int intname);'
-1

you need to create a person class and read name and salary

public class Person
{
    public string Name {get; set;}
    public decimal Salary {get; set;}
}

and your function will be:

public static Person getInfo(string info)
{
    string inputName;
    string inputSalary;

    Console.WriteLine("Information of the employee: {0}", info);

    Console.WriteLine("Name:");
    inputName = Console.ReadLine();

    Console.WriteLine("Salary:");
    inputSalary = Console.ReadLine();

    Person person = new Person();
    person.Name = inputName;
    person.Salary = int.Parse(inputSalary);
    return person;

}

Comments

-1

If you want one method to return different types of information, then I would use generics:

public static T GetInfo<T>(string name);

// This can be called as following:
string name = GetInfo<string>("name");
int salary = GetInfo<int>("salary");

There's one problem, though: Console.ReadLine returns a string, while our method could return any type. How can it convert a string to its 'target' type? You could check T and write custom logic for all types you want to support, but that's cumbersome and brittle. A better solution is to let the caller pass in a little function that knows how to transform a string into a specific type:

public static T GetInfo<T>(string name, Func<string, T> convert);

// This can be called as following:
string name = GetInfo<string>("name", s => s);
int salary = GetInfo<int>("salary", int.Parse);

Now how do you implement that method?

public static T GetInfo<T>(string name, Func<string, T> convert)
{
    Console.WriteLine("Please enter " + name);
    string input = Console.ReadLine();
    return convert(input);
}

A few notes:

  • Type parameters are often just named T, but I find it useful to give them more descriptive names, such as TInfo.
  • The <string> and <int> parts can be left out in the second example, because the compiler has sufficient information to infer their types.
  • I've left our error handling to keep the examples short and simple. In production code, you'll want to come up with a strategy to handle invalid input, though.

Comments

Your Answer

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