简体   繁体   English

Visual Studio C#通用哈希表字典如果if语句错误?

[英]Visual Studio C# generic hash table dictionary wrong if statements?

I am creating a GUI in Visual Studio where a Dictionary collection will be created. 我在Visual Studio中创建一个GUI,将在其中创建Dictionary集合。 This will contain instances of book classes which will contain the string members Title, ISBN and a Boolean variable called Onloan. 这将包含书籍类的实例,这些实例将包含字符串成员Title,ISBN和一个名为Onloan的布尔变量。 these members will be entered by the user and submitted into the dictionary. 这些成员将由用户输入并提交到字典中。 The user can also search books they have entered and change their loan status. 用户还可以搜索他们输入的书籍并更改其借阅状态。

I want the program to be able to search for books even if the user only enters the ISBN or the Title, currently it only works when both a Title and ISBN are searched for together. 我希望该程序即使用户仅输入ISBN或书名也能够搜索书籍,当前它仅在同时搜索书名和ISBN时才有效。 if the user only searches for an ISBN, the Title in the search results text box is blank and if the user only searches the Title the ISBN search results text box states "temp" which is the temporary value I used in a temporary book I used to search. 如果用户仅搜索ISBN,则搜索结果文本框中的标题为空白,并且如果用户仅搜索ISBN,则ISBN搜索结果文本框说明“ temp”,这是我在使用的临时书中使用的临时值搜索。

I've have been changing the program for some time trying to figure out why this happens. 我一直在更改程序一段时间,试图弄清楚为什么会发生这种情况。 I'm wondering if someone can explain it. 我想知道是否有人可以解释它。 Maybe I have written the if statements incorrectly. 也许我写错了if语句。

Book Class 书类

class Books
    {
        private String isbn;
        private string title;
        private Boolean onloan;

        public Books(string isbn, string title)
        {
            this.isbn = isbn;
            onloan = false;
        }
        public string ISBN
        {
            get { return this.isbn; }
            set { this.isbn = value; }
        }
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }
        public Boolean Onloan
        {
            get { return this.onloan; }
            set { this.onloan = value; }
        }
    }

Form: 形成:

public partial class Form1 : Form

     {


       Dictionary<string, Books> Library = new Dictionary<string, Books>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)   //submit button
        {
            if ((String.IsNullOrWhiteSpace(TitleBox.Text) == false && String.IsNullOrWhiteSpace(ISBNBox.Text) == false) || 
                (String.IsNullOrEmpty(TitleBox.Text) == false && String.IsNullOrEmpty(ISBNBox.Text) == false))
            {
                Books LibBook = new Books(ISBNBox.Text, TitleBox.Text);
                if(LoanRadial.Checked == true)
                    {
                    LibBook.Onloan = true;
                    LoanRadial.Checked = false;
                    }

                Library.Add(ISBNBox.Text, LibBook);
                TitleBox.Clear();
                ISBNBox.Clear();
                //int count = Library.Count();
                //ISBNBox.Text = count.ToString();

            }
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            Library.Remove(ISBNBox.Text);
            TitleBox.Clear();
            ISBNBox.Clear();
            if (LoanRadial.Checked == true)
            {
                LoanRadial.Checked = false;
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            string tempstring = "temp";
            Books tempbook = new Books(tempstring, tempstring);
            if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == false && String.IsNullOrWhiteSpace(TitleBox.Text) == true) ||
                (String.IsNullOrEmpty(ISBNBox.Text) == false && String.IsNullOrEmpty(TitleBox.Text) == true))
            {

                tempbook.ISBN = ISBNBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.ISBN == element.Value.ISBN)
                    {
                        tempbook.Title = element.Value.Title;
                        tempbook.Onloan = element.Value.Onloan;
                    }
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }

            }
            else if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == true && String.IsNullOrWhiteSpace(TitleBox.Text) == false) ||
                (String.IsNullOrEmpty(ISBNBox.Text) == true && String.IsNullOrEmpty(TitleBox.Text) == false))
            {
                tempbook.Title = TitleBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.Title == element.Value.Title)
                    {

                        tempbook.ISBN = element.Value.ISBN;
                        tempbook.Onloan = element.Value.Onloan;
                    }
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }
            }
            else if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == false && String.IsNullOrWhiteSpace(TitleBox.Text) == false) ||
                    (String.IsNullOrEmpty(ISBNBox.Text) == false && String.IsNullOrEmpty(TitleBox.Text) == false))
            {

                tempbook.ISBN = ISBNBox.Text;
                tempbook.Title = TitleBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if ((tempbook.Title == element.Value.Title) &&  (tempbook.ISBN == element.Value.ISBN) )
                    {
                        tempbook.Onloan = element.Value.Onloan;
                    }                   
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }

            }
        }

        private void Changeloan_Click(object sender, EventArgs e)
        {
            string tempstring = "temp";
            Books tempbook = new Books(tempstring, tempstring);
            if ((String.IsNullOrWhiteSpace(ISBN2.Text) == false && String.IsNullOrWhiteSpace(Titlebox2.Text) == false) ||
                (String.IsNullOrEmpty(ISBN2.Text) == false && String.IsNullOrEmpty(Titlebox2.Text) == false))
            {
                tempbook.ISBN = ISBN2.Text;
                tempbook.Title = Titlebox2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if ((tempbook.ISBN == element.Value.ISBN) && (tempbook.Title == element.Value.Title))
                    {

                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = tempbook.Title + " Is not on loan";
                        }
                        else
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = tempbook.Title + " Is not on loan";
                        }
                    }
                }
            }                  
            else if ((String.IsNullOrWhiteSpace(ISBN2.Text) == true && String.IsNullOrWhiteSpace(Titlebox2.Text) == false) ||
                (String.IsNullOrEmpty(ISBN2.Text) == true) && (String.IsNullOrEmpty(Titlebox2.Text) == false))
            {
                tempbook.Title = Titlebox2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if(tempbook.Title == element.Value.Title)
                    {
                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = element.Value.Title + " Is not on loan";

                        }
                        else if (element.Value.Onloan == false)
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = element.Value.Title + " Is on loan";
                        }
                    }
                }

                    }
            else if((String.IsNullOrWhiteSpace(ISBN2.Text) == false) && (String.IsNullOrWhiteSpace(Titlebox2.Text) == true) || 
                (String.IsNullOrEmpty(ISBN2.Text) == false) && (String.IsNullOrEmpty(Titlebox2.Text) == true))
            {
                tempbook.ISBN = ISBN2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.ISBN == element.Value.ISBN)
                    {
                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = element.Value.Title + " Is not on loan";

                        }
                        else if (element.Value.Onloan == false)
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = element.Value.Title + " Is on loan";
                        }
                    }
                }
            }

            }
        }

