Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# application error "Parameter Is Not Valid". 3 75
C# Offline Apllication 5 51
Change Format on Data when exporting to Excel 4 22
Connection String 16 42
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!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

16 Experts available now in Live!

Get 1:1 Help Now