![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Oct 2008
Posts: 3
| Is there a way to make objects with for loop with different names 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();
}
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. |
| zavzen is offline | |
| | #2 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| 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. |
| theoobe is offline | |
| | #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. |
| zavzen is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I believe I've found an official Answer to my Problem (Object Factories) | Shamino | Game Programming | 60 | 12-20-2005 11:36 PM |
| chain of objects within pop framework help needed | Davey | C++ Programming | 0 | 04-15-2004 10:01 AM |
| 'functions' in make? | mart_man00 | C Programming | 1 | 06-21-2003 02:16 PM |
| array of objects? | *~*~*~* | C++ Programming | 4 | 05-31-2003 05:57 PM |
| Objects, or pointers to objects? | Unregistered | C++ Programming | 5 | 12-18-2001 12:57 AM |