Hello all,

i want to creat a list of an object
so i used ArrayList in C#


after declaring a class book
i declare the array list as follwing:

private ArrayList mobileStore = new ArrayList();

then i have an object of class , say Mobile

Mobile m1;

and finally i tried to add the object m1 to the array list

mobileStore.Add(m1);
but the error occurs !!


Invalid token '(' in class, struct, or interface member declarationInvalid token ')' in class, struct, or interface member declaration

-------------------

then i tried anoter way :


List<Mobile> store = new List<Mobile>();

but the problem was the same ????


how can i fix it ?

I think you need to create the instance of your mobile class before you add in in ArrayList.

Mobile m1=new Mobile();

I think you need to create the instance of your mobile class before you add in in ArrayList.

Mobile m1=new Mobile();

i already didi !!
the problem is the same !!

i already didi !!
the problem is the same !!

Without seeing all of your code its hard to give a defintie aswer, but is the line mobileStore.Add(m1); inside a method or is it at the class level?

You may be missing a closing bracket or have this at the wrong level, any code other than declarations need to be inside a method. Try placing it inside a constructor:

public partial class Form1 : Form
    {
        List<Mobile> mobileStore = new List<Mobile>();

        public Form1()
        {
            InitializeComponent();

            Mobile m1 = new Mobile();
            mobileStore.Add(m1);
        }
     }

I think you need to create the instance of your mobile class before you add in in ArrayList.

Mobile m1=new Mobile();

Without seeing all of your code its hard to give a defintie aswer, but is the line mobileStore.Add(m1); inside a method or is it at the class level?

You may be missing a closing bracket or have this at the wrong level, any code other than declarations need to be inside a method. Try placing it inside a constructor:

public partial class Form1 : Form
    {
        List<Mobile> mobileStore = new List<Mobile>();

        public Form1()
        {
            InitializeComponent();

            Mobile m1 = new Mobile();
            mobileStore.Add(m1);
        }
     }

ummmm
why you have been used form()?


this is my code

public class Mobile
    {
        // DECLARATION --------------------
        private string name;
        private string desc;
        private string img;
        private double price;
        private int quan;

        // Mobile Class Constructor -------
        public Mobile(string name,string desc,string img,double price,int quan)
        {
            name = name;
            desc = desc;
            img = img;
            price = price;
            quan = quan;
        }

        // Mobile Name Class Prop ---------
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        // Mobile Describtion Class Prop --
        public string Desc
        {
            get { return desc; }
            set { desc = value; }
        }
        // Image file name Class Prop -----
        public string Img
        {
            get { return img; }
            set { img = value; }
        }
        // Mobile Price Class Prop --------
        public double Price
        {
            get { return price; }
            set { price = value; }
        }
        // Mobile Quantity Class Prop ------
        public int Quan
        {
            get { return quan; }
            set { quan = value; }
        }

    }// End MOILE CLASS =)

    public class Web
    {
        //create a collection of object (LIST)
    
        Mobile m1 = new Mobile ("mobile" ," new mobile" , " img " , 100,3);
        
        //ArrayList mobileStore = new ArrayList();
        //mobileStore.Add(m1);
        
        List<Mobile> store = new List<Mobile>();
        store.Add(m1);

    }//End WEB CLASS =)

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    }

i also tried this way::

ArrayList mobileStore = new ArrayList();
        mobileStore.Add(new Mobile( "mobile" ," new mobile" , " img " , 100,3) );

:( !!???

I researched some with google and one quick solution is put your code in a method. like

public class Web
    {
        //create a collection of object (LIST)

        private int test()
        {
            Mobile m1 = new Mobile("mobile", "new mobile", "img", 100, 3);

            ArrayList mobileStore = new ArrayList();
            mobileStore.Add(m1);
            return 0;

        }
        
    }//End WEB CLASS =)

I used a form because i didnt know the context of your problem.
The point was that you cant add items to the collection at the class level, you need to mvoe the code into a method so that the compiler knows when to run it:

public class Web
    {
        //create a collection of object (LIST)
        List<Mobile> store = new List<Mobile>();
        
        Public Web()
        {
             Mobile m1 = new Mobile ("mobile" ," new mobile" , " img " , 100,3);
             store.Add(m1);
        }
     }

Public Web() is the constructor for the Web class. The containing code is run when the class is instantiated.

I researched some with google and one quick solution is put your code in a method. like

public class Web
    {
        //create a collection of object (LIST)

        private int test()
        {
            Mobile m1 = new Mobile("mobile", "new mobile", "img", 100, 3);

            ArrayList mobileStore = new ArrayList();
            mobileStore.Add(m1);
            return 0;

        }
        
    }//End WEB CLASS =)

Great minds work alike,eh :p we posted near identical call at teh same time :p

Just remember that doing it this way you would need to explicitly call the "test()" method to populate the collection. But you are right, as i pointed out earlier, you cannot add to the collection at the class level.

The code i posted is essentially the same except that instead of a named method i used the classes constructor since that is always called when the class is instantiated.

I used a form because i didnt know the context of your problem.
The point was that you cant add items to the collection at the class level, you need to mvoe the code into a method so that the compiler knows when to run it:

public class Web
    {
        //create a collection of object (LIST)
        List<Mobile> store = new List<Mobile>();
        
        Public Web()
        {
             Mobile m1 = new Mobile ("mobile" ," new mobile" , " img " , 100,3);
             store.Add(m1);
        }
     }

Public Web() is the constructor for the Web class. The containing code is run when the class is instantiated.

i guess it work !!

thank you
i will try it

Yes I am new to C# and still learning the bits and pices. You are right to say that you can not add itmes in Collection at class level...Lesson Learned for me..Thanks

Here is an example of an object:

//The Card Object. Global Object
class Card
{
        public int value;
        public string suit;
        public string type;
        public string image;
}
static void Main()
{
      Program myProgram = new Program();
      myProgram.createCard();
}
private void createCard()
{      
     //Create a new instance card object 
      Card card = new Card(); 
      //Add values
      card.value = 5;
      card.suit = "spades";
      card.type = "Number";
      card.image = "spades5.png";
      
      //Create an array of Card Object
      Card[] deck = new Card[1];
      deck[0] = card;
     
     //Or create an array list
     ArrayList deckList = new ArrayList();
     decklist.Add(card);
}

Edited 7 Years Ago by RunTimeError: Typo

This article has been dead for over six months. Start a new discussion instead.