Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if drank.contains doenst seem to work. can someone help me with that?

What I have tried:

public partial class Form1 : Form
    {

        administration admin = new administration();
        //reserveer Reserveer = new reserveer();
        public Form1()
        {
            InitializeComponent();

            load();
        }


        public void load()
        {
            List<groep> Groepnaam = admin.groepsnaam();
            List<persoon> personen = admin.spelers();
            
            foreach (Groep naam in Groepnaam)
            {
                listBox3.Items.Add(naam.Groepsnaam );

            }
            foreach (persoon Persoon in personen)
            {
                listBox1.Items.Add(Persoon);
            }
        }
        

        
        private void button1_Click(object sender, EventArgs e)
        {
            int aantalstrikes = Convert.ToInt32(numstrikes.Value);
            int aantalspares = Convert.ToInt32(numspares.Value);
            int aantalpunten = Convert.ToInt32(tbpunten.Text);
            int aantalbier = Convert.ToInt32(aantbier.Text);
            int prijs = 2;
            

            admin.voegtoebier(aantalbier, prijs);
            
            admin.voegtoescore(aantalpunten, aantalstrikes, aantalspares);
            List<score> Punten = admin.punten();
            
            foreach (score punt in Punten)
            {
                listBox2.Items.Add(punt);
                


            }
            List<bier> consumptie = admin.Bier();
            
            foreach(bier drank in consumptie)
            {
                listBox4.Items.Add(drank);
                


            }
              if (drank.contains)
            {


             }




        }
Posted
Updated 4-Jan-18 5:17am
v2
Comments
David_Wimbley 4-Jan-18 11:36am    
Your code isn't terribly clear as you are doing .contains (should be .Contains) on a variable that is used within a foreach loop...but you are accessing it outside of your foreach loop.

Regardless, I think you might want to look at using .Any(.

1 solution

First off, drank is out of scope by the time your code gets to the end of the foreach loop - so no matter what it was, you can't use it - it no longer exists.

Secondly, Contains and IndexOf require a class that implements IEnumerable<T> so unless the bier class is actually a derived from a List or similar, or you added a Contains method to it yourself, you can't call it. And from the code above that, I don't think it is.
This is what you are trying to do, I think:
C#
public class Beer
    {
    public string Name { get; set; }
    public int Abv { get; set; }
    }
private void MyButton_Click(object sender, EventArgs e)
    {
    List<Beer> beers = new List<Beer>();
    Beer a = new Beer() { Name = "Shimptons Old Cleanser", Abv = 17 };
    beers.Add(a);
    Beer b = new Beer() { Name = "Horse Urine", Abv = 3 };
    beers.Add(b);
    Beer c = new Beer() { Name = "Heinekon", Abv = 1 };
    beers.Add(c);
    if (beers.Contains(b))
        {
        Console.WriteLine("Yes, {0} is a beer. Technically.", b.Name);
        }
    Console.WriteLine("{0} is at index {1}", c.Name, beers.IndexOf(c));
...


Quote:</div>well sort off, i want to check a value (int) that comes from a sql database. but where can i use the contain method to check if there is an number for example 100 in my database.

Basically, don't.

If you want to know if something exists in a DB, ask the DB!
SQL
SELECT COUNT(*) FROM MyTable WHERE MyColumn = 100
Use that with ExecuteScalar, and it will return the number of items with that value.

If you have built them into a collection of bier objects, then use the Linq Exists method instead:
C#
if (beers.Exists(x => x.Abv = 3))
    {
    Console.WriteLine("Yes, {0} is a beer. Technically.", b.Name);
    }

The Contains method only looks for an exact match on an object reference to an object reference in the collection, it can't look at the object internals.
 
Share this answer
 
v2
Comments
Member 13554627 4-Jan-18 11:57am    
well sort off, i want to check a value (int) that comes from a sql database. but where can i use the contain method to check if there is an number for example 100 in my database.


public void voegtoebier(int aantal, int prijs)
{
data.Open();
string query = "INSERT INTO Bier (Aantal_consumpties,Prijs) VALUES(@Aantal_consumpties,@Prijs)";
SqlCommand cmd = new SqlCommand(query, data);
cmd.Parameters.AddWithValue("@Aantal_consumpties", aantal);
cmd.Parameters.AddWithValue("@Prijs", prijs);

cmd.ExecuteNonQuery();
data.Close();
public List<bier> Bier()
{
SqlConnection data = new SqlConnection(Connectionstring);
data.Open();
string query = "select sum(Aantal_consumpties*Prijs) as Rekeninggroep From Bier";

SqlCommand cmd = new SqlCommand(query, data);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{

bier biertje = new bier();

// Lees de gegevens van de student in.
biertje.voegtoebier(reader.GetInt32(0));


// En voeg deze toe aan de lokale lijst.
biertjes.Add(biertje);

}
}
data.Close();
return biertjes;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900