Nevron Gauge for SharePoint
Skip Navigation Links
Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Classes and fields
       
Author Reply
Prime b
posted 407 posts
since Dec 12, 2011 
from

Classes and fields

  Posted on: 03 Feb 2012       
Create a class named Car with auto-implemented properties
for the vehicle ID number, make, model, color, and value of a
Car object. Write a DisplayFleet( ) method that accepts any
number of Car objects, displays their values, and displays the
total value of all Car objects passed to the method. Write a
Main( ) method that declares fi ve Car objects and assigns values
to each, then calls DisplayFleet() three times—passing
three, four, and fi ve Car objects in successive calls..


This is as far as I could get... I am not sure what they mean by "accept any number of Car objects" , do I like have to create list, array or what?
What should I do next?


        private int vIdNumber;
        private string vMake;
        private int vModel;
        private string vColor;
        private double vValue;

        public int VidNumber { get; set; }
        public string Vmake { get; set; }
        public int Vmodel { get; set; }
        public string Vcolor { get; set; }
        public double VValue { get; set; }


        public void DisplayFleet(double value)
        {

        }
Practice makes it perfect && All science is either computer programming or stamp collecting
Maha
posted  809 posts
since  Aug 13, 2006 
from 

 Re: Classes and fields
  Posted on: 03 Feb 2012        0  
Please see

http://www.c-sharpcorner.com/Forums/Thread/154155/parallel-array.aspx



Vulpes
posted  5113 posts
since  Feb 28, 2011 
from 

 Re: Classes and fields
  Posted on: 03 Feb 2012   Accepted Answer     0  
The first point I'd make is that, if you have an auto-implemented property, then you don't need to declare a field as well. The C# compiler will provide a field but you won't be able to access this directly - you just use the property for everything.

It's not clear whether the DisplayFleet method is to be part of the Car class or part of the class containing the Main() method - my guess would be the latter. You can accept any number of Car objects by using a parameter array:

        public static void DisplayFleet(params Car[] cars)
        {
            double totalValue = 0;

            foreach(Car car in cars)
            {
                // code to display car values and add them up
             }

             // code to display total value        
         }

If c1, c2, c3 are Car objects, you could call this method as follows:

       DisplayFleet(c1, c2, c3);


Prime b
posted  407 posts
since  Dec 12, 2011 
from 

 Re: Classes and fields
  Posted on: 03 Feb 2012        0  
Oh hey vulpes, you still not bed, great!

This is what I have done using the link maha provided, I am guessing Maha has same book like I do. Except when i run in it gives me an error.

            Car[] car = new Car[6];
            car[0] = new Car(1111, "Buick", "x58", "red", 20000);
            car[1] = new Car(2222, "Lame", "car", "blue", 111);
            car[2] = new Car(3333, "Most lame", "Kinda", "pink", 1);
            car[3] = new Car(4444, "Mercedes", "C class", "Black", 30000);
            car[5] = new Car(5555, "BMW", "M5", "Black", 40000);


            DisplayFleet(car[0], car[1], car[2]);
            DisplayFleet(car[4], car[2], car[5]);
            DisplayFleet(car[3], car[2]);
        }
        public static void DisplayFleet(params Car[] carsArray)
        {
            double carSum = 0;
            double carPrice = 0;
            int index = 0;

            foreach (Car car in carsArray)
            {
                carSum = carSum + car.VValue;
                carPrice = car.VValue;
                Console.WriteLine("The price {0} of the car {1}", index, carPrice.ToString("C"));
                ++index;
            }
            Console.WriteLine("The sum of all the prices is {0}", carSum.ToString("C"));
            Console.WriteLine();



  class Car
    {


        public int VidNumber { get; set; }
        public string Vmake { get; set; }
        public string Vmodel { get; set; }
        public string Vcolor { get; set; }
        public double VValue { get; set; }


        public Car(int vidNumber, string vMake,string vModel,string vColor, double vValue)
        {
            this.VidNumber = vidNumber;
            this.Vmake = vMake;
            this.Vmodel = vModel;
            this.Vcolor = vColor;
            this.VValue = vValue;
           
        }
    }
Practice makes it perfect && All science is either computer programming or stamp collecting
Vulpes
posted  5113 posts
since  Feb 28, 2011 
from 

 Re: Classes and fields
  Posted on: 04 Feb 2012        0  
Hmm, it does look like you have the same book as Maha, something by Joyce Farrell IIRC.

Anyway, the error is due to car[4] not being initialized - it's still null.

An Audi would be nice :)
Prime b
posted  407 posts
since  Dec 12, 2011 
from 

 Re: Classes and fields
  Posted on: 04 Feb 2012        0  
ohhh I didn't even see it. haha audi would be nice!
Practice makes it perfect && All science is either computer programming or stamp collecting
       
Top Articles
View all »
6 Months Free & No Setup Fees ASP.NET Hosting!
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved