p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer (http://p2p.wrox.com/forumdisplay.php?f=574)
-   -   Lesson 13 - Try It...Seriously? (http://p2p.wrox.com/showthread.php?t=88793)

MinusZero November 13th, 2012 01:46 AM

Lesson 13 - Try It...Seriously?
 
Where do I start. This try it is incredibly frustrating. There is code present in the downloaded file that isnt referred to at all in the book. For example, this code:
Code:

private decimal GrandTotal = 0;
is not mentioned at all, yet I have to add to the grand total and display the new result (without having declared GrandTotal if you follow the book).

The problem (apart from lack of detail) is that in Form 1 in this code
Code:

//get new values
                ListViewItem lvi = itemsListView.Items.Add(frm.ItemName);
                lvi.SubItems.Add(frm.PriceEach.ToString("C"));
                lvi.SubItems.Add(frm.Quantity.ToString("C"));
                lvi.SubItems.Add(frm.TotalPrice.ToString("C"));

I get errors on ItemName, PriceEach, Quantity and TotalPrice even though I have declared them in the NewItemForm:

Code:

public Form1()
        {
            InitializeComponent();
        }
        public string ItemName;
        public decimal PriceEach, Quantity, TotalPrice;

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            ItemName = tbxItem.Text;
            PriceEach = decimal.Parse(tbxPriceEach.Text);
            Quantity = decimal.Parse(tbxQuantity.Text);

            TotalPrice = PriceEach * Quantity;
            tbxTotalPrice.Text=TotalPrice.ToString("C");

            btnOK.Enabled = true;

        }

------------------
This code from the downloaded Lesson 13 Try it contains NumberStyles.Any. When i try to type it, Intellisense doesnt find such an item.

Code:

PriceEach = decimal.Parse(priceEachTextBox.Text, NumberStyles.Any);
I have no idea what is happening and am at my wits end. The book seems to be becoming less helpful as more and more it seems to assume a novice coder has the knowledge to complete something that isnt shown. I might move to the next chapter and live without chapter 13. I dont think it will be a problem.

Oh and one more thing, the book asks you to create the NewItemForm first when you actually want the main form (Form1) to load first. Fortunately the internet helped me work out how to change which form starts first so I didnt have to remake the form.

Rod Stephens November 13th, 2012 11:41 AM

Hi MinusZero,

I'm sorry you're finding this TryIt frustrating. If you get stuck on something, like creating the first form, email me or post here and I'll try to get you going again as quickly as possible. (For example, when you needed to change the startup form.)

Sorry the instructions don't say to build the main form first. That is confusing. I think I was considering the dialog to be the more interesting part of this problem so I started with it first. (When I program, I sometimes do that. I would create the project, ignore the startup form for now, and create the dialog first.)

This is left over from previous examples.

Code:

ListViewItem lvi = itemsListView.Items.Add(frm.ItemName);
lvi.SubItems.Add(frm.PriceEach.ToString("C"));
lvi.SubItems.Add(frm.Quantity.ToString("C"));
lvi.SubItems.Add(frm.TotalPrice.ToString("C"));

The values in red are declared in the dialog. (This is the main reason I built the dialog first. This code can't work in the main form until the dialog is built.) On the top of page 170 it says to create those fields in the dialog form's code outside of any methods.

Because they are public and outside of the methods, the main form's code can access them if it has a variable that refers to a dialog form. In this case, frm refers to one of those forms so frm.PriceEach refers to the PriceEach field owned by that form.

Quote:

This code from the downloaded Lesson 13 Try it contains NumberStyles.Any. When i try to type it, Intellisense doesnt find such an item.
Sorry about that. It's used on page 136 but as System.Globalization.NumberStyles.Any. This example uses a "using System.Globalization" statement at the top so later it can use NumberStyles.Any without starting it with "System.Globalization."

Quote:

I have no idea what is happening and am at my wits end. The book seems to be becoming less helpful as more and more it seems to assume a novice coder has the knowledge to complete something that isnt shown. I might move to the next chapter and live without chapter 13. I dont think it will be a problem.
Again I'm sorry about that. The book does assume you know more as you progress but there do seem to be some things here that are a bit of a stretch.

Please email or post if you get stuck. The book should be challenging but it's not intended to be an exercise in pain and frustration.

MinusZero November 13th, 2012 06:44 PM

Thanks for your reply, I will let you know if I run into more problems.

John S June 10th, 2013 12:42 PM

I had similar problems when trying to figure this one out.

One mistake was here:

lvi.SubItems.Add(frm.Quantity.ToString("C"));

Quantity is not a Currency format, so there should not be a "C" in the brackets.

Removing the C made the error messages go away.

Also, the "using Sytem Globalization stumped me, until I watched the video for this Lesson, which covered it better.

Rod Stephens June 10th, 2013 02:17 PM

Sorry about the "C" thing. I don't know why that would cause error messages, though. It should just display the value as currency.

Yes, you should look at the videos, particularly if you're stumped. There's only so much room for details in the book.

John S June 10th, 2013 02:42 PM

But "Quantity" is not Currency

Rod Stephens June 10th, 2013 04:06 PM

The C just means "format this value as currency." If the value is an integer, like 2, it should produce "$2.00" I didn't think it would produce a warning or error. (But maybe it does. I'm still not at a computer so can't verify.)


All times are GMT -4. The time now is 12:28 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.