List<object> ap = new List<object>();

my object classes are the classes that i have created.
for example;

class istanbul
{
    public istanbul(string a , int b)
    {this.a = a ; this.b = b ; }
    


    public  string a ;
    public int b ;
}
ap.add(new istanbul("dsklfjsf", 12))

-------------------------------
now how can i reach the value "dsklfjsf" or 12 from ap ?
Problem is that in an object array this is a run time value.

Recommended Answers

All 5 Replies

Please use code tags if you post any code.
Perhaps this bit of code will get you on the right track:

class Program
    {
        static void Main(string[] args)
        {
            List<istanbul> ap = new List<istanbul>();

            ap.Add(new istanbul("dsklfjsf", 12));
            foreach (istanbul o in ap)
            {
                Console.WriteLine(o.Name);
            }
            Console.ReadKey(); //in debug mode
        }
    }

    class istanbul
    {
        public istanbul(string a, int b)
        { this.a = a; this.b = b; }

        private string a;

        public string Name
        {
            get { return a; }
            set { a = value; }
        }

        private int b;

        public int Number
        {
            get { return b; }
            set { b = value; }
        }       
    }

Please use code tags if you post any code.
Perhaps this bit of code will get you on the right track:

class Program
{
    static void Main(string[] args)
    {
        List<istanbul> ap = new List<istanbul>();

        ap.Add(new istanbul("dsklfjsf", 12));
        foreach (istanbul o in ap)
        {
            Console.WriteLine(o.Name);
        }
        Console.ReadKey(); //in debug mode
    }
}

class istanbul
{
    public istanbul(string a, int b)
    { this.a = a; this.b = b; }

    private string a;

    public string Name
    {
        get { return a; }
        set { a = value; }
    }

    private int b;

    public int Number
    {
        get { return b; }
        set { b = value; }
    }       
}

end quote.

ap is not a list of istanbul; ap is a list of objects ; list may have istanbul, londra, new york.

Cannot make up anything out of your post???

Seems like you want to list some cities.
I suggest you make a City class with amongst others, a property called CityName or something.
Like this:

List<City> cities = new List<City>();

If all of your classes will store the same data but relevant to different cities then you could add a City member to the class, eg:

class City
{
    string Name;
    string Number;
    string City;
}

Your class would obviously include the necessary properties, modifiers, etc.

If each class will have a different implementation then you can create a virtual class that outlines the members each city must have. Then each derived city class inherits from the base class and overrides the virtual members.

Using this structure, you can create a list of your base class to store all your cities but when you access the virtual members it will call the child versions. Such as:

class Class1
    {
        public Class1()
        {
            A = 1;
        }

        virtual public int A { get; set; }
    }

    class Class2 : Class1
    {
        public Class2()
        {
            A = 2;
        }

        public override int A { get; set; }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            Class2 c2 = new Class2();
            List<Class1> classes = new List<Class1>();
            classes.Add(c1);
            classes.Add(c2);

            foreach (Class1 cl in classes)
            {
                Console.WriteLine(cl.A);
            }

            Console.ReadKey();
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.