0
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.

Edited by __avd: Added [code] tags.

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Geekitygeek
Featured Replies
  • 1
    ddanbe 2,525   6 Years Ago

    Please use code tags if you post any code. Perhaps this bit of code will get you on the right track: [CODE=c#]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 } … Read More

1

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; }
        }       
    }
0

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.

Edited by mike_2000_17: Fixed formatting

0

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>();
1

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();
        }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.