Link to home
Start Free TrialLog in
Avatar of MHenry
MHenry

asked on

How to refer to a list item later in program in C#?

I'm really confused here. I'm trying to figure out how I can print out the station name and the contact name in another method. Nothing I try is working...

Here's what I have to setup the class and the list....

   public class WeatherStation
    {
        private string temperature;
        private string stationDesignation;
        private string contactName;

        public WeatherStation(string StationDesignation, string ContactName)
        {
            stationDesignation = StationDesignation;
            contactName = ContactName;
        }

        public void setStation(string StationDesignation) { stationDesignation = StationDesignation; }
        public void setContact(string ContactName) { contactName = ContactName; }


        public string getStation() { return stationDesignation; }
        public string getContact() { return contactName; }

    }



    class WeatherStationList
    {

        private List<WeatherStation> StationList;   //First, we need a List object variable...

        public WeatherStationList()   //...then a default constructor...
        {

            StationList = new List<WeatherStation>();  //create a List object for WeatherStation...

        }

        public void Add(string StationDesignation, string ContactName)
        {

            StationList.Add(new WeatherStation(StationDesignation, ContactName));

        }
    }

.......................................................................


And here's the method. What I want to do is print out something like WeatherStationList.StationDesignation but I can't figure out what I'm doing wrong.


