Solved

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

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

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now