moe
moe

Reputation: 5249

using forloop in switch statement using c#

I want to use for loop with switch statement but getting this error: "for each statement cannot operate on variable of type int"

 DataRow row = MyData.Rows[0];
   int temp = Convert.ToInt32(row["Category"]);

foreach (int v in temp)
{
                switch (temp)
                {
                    case "1":
                        lblCategory.Text = temp + " - Complete.";
                        break;

                    case "2":
                        lblCategory.Text = temp + " - Not Complete";
                        break;
               }
}

okay, i am updating my code based on your feedback but have one last question, i have column called Category in my data collection and it has values like 1,2,3 etc so that is why i mean when i say case "1":

var temp = MyData.Rows;
foreach (int v in temp)
{
    switch (v.Category)
    {
    case "1":
        lblCategory.Text += v + " - Complete.\n";
        break;
    case "2":
        lblCategory.Text += v + " - Not Complete\n";
        break;
    }
}

Upvotes: 2

Views: 2009

Answers (4)

Sina Iravanian
Sina Iravanian

Reputation: 16286

You can use:

foreach (DataRow row in MyData.Rows)
{
    int v = Convert.ToInt32(row["Category"]);
    switch (v)
    {
    case 1:
        lblCategory.Text += v + " - Complete.\n";
        break;
    case 2:
        lblCategory.Text += v + " - Not Complete\n";
        break;
    }
}

In your code sample, you are iterating items in the collection, but switching on the collection itself. Also you are converting an item to an Int32 but using strings in the case statement.

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98740

From foreach, in (C# Reference)

The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface. The foreach statement is used to iterate through the collection to get the information that you want..

But in your code temp is an int, Int32 doesn't implement IEnumerable or IEnumerable<T> and it is not a collection at all, so..

Upvotes: 1

Cornel Marian
Cornel Marian

Reputation: 2503

switch(v) instead of temp....

also your temp variable must be a collection of int

DataRow row = MyData.Rows[0];
 int temp = Convert.ToInt32(row["Category"]);

foreach (int v in temp)
{
            switch (v)
            {
                case 1:
                    lblCategory.Text = temp + " - Complete.";
                    break;

                case 2:
                    lblCategory.Text = temp + " - Not Complete";
                    break;
           }
}

Upvotes: 0

Igor Ševo
Igor Ševo

Reputation: 5495

You need to have a collection of rows to loop through. You wrote a loop for a single element.

For the following line to work:

foreach (int v in temp) ...

your temp variable must be a collection of integers (for example IEnumerable<int> or List<int> or int[]).

Try writing:

var temp = MyData.Rows;

instead of

DataRow row = MyData.Rows[0];
int temp = Convert.ToInt32(row["Category"]);

before your foreach loop, like so:

var temp = MyData.Rows;
foreach (int v in temp)
{
    switch (v)
    {
    case 1:
        lblCategory.Text += v + " - Complete.\n";
        break;
    case 2:
        lblCategory.Text += v + " - Not Complete\n";
        break;
    }
}

Upvotes: 6

Related Questions