Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2011, 01:53 AM
Registered User
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 6 Exercise 4

Hi all, I'm trying to work out a solution for exercise 4, but I think I'm making some very elementary mistakes. The answer in the back of the book is much shorter, but I'd like to understand what I'm doing wrong here if possible.

This is my code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args);
        
     struct order
     {
         public string itemName;
         public int unitCount;
         public double unitCost;
         public double totalCost() {return unitCost * unitCount;}
     }
    }
    Console.WriteLine("This function calculates the total cost of all units.");
    itemName myItem;
    Console.WriteLine("Please enter the name of the item you would like to purchase.");
    myItem = console.ReadLine();
    Console.WriteLine("Thank you. Now please enter the number of items you would like to purchase.");
    unitCount myCount;
    myCount = Console.ReadLine();
    Console.WriteLine("Thank you. Now please enter the cost of a {0}.", myItem);
    unitCost = myCost;
    myCost = Console.ReadLine();
    totalCost = myTotal;
    Console.WriteLine("Thank you! The total cost of your {0} orders of item {1} is {2}.", myCount, myItem, myTotal);
    Console.ReadKey();
     
}
This is the error message I'm getting:

A namespace cannot directly contain members such as fields or methods (line 20)

Thanks for all your help! :)
 
Old December 29th, 2011, 04:08 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default

I made some changes to your source code and that works fine for me
please check the code below
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        struct Order
        {
            public string itemName;
            public int unitCount;
            public double unitCost;
            public double totalCost()
            { return unitCost * unitCount;
            }
        }
        static void Main(string[] args)
        {
      
            Order myOrder;
            Console.WriteLine("Please enter the name of the item you would like to purchase.");
            myOrder.itemName = Console.ReadLine();
            Console.WriteLine("Thank you. Now please enter the number of items you would like to purchase.");
            myOrder.unitCount = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Thank you. Now please enter the cost of a {0}.", myOrder.itemName);
            myOrder.unitCost =Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Thank you! The total cost of your {0} orders of item {1} is {2}.", myOrder.unitCount, myOrder.itemName,myOrder.totalCost());
            Console.ReadKey();

        }
    }
}
In your code u defined itemName myItem;, am also confused on this,Is it possible to declare a variable like this?

You defined the struct in main function,Its better to place outside the main.

Also without declaring a struct variable is it possible to access its members?

syntax for calling a function is not like this in your code


please don't mark this as answer because I am also a beginner like you so someone else also verify this one.
 
Old December 30th, 2011, 04:02 PM
Registered User
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply! That code does work. However, just moving the Main line doesn't fix my code, so I'm going to have to look this up a bit more...

This method of declaring variables is used a few times in the book, such as the Try It Out on page 47, for instance. It just splits the declaration and the definition in two lines, like this:
int myInt;
myInt = 5;

as opposed to writing myInt = 5;

So your code is also more efficient, since it's nice and short! :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter ! - Exercise 4 tinmegali BOOK: Beginning Flash, Flex, and AIR Development for Mobile Devices 0 November 25th, 2011 09:18 PM
Chapter 2 Exercise 2 rolndd BOOK: Ivor Horton's Beginning Visual C++ 2010 2 July 12th, 2011 01:58 PM
Chapter 3 Exercise 2 rouzbeh_ziafati BOOK: Beginning Microsoft Visual Basic 2010 3 March 14th, 2011 07:21 AM
Chapter 7 - Exercise [email protected] BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 2 March 16th, 2010 07:32 PM
Chapter 8 exercise 3 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 8 March 4th, 2010 04:15 AM





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