CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2011
    Posts
    1

    [Help with a project]Problem with ArrayList

    Hello,i m trying to build a console app using monodevelop.The program has 3 main options: let users insert products under three
    categories..Laptops,smartphones and desktop pcs , then u have the option to sell a product and the third option is to print on console all the available products .
    I m basically stack on how to save data...i first tried to use array but i the reallized that i need dynamic array...and now i try to do it with ArrayList. But in ths line :
    Code:
    public static void addLAP() {
    			Console.WriteLine("Model's name:"); LaptopsList.Add( new Laptops {Model=Console.ReadLine()});
    			Console.WriteLine("CPU:");          LaptopsList.Add( new Laptops {CPU=Console.ReadLine()});
    			Console.WriteLine("RAM:");          LaptopsList.Add( new Laptops {RAM=Console.ReadLine()});
    i get static field or property cannot be assigned in an object initializer error
    Any thoughts????

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: [Help with a project]Problem with ArrayList

    Your problem in that particular section of code, is that you are not creating your "Laptops" object correctly. You either need to add public properties to your laptop class and set them individually, and/or create a constructor in your class that accepts those parameters (model, cpu, ram..)

    Code:
        class Laptop
        {
            public string Model;
            public string CPU;
            public string RAM;
    
            public Laptop()
            {
            }
            public Laptop(string modelVal, string cpuVal, string ramVal)
            {
                Model = modelVal;
                CPU = cpuVal;
                RAM = ramVal;
            }
        }
    ...add each one to your array like this... (modified for your input etc of course...)

    Code:
                    System.Collections.ArrayList laptopList = new System.Collections.ArrayList();
    
                    laptopList.Add(new Laptop("Vaio", "P4", "4gb"));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured