GoFindInGoogle
GoFindInGoogle

Reputation: 61

Accessing list from method in another method

I would like to access list (in my program named "section") inside a method(menu choose). I tried 3 ways:

public static void dataBase()
        {
                List<float> section = new List<float>();
        }


// 1st try
//  List<float> section = new List<float>();
//
        public static void mainMenu()
        {
            Console.Clear();
            Console.WriteLine("Trans->Connector->\n");
            Console.WriteLine("Add: \n1. Section \n2. Wled \n3. Regenerator");
            menuChoose();
        }

        public static void menuChoose()
        {

            var key = Console.ReadKey();
            switch (key.Key)
            {
                case ConsoleKey.D1:
                case ConsoleKey.NumPad1:

                    Console.Clear();
                    Console.WriteLine("Give lenght:");
                    float result;
                    float.TryParse(Console.ReadLine(), out result);
                    dataBase.section.Add();
                    section.Add(result);
                    break;

                case ConsoleKey.D2:

                    Console.WriteLine("2");
                    break;

                case ConsoleKey.D3:
                    Console.WriteLine("3");
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }

        }

        static void Main(string[] args)
        {

            int WeldCount;
            int ConnectroCount;
//3rd try
// List<float> section = new List<float>();
//

            mainMenu();
        }

Thank you for your time!

Upvotes: 0

Views: 6872

Answers (4)

GoFindInGoogle
GoFindInGoogle

Reputation: 61

I could not use any of your answers so I did this in my way(much worse than yours, for sure)

public class Program
{

    public int WeldCount;
    public int ConnectroCount;
    public List<float> section = new List<float>();
    //public List<> TrackElements = new List<>();

    public Program()
    {
        section.Add(0);

    }
    public void showResults()
    {
        float allSections = 0;
        foreach (float item in section)
        {
            allSections += item;
        }

        Console.Clear();
        Console.WriteLine("c1 {0}, c2 {1}, c3{2}", WeldCount,ConnectroCount,allSections);
        Console.ReadKey();
    }



    public void finalConstruction()
    {




    }


    public static void mainMenu()
    {
        Console.Clear();
        Console.WriteLine("\n");
        Console.WriteLine("Add: \n1. Section \n2. Weld \n3. Regenerator\n4. Show results");




    }


    public void menuChoose()
    {

        var key = Console.ReadKey();

        switch (key.Key)
        {
            case ConsoleKey.D1:
            case ConsoleKey.NumPad1:

                Console.Clear();
                Console.WriteLine("Give lenght:");
                float result;
                float.TryParse(Console.ReadLine(), out result);


                section.Add(result);



                mainMenu();
                menuChoose();

                break;

            case ConsoleKey.D2:

                WeldCount++;
                mainMenu();
                menuChoose();
                break;

            case ConsoleKey.D3:
                ConnectroCount++;
                mainMenu();
                menuChoose();
                break;

            case ConsoleKey.D4:
                showResults();
                mainMenu();
                menuChoose();
                break;





            default:
                Console.WriteLine("wtf did just happend");
                break;
        }

    }


    static void Main(string[] args)
    {


        Program program = new Program();
        mainMenu();
        program.menuChoose();




    }





}
}

Upvotes: 0

Maxim Fleitling
Maxim Fleitling

Reputation: 562

You can to it, if you would implement something like this:

public static class DataBase
{
    public static List<float> Section { get; set; }

    static DataBase()
    {
        Section = new List<float>();
    }
}

here how to use e.g. in your switch statement:

    case ConsoleKey.NumPad1:
         Console.Clear();
         Console.WriteLine("Give lenght:");
         float result;
         float.TryParse(Console.ReadLine(), out result);
         DataBase.Section.Add(result);
      break;

I didn't get what you try to achieve by dataBase.section.Add(); so I removed it in my example.

Upvotes: 0

Idos
Idos

Reputation: 15310

You can't access a member in the local scope of your function outside it. You might want to think about making it a private instance variable inside your class that you will then be able to access from any method declared that belongs to this class, something along these lines:

public class MyClass
{
    // this field is accessible from any method declared within this class
    private List<Float> section;
    public MyClass()
    {
        section = new List<Float>();
    }

    private void someMethod()
    {
        section.Add(2.2);    
        Console.WriteLine(section[0]); // example 
    }
}

Upvotes: 1

Filip
Filip

Reputation: 1864

Well you can make it class level variable (like in your 1st try) and make it static.

However you should use return value from your menuChoose() method. Having all code depend on single static list instance is not ideal.

public static List<float> menuChoose()
        {

            List<float> selection = new List<float>();
            var key = Console.ReadKey();
            switch (key.Key)
            {
                case ConsoleKey.D1:
                case ConsoleKey.NumPad1:

                    Console.Clear();
                    Console.WriteLine("Give lenght:");
                    float result;
                    float.TryParse(Console.ReadLine(), out result);
                    selection.Add(result);
                    break;

                case ConsoleKey.D2:

                    Console.WriteLine("2");
                    break;

                case ConsoleKey.D3:
                    Console.WriteLine("3");
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
          return selection;

        }

Upvotes: 0

Related Questions