Books form picture 书籍形式图片

You are using a Dictionary , but more as a list of tuples than a proper hash table. 您使用的是Dictionary ,而不是适当的哈希表,而更多地用作元组列表。 I'm assuming your key is the ISBN, in which case you might get the book from the dictionary like this: 我假设您的密钥是ISBN,在这种情况下,您可以像这样从字典中获取这本书:

String isbn = ISBNBox.Text; //Assuming the TextBox.Text is valid
if(Library.ContainsKey(isbn))
{
    Books book = Library[isbn];
    //Do whatever you want to do with the book
}

Addressing the other aspect of your problem, where the text is being set to the tempbook ISBN "temp", any time the title or ISBN entered are not found in the Library, they will be set to the temp values. 解决您问题的另一方面, tempbook文本设置为tempbook ISBN“ temp”,只要在库中找不到输入的标题或ISBN,它们就会被设置为temp值。 This is because you are setting the textbook values 这是因为您正在设置教科书的值

Titlebox2.Text = tempbook.Title;
ISBN2.Text = tempbook.ISBN;

even if the book is not found in the dictionary. 即使在词典中找不到这本书。 Consider having some case for when the book is not found. 考虑为找不到书时提供一些理由。

If the ISBN is indeed your key, then if they provide one then don't need to worry about the title. 如果ISBN确实是您的密钥,那么如果他们提供了ISBN,则不必担心标题。 You really only need to iterate through the dictionary if they only give you the title. 如果他们只给您标题,您实际上只需要遍历字典。 Also, you can do your setting of the UI properties at the end to reduce some of the redundant code. 另外,您可以在最后设置UI属性,以减少一些冗余代码。 Here's a bit of a refactor with a place for code to handle when a book isn't found: 以下是一些重构,其中包含找不到书的代码处理位置:

private void Search_Click(object sender, EventArgs e)
{
    Books tempbook = null;
    if (String.IsNullOrWhiteSpace(ISBNBox.Text) == false && String.IsNullOrEmpty(ISBNBox.Text) == false)
    {
        String isbn = ISBNBox.Text;
        if(Library.ContainsKey(isbn)){
            tempbook = Library[isbn];
        }

    }
    else if (String.IsNullOrWhiteSpace(TitleBox.Text) == false && String.IsNullOrEmpty(TitleBox.Text) == false)
    {
        String title = TitleBox.Text;
        foreach (KeyValuePair<string, Books> element in Library)
        {
            if (element.Value.Title == title)
            {

                tempbook = element.Value;
                break;
            }
        }
    }    
    if(tempbook != null)
        Titlebox2.Text = tempbook.Title;
        ISBN2.Text = tempbook.ISBN;
        if (tempbook.Onloan == true)
        {
            LoanBox.Text = tempbook.Title + " Is on loan";
        }
        else
        {
            LoanBox.Text = tempbook.Title + " Is not on loan";
        }

    } else {
    //Handle case where book is not found
    }
}

Also, your class describing a book is called Books , but since a single object of the class would be just one book, it would probably be better named simply Book . 同样,您描述一本书的类称为Books ,但由于该类的单个对象只是一本书,因此最好将其命名为Book

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM