Solved

Calling a class that contain a Array list ... Need help

Posted on 2014-10-02
1
154 Views
Last Modified: 2014-10-02
I have this class bellow Product and I was wondering how can I call it from a  static void Main(string[] args)
        {
            Product pm = new Product("Yves", 10.1m);

         would like to call it in a window console that contain a list of all the product?
         
        }

 class Product
    {
        string name;
        public string Name { get { return name; } }

        decimal price;
        public decimal Price { get { return price; } }

        public Product(string name, decimal price)
        {
            this.name = name;
            this.price = price;

        }

        public static ArrayList GetSampleProduct() {
            ArrayList newlist = new ArrayList();
            newlist.Add(new Product("Scary movie", 9.99m));
            newlist.Add(new Product("Scary movie1", 10.1m));
            newlist.Add(new Product("Scary movie2", 11.1m));
            return newlist;
        
        }
        public override string ToString()
        {
            return string.Format("{0}: {1}", name, price);
        }
    
    }

Open in new window



I just want to be able to call the newlist from the console window...
0
Comment
Question by:yguyon28
1 Comment
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40356907
I am not quite understanding your question.

Are you wanting to build a list that contains products:
using System;
using System.Collections;

namespace ProductsExample
{
	class Program
	{
		static ArrayList products = new ArrayList();
		static void Main(string[] args)
		{
			products.Add(new Product("Yves", 10.1m));
			products.AddRange(Product.GetSampleProduct());
			foreach (Product product in products)
				Console.WriteLine(product.ToString());
			Console.ReadLine();
		}
	}

	class Product
	{
		string name;
		public string Name { get { return name; } }

		decimal price;
		public decimal Price { get { return price; } }

		public Product(string name, decimal price)
		{
			this.name = name;
			this.price = price;
		}

		public static ArrayList GetSampleProduct()
		{
			ArrayList newlist = new ArrayList();
			newlist.Add(new Product("Scary movie", 9.99m));
			newlist.Add(new Product("Scary movie1", 10.1m));
			newlist.Add(new Product("Scary movie2", 11.1m));
			return newlist;
		}

		public override string ToString()
		{
			return string.Format("{0}: {1}", name, price);
		}
	}
}

Open in new window


For type safety I would recommend the usage of a list as opposed to an ArrayList though:
using System;
using System.Collections.Generic;

namespace ProductsExample
{
	class Program
	{
		static List<Product> products = new List<Product>();
		static void Main(string[] args)
		{
			products.Add(new Product("Yves", 10.1m));
			products.AddRange(Product.GetSampleProduct());
			foreach (Product product in products)
				Console.WriteLine(product.ToString());
			Console.ReadLine();
		}
	}

	class Product
	{
		string name;
		public string Name { get { return name; } }

		decimal price;
		public decimal Price { get { return price; } }

		public Product(string name, decimal price)
		{
			this.name = name;
			this.price = price;
		}

		public static List<Product> GetSampleProduct()
		{
			return new List<Product>() { new Product("Scary movie", 9.99m), new Product("Scary movie1", 10.1m), new Product("Scary movie2", 11.1m) };
		}

		public override string ToString()
		{
			return string.Format("{0}: {1}", name, price);
		}
	}
}

Open in new window


-saige-
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question