Thread: Is there a way to make objects with for loop with different names

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Is there a way to make objects with for loop with different names

    Hello to who is seeing this thread,
    Sorry about that funny problem. For example I have a customer class and need to make any number customer. I have an array of strings and want to make these strings customers object name.
    Code:
    for (int i = 0; i < number; i++)
    {
       Customer stringArray[i] = new Customer();
    }
    Like this if possible.

    It wont work. Because of types. (String, Customer)

    I can make firstly customers. customer1, customer2, customer3 and more. But I dont want to make more objects. Just need objects about an exact number.

    Thank you.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I know I am deviating away from what you've specifically asked for, but, why must the customer object name be the customer's name? Why can't the customer's name be one of the variables within the customer object?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static List<Customer> customers;
    
            static void Main()
            {
                // let's say these are the names in a string array
                String[] names = new String[] { "david", "john", "peter", "andrew", "simon" };
    
                // why not have a list of Customer objects and include
                // the customer's name within the Customer object
                customers = new List<Customer>();
    
                // initial population of the list
                foreach (String str in names)
                    customers.Add(new Customer(str));
    
                // example of finding a customer's object
                Customer test = GetCustomer("david");
    
                if (test == null)
                    Console.WriteLine("customer doesn't exist.");
                else
                    Console.WriteLine("customer exists!! :-)");
            }
    
            static Customer GetCustomer(String CustomerName)
            {
                var query = from x in customers where x.CustomerName == CustomerName select x;
    
                if (query.Count<Customer>() > 0)
                    return query.ToArray<Customer>()[0];
    
                return null;
            }
        }
    
        class Customer
        {
            public String CustomerName;
            // adding the rest of your object variables and methods here
    
    
    
            public Customer(String CustomerName)
            {
                this.CustomerName = CustomerName;
            }
    
            public override bool Equals(object obj)
            {
                // give the Equals method a proper purpose
                return this.CustomerName == ((Customer)obj).CustomerName;
            }
    
            public override int GetHashCode() { return base.GetHashCode(); }
        }
    }
    Last edited by theoobe; 10-06-2009 at 03:49 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Thank you :)

    I'm so happy. You really helped me a lot. Now I can sleep easily. That problem was eating my brain last two days .
    Thank you, Arigato, Teşekkürler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  3. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM
  4. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM