我如何使用contains和indexof? [英] How can I use contains and indexof?

查看:69
本文介绍了我如何使用contains和indexof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果drank.contains doenst似乎有效。有人可以帮我吗?



我尝试过:



 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();
列表< persoon> personen = admin.spelers();

foreach(Groepnaam的Groep naam)
{
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);
列表<得分> Punten = admin.punten();

foreach(在Punten得分)
{
listBox2.Items.Add(punt);



}
List< bier> consumptie = admin.Bier();

foreach(bier在消费中喝酒)
{
listBox4.Items.Add(drank);



}
if(drank.contains)
{


}




}

解决方案

首先,喝了在您的代码到达 foreach 循环结束时超出范围 - 所以无论它是什么,您都无法使用它 - 它不再存在。



其次,Contains和IndexOf需要一个实现IEnumerable< T>的类。所以除非 bier 类实际上是从List或类似的派生,或者你自己添加了一个Contains方法,否则你无法调用它。从上面的代码来看,我认为不是。

这就是你想要做的事情,我想:

  public   class 啤酒
{
public string 名称{ get ; set ; }
public int Abv { get ; set ; }
}
私有 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( 是的,{0}是啤酒。技术上。,b.Name);
}
Console.WriteLine( {0}位于索引{1},c.Name,beers.IndexOf(c));
...





Quote:< / div >排序好,我想检查一个来自sql数据库的值(int)。但是我在哪里可以使用contains方法来检查我的数据库中是否有一个例如10​​0的数字。



基本上,不要。



如果你想知道数据库中是否存在某些内容,请询问数据库!

  SELECT  COUNT(*) FROM  MyTable  WHERE  MyColumn =  100  

将它与ExecuteScalar一起使用,它将返回具有该值的项目数。



如果你已经将它们构建成 bier 对象的集合,那么请使用Linq Exists方法:

  if (beers.Exists(x = >  x.Abv =  3 ))
{
Console.WriteLine( 是,{0}是啤酒。技术上。,b.Name);
}



Contains方法只查找对集合中对象引用的对象引用的完全匹配,它不能查看对象内部


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)
            {


             }




        }

解决方案

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:

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!

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:

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.


这篇关于我如何使用contains和indexof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