Click here to Skip to main content
14,699,489 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I dont know whats wrong but all of a sudden this code stopped working, the compiler throws out no errors and the problem seems to come from out of nowhere. when i click button 1 it runs this code which takes the tag of the button and runs it through the array to see if there any matches, and if there is a match than it outputs the array it matches into a textbox.

What I have tried:

This line of code
Arraytxt.Text = Arraytxt.Text + book.ToString() + Environment.NewLine;

is displaying a different result to what the rest of the code was displaying when you use a breakpoint
<pre lang="c#">private void Form1_Load(object sender, EventArgs e)
        {
            //Book Array
            MyBooks.Add(new Book("Book 1", 15));
            MyBooks.Add(new Book("Manga vol 1-15", 39));
            MyBooks.Add(new Book("Novels 1-199", 19));
            MyBooks.Add(new Book("Novels 200-400", 35));
            MyBooks.Add(new Book("Comics series mainstream", 19));
            MyBooks.Add(new Book("Comics series secondary", 10));
            MyBooks.Add(new Book("Text book 1 semester/2 modules", 40));
            MyBooks.Add(new Book("Text book module add-ons", 15));
            MyBooks.Add(new Book("Harcover", 3));
            dataGridView1.Columns["Price"].Visible = false;
        }
private void BtnGlobal_Click(object sender, EventArgs e)
foreach (Book book in MyBooks)
                {
                    if (BtnGlobal.Text == book.BookName)
                    {
                        Arraytxt.Text = Arraytxt.Text + book.ToString() + Environment.NewLine;
                    }
                }
Posted
Updated 13-Aug-20 2:33am

1 solution

Unlikely.
That does exactly what I would expect it to do:
private class Book
    {
    public string BookName { get; set; }
    public int Pages { get; set; }
    public Book(string s, int p)
        {
        BookName = s;
        Pages = p;
        }
    }

List<Book> MyBooks = new List<Book>();
MyBooks.Add(new Book("Book 1", 15));
MyBooks.Add(new Book("Manga vol 1-15", 39));
MyBooks.Add(new Book("Novels 1-199", 19));
MyBooks.Add(new Book("Novels 200-400", 35));
MyBooks.Add(new Book("Comics series mainstream", 19));
MyBooks.Add(new Book("Comics series secondary", 10));
MyBooks.Add(new Book("Text book 1 semester/2 modules", 40));
MyBooks.Add(new Book("Text book module add-ons", 15));
MyBooks.Add(new Book("Harcover", 3));
Label Arraytxt = new Label();
foreach (Book book in MyBooks)
    {
    if (BtnGlobal.Text == book.BookName)
        {
        Arraytxt.Text = Arraytxt.Text + book.ToString() + Environment.NewLine;
        }
    }
Displays the text I expect:
OneOffJobs.FrmMain+Book

If you are expecting it to show the title of the book then you need to override ToString in your Book class:
public override string ToString()
    {
    return $"{BookName}:{Pages}";
    }

How you will get:
Novels 200-400:35
   
Comments
OriginalGriff 13-Aug-20 8:07am
   
Possibly because you misspelled "Hardcover" when you added the items? :laugh:

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