-2

Please close to avoid any additional unnecessary downvoting

I'm quite shocked that I can't find this on SO so I guess my question is acceptable...

Originally, I wanted to pass multiple variables to Main from a method but it only allows one return type, so I then split the method up into two methods, returning both data types but I want to send them to Main so I can display them after being assigned in the method...

CODE

      static void Main(string[] args)
    {
        GetID();
        Console.WriteLine("Your id is {0} and your password is {1}");            
    }

    public static int GetID()
    {

        Console.WriteLine("Please enter your ID");
       int id = int.Parse(Console.ReadLine());
        Console.WriteLine("You entered {0} as the id \t, is this correct? Y/N", id);
        string response = Console.ReadLine();
        switch (response)
        {
            case "N":
                GetID();
                break;
            case "Y":
                Console.WriteLine("Accepted");
                GetPass(ref id);
                break;
            default:
                Console.WriteLine("Incorrect answer");
                break;
        }
        return id;
    }
    public static string GetPass(ref int id)
    {
        Console.WriteLine("Please enter your password");
        string password = Console.ReadLine();
        Console.WriteLine("You entered {0} as the id \t, is this correct? Y/N", password);
        string response = Console.ReadLine();
        switch (response)
        {
            case "N":
                GetPass(ref id);
                break;
            case "Y":
                Console.WriteLine("Accepted");
                break;
            default:
                Console.WriteLine("Incorrect answer");
                break;
        }
        return password;
    }

I have come back to C# after a long hiatus and I'm quite rusty as you can tell by this simple issue, so I apologise for the low quality power of the question

Danny Watson
  • 165
  • 5
  • 24
  • You project can only have one Main(). So I think you code posted should be in a class module instead of a console module. A class module doesn't have a main. – jdweng Dec 23 '16 at 20:40
  • jdweng - these are methods and I only use one Main() – Danny Watson Dec 23 '16 at 20:42
  • You can only pass variables to main from a command line arg[]. So where are your inputs coming from. In windows when you double click on the exe there are no inputs. You can create a shortcut and add arguments into the shortcut. – jdweng Dec 23 '16 at 20:49
  • @jdweng I think you are confused – Danny Watson Dec 23 '16 at 21:00
  • @DannyWatson Actually your question is a bit misstated; to *pass* a variable to main you have to use command line args as @jdweng stated. You aren't passing variables in, you are trying to get them out of other methods (and into `Main` which is actually ancillary here). Your title especially indicates that you are manually invoking `Main` (since that is the context of "passing" in most programming) – BradleyDotNET Dec 23 '16 at 21:03

4 Answers4

2

Can you use variables to assign the return types in main before printing to the console?

static void Main(string[] args)
{
    var id = GetID();
    var password = GetPass(ref id);
    Console.WriteLine("Your id is {0} and your password is {1}", id, password);            
}
imlim
  • 1,607
  • 1
  • 14
  • 21
Jonathan Walton
  • 449
  • 3
  • 18
1

Instead of returning multiple values in separate calls, consider creating an object type that contains multiple values, and return an instance of that object type.

Jim Kiley
  • 3,632
  • 3
  • 26
  • 43
  • The info I came across did include OOP, but I would prefer to avoid it out of simplicity as this code is merely a refresher – Danny Watson Dec 23 '16 at 20:39
  • @DannyWatson All else being equal, don't avoid it for *too* long. – BradleyDotNET Dec 23 '16 at 20:49
  • I love how whenever @BradleyDotNET is on the case I always get a downvote shortly after his comment – Danny Watson Dec 23 '16 at 20:58
  • @DannyWatson lol, not me (can't say if I've ever downvoted you; but not this time) For that matter, I'm not sure I've even seen any of your recent posts (maybe you deleted them?); are you sure I'm the link here? – BradleyDotNET Dec 23 '16 at 20:59
1

You can use out parameters to return multiple values from a method call:

For instance:

public void MyMethod(out string first, out string second)
{
    first = "some";
    second = "thing";
}

And you can call as follows:

string f;
string s;
MyMethod(out f, out s);

Some info on out can be found here:

And the difference between out and ref:

Community
  • 1
  • 1
Klinger
  • 4,900
  • 1
  • 30
  • 35
0

you have to make change in code as below which should work.

static void Main(string[] args)
{
    Hashtable ht = GetID(); // Use hashtable to concate 2 parameters
    Console.WriteLine("Your id is {0} and your password is {1}", ht["id"], ht["pass"]);            
}
// Change return type to Hashtable so it can return 2 concate parameter
public static Hashtable GetID()
{
    Hashtable ht = new Hashtable();
    Console.WriteLine("Please enter your ID");
    int id = int.Parse(Console.ReadLine());
    Console.WriteLine("You entered {0} as the id \t, is this correct? Y/N", id);
    string response = Console.ReadLine();
    // Store Password from here
    string pass = "";
    switch (response)
    {
        case "N":
            GetID();
            break;
        case "Y":
            Console.WriteLine("Accepted");
            pass = GetPass(id);
            break;
        default:
            Console.WriteLine("Incorrect answer");
            break;
    }
    // Adding 2 parameters
    ht["id"] = id;
    ht["pass"] = pass;
    return ht;
}
// No need of ref
public static string GetPass(int id)
{
    Console.WriteLine("Please enter your password");
    string password = Console.ReadLine();
    Console.WriteLine("You entered {0} as the id \t, is this correct? Y/N", password);
    string response = Console.ReadLine();
    switch (response)
    {
        case "N":
            GetPass(ref id);
            break;
        case "Y":
            Console.WriteLine("Accepted");
            break;
        default:
            Console.WriteLine("Incorrect answer");
            break;
    }
    return password;
}
imlim
  • 1,607
  • 1
  • 14
  • 21