public void Fill()
        {
Console.WriteLine("Enter reported temperatures...");
            Console.WriteLine("\n");
            for (tempCount = 0; tempCount < MaxSize; tempCount++)
            {
                Console.Write(WeatherStation.StationList.Count[tempCount]);
                Console.WriteLine("\n");
                Console.Write(WeatherStationList.ContactName[tempCount]);
                Console.WriteLine("\n");
                Console.WriteLine("Enter Temperature:");


Any help?

Thanks,
MH

Avatar of milindsm
milindsm
Flag of India image

Try this,

Console.Write(WeatherStationList[tempCount].StationDesignation) ;
Console.WriteLine("\n");
Console.Write(WeatherStationList[tempCount].ContactName);
Avatar of MHenry
MHenry

ASKER

that shows WeatherStationList is a type but is used like a variable.
Oh ...my bad.... try this

Console.Write(StationList[tempCount].StationDesignation) ;
Console.WriteLine("\n");
Console.Write(StationList[tempCount].ContactName);

I hope fill method is a member of WeatherStationList class
Avatar of MHenry

ASKER

That throws StationList does not exist in the current context.

I know I'm doing something wrong, just don't know what it is...



The WeatherStationList class is this:

   class WeatherStationList
    {

        private List<WeatherStation> StationList;   //First, we need a List object variable...

        public WeatherStationList()   //...then a default constructor...
        {

            StationList = new List<WeatherStation>();  //create a List object for WeatherStation...

        }

        public void Add(string StationDesignation, string ContactName)
        {

            StationList.Add(new WeatherStation(StationDesignation, ContactName));

        }
Can you just paste a complete code if possible????
If Fill is a method outside your class then it will throw an error because StationList is a private member...
What is the MaxSize value in For loop BTW??? How did u arrive at this value???
Avatar of MHenry

ASKER

It's not finished, I'm stuck on this part so it's really ugly...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project2
{


    public class PollenCountTemp
    {
        private string temperature, lowPollenCount, highPollenCount, pollenAverage;

       
        public PollenCountTemp(string Temperature, string LowPollenCount, string HighPollenCount, string PollenAverage)
        {
            temperature = Temperature;
            lowPollenCount = LowPollenCount;
            highPollenCount = HighPollenCount;
            pollenAverage = PollenAverage;
        }

            public void setTemp(string Temperature) { temperature = Temperature; }
            public void setLow(string LowPollenCount) { lowPollenCount = LowPollenCount; }
            public void setHigh(string HighPollenCount) {highPollenCount = HighPollenCount;}
            public void setAvg(string PollenAverage) { pollenAverage = PollenAverage; }

            public string getTemp() { return temperature; }
            public string getLow() { return lowPollenCount; }
            public string getHigh() {return highPollenCount;}
            public string getAvg() { return pollenAverage; }

    }

    //setup the Pollen/Temperature List
    class PollenCountTempList
    {

        private List<PollenCountTemp> PollenTempList;  



        public PollenCountTempList()   // default constructor...
        {

            PollenTempList = new List<PollenCountTemp>();  //create a List object for WeatherStation...

        }

        public void Add(string Temperature, string LowPollenCount, string HighPollenCount, string PollenAverage)
        {

            PollenTempList.Add(new PollenCountTemp(Temperature, LowPollenCount, HighPollenCount, PollenAverage));

        }
    }


    public class WeatherStation
    {
        private string temperature;
        private string stationDesignation;
        private string contactName;

        public WeatherStation(string StationDesignation, string ContactName)
        {
            stationDesignation = StationDesignation;
            contactName = ContactName;
        }

        public void setStation(string StationDesignation) { stationDesignation = StationDesignation; }
        public void setContact(string ContactName) { contactName = ContactName; }


        public string getStation() { return stationDesignation; }
        public string getContact() { return contactName; }
    }


    class WeatherStationList
    {

        private List<WeatherStation> StationList;  

        public WeatherStationList()   // default constructor...
        {

            StationList = new List<WeatherStation>();  //create a List object for WeatherStation...

        }

        public void Add(string StationDesignation, string ContactName)
        {

            StationList.Add(new WeatherStation(StationDesignation, ContactName));

        }

    }





    //The UI

    class Program

    {

        static void Main(string[] args)

        {

            new WeatherStationUI();  //Jump start it...

        }

    }

    class WeatherStationUI
    {
        private WeatherStationList Collection = new WeatherStationList();
        private PollenCountTempList PollenTempCollection = new PollenCountTempList();

       
        public WeatherStationUI()
        {
            string Command;

            while (true)  //Define a command loop
            {

                Menu();  

                Command = (Console.ReadLine());

                if (Command == "Quit")  //Do it!

                    break;


                else if (Command == "Post Temperatures")
                {
                    Fill();
                }



                else if (Command == "Add Stations")
                {

                    AddStation();

                }


                else if (Command == "Daily Report")
                {
                    Show();
                }

                else if (Command == "High-Low Report")
                {
                    HighLow();
                }

                else if (Command == "Daily Pollen Count")
                {
                    Console.WriteLine("NSG Daily Pollen Report");
                    Console.WriteLine("================================================");

                    PollenCountReport();
                }

                else
                {

                    Console.WriteLine("========NSG Temperature Data Report========");

                    HighLow();
                }
            }
        }
 
           
           
            public static void Menu()
            {
            Console.WriteLine("\n");
            Console.WriteLine("Choices.................");
            Console.WriteLine("\n");
            Console.WriteLine("---------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("\tAdd Stations");
            Console.WriteLine("\tPost Temperatures");
            Console.WriteLine("\tDaily Report");
            Console.WriteLine("\tHigh-Low Report");
            Console.WriteLine("\tDaily Pollen Count");
            Console.WriteLine("\tQuit");
            Console.WriteLine("\n");
            Console.WriteLine("---------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("Enter Command:");
            Console.WriteLine("\n");
            Console.WriteLine("---------------------------");
            }
       
       
         // Adding new stations

        public void AddStation()
        {
            int tempCount = 0;
            int MaxSize = 8;
            string StationDesignation, ContactName;
           
            Console.WriteLine("\n");
            Console.WriteLine("Enter Station Information Below, stop To Quit");


            for (tempCount = 0; tempCount < MaxSize; tempCount++)
            {
                Console.WriteLine("\n");
                Console.WriteLine("Enter Weather Station Designation:");


                //get Station Designation

                StationDesignation = Console.ReadLine();

                if (StationDesignation.Equals("Stop"))

                    break;

               //get contact name

                Console.WriteLine("\n");
                Console.WriteLine("Enter Contact Person:");
                ContactName = Console.ReadLine();
                if (ContactName.Equals("Stop"))

                    break;

                //add to collection

                 Collection.Add(StationDesignation, ContactName);


                Console.WriteLine("---------------------------");
                Console.WriteLine("---------------------------");
            }
        }

 
        public void Fill()
        {
            int tempCount = 0; // loop counter for entered values
            double meanTempF = 0;
            double meanTempC = 0;
            double meanTempFinalF = 0;
            double meanTempFinalC = 0;
            double pollenAverage = 0;
            double temperatureDouble = 0;
            double pollenLow = 0;
            double pollenHigh = 0;
            int MaxSize = 8;

            string getPollenHigh; //pollen high count
            string getPollenLow; // pollen low count
            string Temperature, LowPollenCount, HighPollenCount, PollenAverage;
           

            //setup variables for gathering information
            //Start gathering info from user

            Console.WriteLine("Enter reported temperatures...");
            Console.WriteLine("\n");
            for (tempCount = 0; tempCount < MaxSize; tempCount++)
            {
                Console.Write(StationList[tempCount].StationDesignation);
                Console.WriteLine("\n");
                Console.Write(WeatherStationList.ContactName[tempCount]);
                Console.WriteLine("\n");
                Console.WriteLine("Enter Temperature:");

                Temperature = Console.ReadLine(); // Get string from user
                if (Temperature.Equals("Stop"))

                    break;
                else
                {

                    Console.WriteLine("\n");
                    Console.WriteLine("Enter Pollen Count:");
                    Console.WriteLine("\n");
                    Console.WriteLine("Enter Pollen High:");
                    HighPollenCount = Console.ReadLine();
                    if (HighPollenCount.Equals("stop"))
                        break;
                   
                   
                    Console.WriteLine("\n");
                    Console.WriteLine("Enter Pollen Low:");
                    LowPollenCount = Console.ReadLine();
                    if (LowPollenCount.Equals("stop"))
                        break;
                   
                   
                    // Convert it to a number...
                    temperatureDouble = (double.Parse(Temperature));

                    // get Pollen high count

                   
                   
                        // convert to double
                    pollenHigh = double.Parse(HighPollenCount);


                    // get Pollen low count

                   
                        // convert to double

                    pollenLow = double.Parse(LowPollenCount);


                    pollenAverage = (pollenLow + pollenHigh) / 2;

                    pollenAverage.ToString = PollenAverage;



                    // ...compute the Celsius values and store in the wsCelsius array...

                    wsCelsius[tempCount] = ((wsTemps[tempCount]) - 32) / 9;


                    //might as well get the mean temperatures while we're here

                    meanTempF = meanTempF + wsTemps[tempCount];
                    meanTempC = meanTempC + wsCelsius[tempCount];


                    // Calculate the Mean Temp. Use tempCount. It has the total number of temperatues entered. Remeber to subtract one for the "Stop".


                    meanTempFinalF = (meanTempF / tempCount);
                    meanTempFinalC = (meanTempC / tempCount);

                    PollenTempCollection.Add(Temperature, LowPollenCount, HighPollenCount, PollenAverage);


                    Console.WriteLine("\n");
                    Console.WriteLine("==============================================");
                }
            }
        }


       

       

//Create Report

        public static void Show()
        {
           
            Console.WriteLine("---------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("=============NSG Temperature Data Report===============");
            Console.WriteLine("\n");
            Console.WriteLine("\t  Fahrenheit            Celsius");

            for (tempCount = 0; tempCount < MaxSize; tempCount++)
            {



                Console.WriteLine("\n");
                Console.WriteLine("-------------------------------------------");
                Console.WriteLine("\n");
                Console.WriteLine(wsDesignation[tempCount] + "\t     " + wsTemps[tempCount].ToString("n1") + "\t         " + wsCelsius[tempCount].ToString("n2"));
                Console.WriteLine("\n");
                Console.WriteLine("-------------------------------------------");
                Console.WriteLine("\n");
            }
        }



        //setup the Pollen Report

        public static void PollenCountReport()
        {
            for (tempCount = 0; tempCount < MaxSize; tempCount++)
            {



                Console.WriteLine("\n");
                Console.WriteLine("-------------------------------------------");
                Console.WriteLine("\n");
                Console.WriteLine(wsDesignation[tempCount] + "\t     " + wsPollen[tempCount]);
                Console.WriteLine("\n");
                Console.WriteLine("-------------------------------------------");
                Console.WriteLine("\n");
            }


        }


        //sort for high and low temp


        public static void HighLow()
        {

            int J, K;
            int C, D;


            double Buffer;
            double BufferCelsius;


            //sort fahrenheit

            for (J = 0; J < tempCount; J++)

                for (K = 0; K < tempCount - 1; K++)

                    if (wsTemps[K].CompareTo(wsTemps[K + 1]) > 0)  //Check to see if the current
                    {                               //element is larger than the next...if so...

                        Buffer = wsTemps[K];   //Swap them...

                        wsTemps[K] = wsTemps[K + 1];

                        wsTemps[K + 1] = Buffer;

                    }


            //sort celsius

            for (C = 0; C < tempCount; C++)

                for (D = 0; D < tempCount - 1; D++)

                    if (wsCelsius[D].CompareTo(wsCelsius[D + 1]) > 0)  //Check to see if the current
                    {                               //element is larger than the next...if so...

                        BufferCelsius = wsCelsius[D];   //Swap them...

                        wsCelsius[D] = wsCelsius[D + 1];

                        wsCelsius[D + 1] = BufferCelsius;

                    }

            Console.WriteLine("---------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("=================================NSG Temperature Data Report===================");
            Console.WriteLine("\n");
            Console.WriteLine("                              Fahrenheit             Celsius");
            Console.WriteLine("\n");
            Console.WriteLine("Lowest Temperature:             " + wsTemps[tempCount].ToString("n1") + "                  " + wsCelsius[0].ToString("n2"));
            Console.WriteLine("\n");
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("Highest Temperature:            " + wsTemps[0].ToString("n1") + "                  " + wsCelsius[tempCount].ToString("n2"));
            Console.WriteLine("\n");
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("\n");
            Console.WriteLine(" ==============================End Temperature Data Report=====================");
        }
    }
}


Dude...!!! Fill is not a member of any class..... and StationList is a private member you can't directly access it...!!! And BTW, where are you creating an object of WeatherStationList ?????

I tried compiling your code and I would suggest you write your code step-by-step. Don't try to write everything in one shot...!!!!!
Avatar of MHenry

ASKER

and if I understood any of that, I'd be in a lot better shape...

I'm trying to learn this stuff. I don't understand why StationList is a private member? This encapsulation stuff is kicking my butt.

Why go through all the trouble of setting up this if it's private? I thought the public WeatherStationList part made it public?

I'm so confused...



 class WeatherStationList
    {

        private List<WeatherStation> StationList;  

        public WeatherStationList()   //  default constructor...
        {

            StationList = new List<WeatherStation>();  //create a List object for WeatherStation...

        }

        public void Add(string StationDesignation, string ContactName)
        {

            StationList.Add(new WeatherStation(StationDesignation, ContactName));

        }

    }
ASKER CERTIFIED SOLUTION
Avatar of milindsm
milindsm
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MHenry

ASKER

Thanks. As you can see, I'm floundering here. Trying to understand all the ins and outs is really frustrating.
I don't see declaration of wsCelsius anywhere?????!!!!!!
Avatar of MHenry

ASKER

Haven't gotten that far yet. I was stuck at this point. I've been working on this for about four hours. I finally gave up and posted here.

I'm too old to learn new crap...
no issues... just develop your code step-by-step otherwise it becomes messy and very difficult for a developer to sort out the issues..... :)