Optical
Optical

Reputation: 73

c# add integer to list

When I check if a textbox equals 1, I want to check if the list allready contains a specific value. When I run this, it allways goes to the else code. What am I doing wrong?

List<int> list = new List<int>();

if (Convert.ToInt32(DobbelWaarde.Text) == 1)
{
    if (list.Contains(1))
    {
        Console.WriteLine("1 is allready been chosen");
    }
    else
    {
        list.Add(1);
        Console.WriteLine();
        foreach (int li in list)
        {
            Console.WriteLine(li);
            Console.WriteLine("We add 1");
        }
    }
}

Upvotes: 4

Views: 51571

Answers (6)

maximpa
maximpa

Reputation: 1988

The first line initialises an empty list:

List<int> list = new List<int>();

The list is empty so Contains() returns false, no matter what the value you check for is.

To initialise the list you could use:

List<int> list = new List<int> { 1 };

More details are here: Object and Collection Initializers (C# Programming Guide)

Upvotes: 12

Đức B&#249;i
Đức B&#249;i

Reputation: 527

I don't get ur code !

First ur list have no element

Your code , it may be like this:

 List<int> list = new List<int>();
    list.Add(1);
    list.Add(2);
    list.Add(3);
    list.Add(4);

 int number = Convert.ToInt32(DobbelWaarde.Text);

        if ( number == 1)
        {
            if (list.Contains(1))
            {
                Console.WriteLine(number + " is allready been chosen");
            }
            else
            {
                list.Add(number );
                Console.WriteLine();
                foreach (int li in list)
                {
                    Console.WriteLine(li);
                    Console.WriteLine("We add " + number);
                }
            }
        }

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460108

You initialize the list always before you check if it contains that number. That will clear the list. So you should move the initialization for example into the constructor of your class:

class MyClass
{
    List<int> list;
    public MyClass()
    {
        list  = new List<int>();
    }

    private void Validate()
    {
        if (Convert.ToInt32(DobbelWaarde.Text) == 1)
        {
            if (list.Contains(1))
            {
                Console.WriteLine("1 is allready been chosen");
            }
            else
            {
                list.Add(1);
                // ...
    }
}

Upvotes: 1

Ria
Ria

Reputation: 10347

Define your list outer checkTextBox method:

List<int> list = new List<int>();

void CheckTextBox()
{
    if (Convert.ToInt32(DobbelWaarde.Text) == 1)
    {
        if (list.Contains(1))
        {
            Console.WriteLine("1 is allready been chosen");
        }
        else
        {
            list.Add(1);
            Console.WriteLine();
            foreach (int li in list)
            {
                Console.WriteLine(li);
                Console.WriteLine("We add 1");
            }
        }
    }
}

Upvotes: 0

kiran
kiran

Reputation: 1072

condition in the if statement is always evaluated to false i.e list doesn't have 1 so it returns false hence else part gets executed.

Upvotes: 0

Habib
Habib

Reputation: 223247

When I run this, it allways goes to the else code.

You are not adding integer in your list anywhere. You start with an empty List and then check if it contains 1 that is why it goes to the else part.

You may initialize the list like:

List<int> list = new List<int>() {1};

Upvotes: 4

Related Questions