C# 使用另一个类中的bool方法搜索列表框。获得';并非所有代码路径都返回值';。知道为什么吗?

C# 使用另一个类中的bool方法搜索列表框。获得';并非所有代码路径都返回值';。知道为什么吗?,c#,class,methods,listbox,boolean,C#,Class,Methods,Listbox,Boolean,更多的细节在这个底部。我的代码: 表格类别: public partial class Form1 : Form { public ShoppingBasket myBasket = new ShoppingBasket(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {

更多的细节在这个底部。我的代码:

表格类别:

public partial class Form1 : Form
{
    public ShoppingBasket myBasket = new ShoppingBasket();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (myBasket.IsProductInBasket("Apples"))
        {
            MessageBox.Show("Yes");
        }
        else
        {
            MessageBox.Show("No");
        }
    }
}
OrderItem类:

public class OrderItem
{
    public string ProductName { get; set; } 
    public decimal LatestPrice { get; set; }
    public int Quantity { get; set; }
    public decimal TotalOrder { get; set; }

    public OrderItem(string productName, decimal latestPrice, int quantity)
    {
        ProductName = productName;
        LatestPrice = latestPrice;
        Quantity = quantity;
        TotalOrder = latestPrice * quantity;
    }
}
购物类:

public class ShoppingBasket : List<OrderItem>
{
    public ShoppingBasket()
    {

    }

    public Form1 fm1;

    public bool IsProductInBasket(string productName) //Error of " 'ShoppingBasket.IsProductInBasket(string)': not all code paths return a value"
    {
        if (fm1.lstCart.Items.Count > 0)
        {
            for (int i = 0; i < fm1.lstCart.Items.Count; i++) // Warning of 'Unreachable code detected'
            {
                if (fm1.lstCart.Items[i].ToString().Contains(productName))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {
            return false;
        }
    }
}
公共类购物篮:列表
{
公共购物篮()
{
}
公共表格1 fm1;
public bool IsProductInBasket(string productName)//出现错误“'ShoppingBasket.IsProductInBasket(string)':并非所有代码路径都返回值”
{
如果(fm1.lstCart.Items.Count>0)
{
for(int i=0;i

为什么我会犯这样的错误?IsProductInBasket将始终返回true或false,列表框中的值永远不能为负值,因此如果计数为0,则返回false,如果计数为0,则返回false,如果计数为0,则返回listbox,如果找到则返回true,如果未找到则返回false。

如果
语句返回true,但是循环没有可迭代的内容,您的方法将永远不会返回任何内容。
如果另一个线程修改列表,则可能发生这种情况

您应该完全去掉外部
if
/
else
,只需在循环后
返回false即可

另外,您不希望在内部的
else
返回false

现在,如果第一个产品不匹配,您的循环将立即停止,而不检查其他项目。

如果
if
语句返回true,但循环没有可迭代的内容,则您的方法将永远不会返回任何内容。
如果另一个线程修改列表,则可能发生这种情况

您应该完全去掉外部
if
/
else
,只需在循环后
返回false即可

另外,您不希望在内部的
else
返回false

现在,如果第一个产品不匹配,循环将立即停止,而不检查其他项目。

这是代码中的两个错误。frm1未在代码中的任何位置初始化。你会在这里得到例外

 if (fm1.lstCart.Items.Count > 0) //Object reference
您应该像这样更改代码

  for (int i = 0; i < fm1.lstCart.Items.Count; i++) 
        {
            if (fm1.lstCart.Items[i].ToString().Contains(productName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
   return false;
for(int i=0;i
这是代码中的两个错误。frm1未在代码中的任何位置初始化。你会在这里得到例外

 if (fm1.lstCart.Items.Count > 0) //Object reference
您应该像这样更改代码

  for (int i = 0; i < fm1.lstCart.Items.Count; i++) 
        {
            if (fm1.lstCart.Items[i].ToString().Contains(productName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
   return false;
for(int i=0;i
您在哪里初始化了frm1?@EhsanUllah我以为“public Form1 fm1;”初始化了它?我将如何初始化它?感谢您的帮助:)您需要了解更多关于面向对象编程的知识。您还需要了解循环(失败)是如何工作的。我是OO编程新手,所以是的,我知道,关于学习的最佳方法有什么建议吗?您在哪里初始化过frm1?@EhsanUllah我认为“public Form1 fm1;”初始化了它?我将如何初始化它?感谢您的帮助:)您需要了解更多关于面向对象编程的知识。你还需要了解你的循环(失败)是如何工作的。我是OO编程新手,所以是的,我知道,关于学习的最佳方式有什么建议吗?@liam.burns:没有;它声明了一个字段,该字段将等于
null
。您需要将表单传递给构造函数并设置字段。但是,此问题与编译器错误无关。它在代码中始终为null。你在代码中的任何地方都像frm1=new Form1()一样初始化它吗?@EhsanUllah:这不会满足他的要求;他不想要新的形式。@EhsanUllah:那是因为我回答的第二个问题;循环总是从第一次迭代返回。@liam.burns:No;它声明了一个字段,该字段将等于
null
。您需要将表单传递给构造函数并设置字段。但是,此问题与编译器错误无关。它在代码中始终为null。你在代码中的任何地方都像frm1=new Form1()一样初始化它吗?@EhsanUllah:这不会满足他的要求;他不想要新的形式。@EhsanUllah:那是因为我回答的第二个问题;循环总是从第一次迭代中返回。我如何使它具有可迭代的内容?您没有。相反,即使循环没有运行,也要修复代码以返回一个值。阅读我的答案的其余部分。我该如何使循环具有可迭代的内容?你没有。相反,即使循环没有运行,也要修复代码以返回一个值。阅读我的其余答案。