none
Returning more than 1 item from a method RRS feed

  • Question

  • I'm learning C# but struggling with Methods and how to let them speak to each other

    throws an error ><

    class Program
        {
            public static string myName { get; private set; }
            public static string myName2 { get; private set; }

            static void Main(string[] args)
            {
                myName = GetName(myName,myName2)

                NamePrint(myName,myName2);
                Console.ReadKey();
            }

            public static string GetName(string myName, string myName2)
            {
                Console.WriteLine("What is your name?");
                myName = Console.ReadLine();

                Console.Write("What is your Second Name?");
                myName2 = Console.ReadLine();

                
            }

            public static void NamePrint(string myName)
            {
                Console.WriteLine("Your Full Name is {0}{1}", myName,myName2);
            }
        }

    Saturday, March 10, 2018 11:11 AM

Answers

  • Hello,

    I've refactored your code, try this out. You may ask why I'm not passing information? This is because you have private variables and see no reason to pass variables around and yes there are other ways to do so but I'm keeping it simple.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public static string myName { get; set; }
            public static string myName2 { get; set; }
    
            static void Main(string[] args)
            {
                GetName();
    
                NamePrint();
                Console.ReadKey();
            }
            public static void GetName()
            {
                Console.WriteLine("What is your name?");
                myName = Console.ReadLine();
    
                Console.Write("What is your Second Name?");
                myName2 = Console.ReadLine();
            }
    
            public static void NamePrint()
            {
                Console.WriteLine($"Your Full Name is {myName} {myName2}");
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by MarcOwaR Saturday, March 10, 2018 11:35 AM
    Saturday, March 10, 2018 11:20 AM
    Moderator

All replies

  • Hi, first of all you are on the right track. Now when you want to return more than 1 value from a single method you use the out keyword. Like this:

    public static void GetName(out string myName, out string myName2)
            {
                Console.WriteLine("What is your name?");
                myName = Console.ReadLine();

                Console.Write("What is your Second Name?");
                myName2 = Console.ReadLine();

                
            }

    Try this out and send me your feedback.

    UPDATED: Note that when you call the function you need to call it like this

    GetName(out myName1, out myName2);

    Saturday, March 10, 2018 11:19 AM
  • Hello,

    I've refactored your code, try this out. You may ask why I'm not passing information? This is because you have private variables and see no reason to pass variables around and yes there are other ways to do so but I'm keeping it simple.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public static string myName { get; set; }
            public static string myName2 { get; set; }
    
            static void Main(string[] args)
            {
                GetName();
    
                NamePrint();
                Console.ReadKey();
            }
            public static void GetName()
            {
                Console.WriteLine("What is your name?");
                myName = Console.ReadLine();
    
                Console.Write("What is your Second Name?");
                myName2 = Console.ReadLine();
            }
    
            public static void NamePrint()
            {
                Console.WriteLine($"Your Full Name is {myName} {myName2}");
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by MarcOwaR Saturday, March 10, 2018 11:35 AM
    Saturday, March 10, 2018 11:20 AM
    Moderator
  • Here is a better approach using a class.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            private static Person _person = null;
    
            static void Main(string[] args)
            {
                GetName();
    
                NamePrint();
                Console.ReadKey();
            }
            public static void GetName()
            {
                var myName = "";
                var myName2 = "";
    
                Console.WriteLine("What is your name?");
                myName = Console.ReadLine();
    
                Console.Write("What is your Second Name?");
                myName2 = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(myName) && !string.IsNullOrWhiteSpace(myName2))
                {
                    _person = new Person() {FirstName = myName, LastName = myName2};
                }
            }
    
            public static void NamePrint()
            {
                if (_person != null)
                {
                    Console.WriteLine(_person);
                }
                else
                {
                    Console.WriteLine("Missing first or last or both names");
                }
            }
        }
    
        class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public override string ToString()
            {
                return $"{FirstName} {LastName}";
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 10, 2018 11:28 AM
    Moderator
  • Thanks for the speedy feedback :) I'll look over what you've explained, thanks for your help 
    Saturday, March 10, 2018 11:36 AM