class ReadGradesIn
    {

        public void readGrade()
        {

            int numberOfA = 0; //number of A grades
            int numberOfB = 0;//number of B grades
            int numberOfC = 0;//number of C grades
            int numberOfD = 0;//number of D grades
            int numberOfE = 0;//number of E grades
            int numberOfF = 0;//number of F grades
            string lineOfData;//value given to this variable is what it is reading from file

            TextReader grades = new StreamReader("grades.dat"); //read grades in

            lineOfData = grades.ReadLine(); 


            while (lineOfData != null) //start while loop until lineofdata is null
            {
                lineOfData = grades.ReadLine();
              
                switch (lineOfData) //start switch statement
                {
                    case "A":
                        numberOfA++;                       
                        break;
                    case "B":
                        numberOfB++; 
                        break;
                    case "C":
                        numberOfC++;
                        break;
                    case "D":
                        numberOfD++;
                        break;
                    case "E":
                        numberOfE++;
                        break;
                    case "F":
                        numberOfF++;
                        break;
                }//end switch statement
               

            }//end while loop
            int numOfGrades = numberOfA + numberOfB + numberOfC + numberOfD + numberOfE + numberOfF;
            grades.Close(); //close textreader
           
            Console.ReadLine();

        }//end method readGrade

    }//end class

Hi everyone, new here. Basically what I am trying to achieve is to be able to access the variable numberOfA , numberOfB, numebrOfC, numberOfD from another class. I am fairly new to C# and thought the best way to do this would be to put a switch statement in. I am reading grades (a,b,c,d,e,f) in from a .dat file but would like to keep all the above in a seperate class, the only problem is how I access the values in the variables, hope this makes sense,

Many Thanks in advance...

Edited 6 Years Ago by __avd: Added [code] tags.

Hi just add public keyword at the variable declaration (e.g. public int numberOfA;..etc). This will solve your prob.

Sori, declare public in the global instead of in the function. e.g.

class ReadGradesIn
{
    public int numberOfA;

Also, instead of making the variable public, you could make it private and then use public properties to control the getting and setting of this variable.

This is the preferred object oriented way of doing this as it encapsulates the variable. I understand this is probably an assignment or something similar, but might as well get used to it now.

class ReadGradesIn
{
      int numberofA; // private by default.
      Public int NumberofA
       {
          get { return numberOfA; }
          set { numberofA = value; }
       }

}

thanks mate great help!

Hi, if i solved your prob. rate me as solving your prob and close the thread. Thank you man.

I see jbrock so your saying that I could then call the numberOfA function from another class?

I see jbrock so your saying that I could then call the numberOfA function from another class?

Yes, with the public modifier you can do that. With properties, you can also have more control on how the variable is set accessed. For example, in my area, an A is between 90 and 100, so when creating this property, you can use the set to only accept numbers between 90 and 100.

This question has already been answered. Start a new discussion instead.