Solved

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

Posted on 2014-10-02
1
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

738 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