-4

i am trying to use the value of a loop of one class into another class how can i do it. please help me..

       while (!sStreamReader.EndOfStream)
            {
                string sLine = sStreamReader.ReadLine();
                // make sure we have something to work with
                if (String.IsNullOrEmpty(sLine)) continue;

                string[] cols = sLine.Split(',');
                // make sure we have the minimum number of columns to process
                if (cols.Length > 4)
                {
                    double a = Convert.ToDouble(cols[1]);
                    Console.Write(a);
                    int b = Convert.ToInt32(cols[3]);
                    Console.WriteLine(b);
                    Console.WriteLine();
                }
            }

i am trying to use the values of a and b into another class. this loop is another class.

Aditya Agarwal
  • 165
  • 1
  • 5
  • 15

3 Answers3

0

You may be better with Int32.TryParse so you don't get the exception in the first place, but to throw it out to the next level up, you'd use throw (See here for throw vs throw ex). Ie: In tyour code, something like

try {
    while (!sStreamReader.EndOfStream)
        {
            string sLine = sStreamReader.ReadLine();
            // make sure we have something to work with
            if (String.IsNullOrEmpty(sLine)) continue;

            string[] cols = sLine.Split(',');
            // make sure we have the minimum number of columns to process
            if (cols.Length > 4)
            {
                double a = Convert.ToDouble(cols[1]);
                Console.Write(a);
                int b = Convert.ToInt32(cols[3]);
                Console.WriteLine(b);
                Console.WriteLine();
            }
        }
} catch {
    throw;
}
Community
  • 1
  • 1
Echilon
  • 10,064
  • 33
  • 131
  • 217
  • 1
    Not to be rude, but how is this an answer? I think the question was how to use a and b out of the while scope :) Don't let the throw in the title get you off track! – Gerald Versluis Aug 03 '12 at 07:09
0

It depends what you want to do in that class. As i see it there is plenty of solutions to this. Do you want all the a and b to your class? Simply save them to 2 lists or something simular. Then send them into the class constructor or method. Example :

    var listOfAs = new List<double>();
        var listOfBs = new List<int>();
        while (!sStreamReader.EndOfStream)
        {
           string sLine = sStreamReader.ReadLine();
           // make sure we have something to work with
           if (String.IsNullOrEmpty(sLine)) continue;
           string[] cols = sLine.Split(',');
           // make sure we have the minimum number of columns to process
           if (cols.Length > 4)
           {
               double a = Convert.ToDouble(cols[1]);
               listOfAs.Add(a);

               int b = Convert.ToInt32(cols[3]);
               listOfBs.Add(b);
           }
        }
//Here you can use all As and Bs and send them to the other class
MyClass.DoSomething(listOfAs,listOfBs);

Just want a and b foreach case? Just send them directly to the class constructor or method and work with them directly inside the loop, please observe that this will happen foreach a and b that matches cols.length > 4.

inside the loop

double a = Convert.ToDouble(cols[1]);
int b = Convert.ToInt32(cols[3]);
MyClass.DoSomething(a,b);
Jonas W
  • 3,200
  • 1
  • 31
  • 44
0

Right now you cannot use a and/or b out of the if statement. This is because you are only declaring them there, so you only have access to them until the last brace (of the if statement).

If you want access to them beyond that you have to declare them earlier. I.e. If you would want them to use in just the while loop do it like this:

    while (!sStreamReader.EndOfStream)
    {
        double a = 0.00;
        int b = 0;

        string sLine = sStreamReader.ReadLine();
        // make sure we have something to work with
        if (String.IsNullOrEmpty(sLine)) continue;

        string[] cols = sLine.Split(',');
        // make sure we have the minimum number of columns to process
        if (cols.Length > 4)
        {
            a = Convert.ToDouble(cols[1]);
            b = Convert.ToInt32(cols[3]);
        }

        // You can now use these here too!
        Console.Write(a);
        Console.WriteLine(b);
        Console.WriteLine();
    }

If you want to use it even outside the scope (you might want to loop up this word) of the while just lift them up another level, outside of the while.

Note that this only working for one value. The values of a and b get overwritten each time. If you would want a list of values or something create an array that you can pass on. (See Jonas' example)

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100