Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created my own class for a book and need to analyze the array for whether or not it contain true or false and then add 5 to anything that contains true. This is the class
```
 class Book
    {
        public double Price { get; set; }
        public string BookName { get; set; }
        public bool Hardcover { get; set; }

        public Book(string bookname, bool hardcover, double price)
        {
            Price = price;
            BookName = bookname;
            Hardcover = hardcover;
        }
        public override string ToString()
        {
            if (Hardcover == true)
            {
                return BookName + Price + Hardcover;
            }
            else
            {
                return BookName + Price + Hardcover;
            }

        }
```
thx for the help


What I have tried:

My current solution is to use linq but it still hasnt worked out, i also tried string compare
```  public Form1()
        {
            InitializeComponent();
        }
        List<Book> MyBooks = new List<Book>(); 
        private void Form1_Load(object sender, EventArgs e)
        {
            MyBooks.Add(new Book("Book 1", false, 5.50));
            MyBooks.Add(new Book("Book 2", false, 5.50));
            MyBooks.Add(new Book("Book 3", false, 5.50));
            MyBooks.Add(new Book("Book 4", false, 5.50));
            MyBooks.Add(new Book("Book 5", true, 5.50));
        }
        private void Grid_Click(object sender, EventArgs e)
        {
            Button c = (Button)sender;

            List<Book> MyBooks = new List<Book>();

            IEnumerable<String> bookQuery =
                from book in MyBooks
                where book is true
                select book;

            foreach (String p in MyBooks)
            {
                textBox1.Text = textBox1.Text + p.ToString() + Environment.NewLine;
            }
        }
```
Posted
Updated 5-Aug-20 2:34am

List<Book> MyBooks = new List<Book>();

            IEnumerable<String> bookQuery =
                from book in MyBooks
                where book is true
                select book;


You're creating a new empty List and then selecting from it so you're never going to get any results as it is empty. The "MyBooks" above is different from the "MyBooks" you defined at class level that you added to Load. As the MyBooks you want is already defined and populated, simply use that version.

private void Grid_Click(object sender, EventArgs e)
        {
            Button c = (Button)sender;

            IEnumerable<String> bookQuery =
                from book in MyBooks
                where book is true
                select book;


When you do code like above it uses the MyBooks at class level, but in your original code you created a second MyBooks at the Grid_Click level, this is a concept known as variable scoping.

Also don't use "double" for prices, use "decimal". Double is an approximation of your number and can result in weird computational errors you're not expecting which can be catastrophic when dealing with money. Using decimal ensures all your values and calculations are precise.
 
Share this answer
 
v2
Comments
vikil chandrapati 5-Aug-20 7:32am    
i cant use decimal cause for whatever reason i cant input decimal numbers
To add to what F-ES Sitecore has said:
1) Your query is currently redundant anyway, as you ignore the results:
C#
IEnumerable<String> bookQuery =
    from book in MyBooks
    where book is true
    select book;

foreach (String p in MyBooks)
{
    textBox1.Text = textBox1.Text + p.ToString() + Environment.NewLine;
}
Probably, you need to change the loop to this:
C#
foreach (String p in bookQuery)
which uses the results rather than the input.

2) Don't concatenate strings - it gets very inefficient pretty quickly. Remember that strings are immutable, which means they can't be changed once created. So each time you add some more text to a string, you create a new string big enough for both "bits of text". Use a StringBuilder instead. You also don;'t need to convert a string to a string ... ever.
C#
StringBuilder sb = new StringBuilder();
foreach (String p in MyBooks)
{
    sb.AppendLine(p);
}
textBox1.Text = sb.ToString();
 
Share this answer
 

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