Solved

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

Posted on 2014-10-02
1
161 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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