0

I have an "Address" class, and make 10 instances of it like so:

Address ad1 = new Address(){
OwnerId = 1,
Street = "Papanikolaou 3",
ProvinceId = 1,
PostalCode = "13245",
TownId = 1,
CountryId = 1,
TypeId = 1,
Active = true
  };

Every new object name only has the number at the end of the name changed. In example:

Address ad2 = new Address();
Address ad3 = new Address();

And so on...

Next i want to add all those objects into a list. Since the object names are similar could i use a "for" loop, and use the loop counter in order to "change" the object name, since every instance name of the "Address" class is so similar?

E.G(I know this won't work):

List<Address> lst = new List<Address>();
for(i = 1; i <= 10; i++) {
  lst.add(adi);
} 
SpirosMesa
  • 69
  • 1
  • 1
  • 10
  • It's possible, but instead you must think about how to not to create all these variables ^_^ Hint: create all this stuff in loop and then operate with list. – Spawn Sep 19 '15 at 18:24

4 Answers4

4

If you need to create 10 objects and add to a list without a 'Name' , you can do this

List<Address> lst = new List<Address>();
for(i = 1; i <= 10; i++) {
   lst.Add(new Address())
} 

Otherwise you can use Dictionary to have a name for object and add to a List<Dictionary<string,Object>>()

 List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
 Dictionary<string, Object> objs = new Dictionary<string, object>();
 for (int i = 1; i <= 10; i++)
 {
   objs.Add(i.ToString(), new Address() { OwnerId = "test", ProvinceID = "test2" });
 }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
2

Because the variable names are just references, there is no (useful) way to do this as far as I know.

But it's simple enough like this:

lst.add(new Address(){
    OwnerId = 1,
    Street = "Papanikolaou 3",
    ProvinceId = 1,
    PostalCode = "13245",
    TownId = 1,
    CountryId = 1,
    TypeId = 1,
    Active = true
 };
Michael D
  • 678
  • 3
  • 11
0

I don't recommend building objects in this fashion, however, this makes an interesting problem to solve.

It depends on how you have declared your variables. If they're public fields of a class, you can actually get them through reflection:

       private static void Main(string[] args)
    {
        var addressContainer = new AddressContainer();
        var fields = addressContainer.GetType().GetFields();
        List<Address> lst = new List<Address>();
        foreach (var field in fields)
        {
            //print varialbe name
            Console.WriteLine(field.Name);
            var theValue = field.GetValue(addressContainer);
            if (theValue is Address) lst.Add((Address) theValue);
        }
        //spit out the collected address street values
        foreach (var address in lst)
        {
            Console.WriteLine(address.Street);
        }
        Console.ReadLine();
    }
}

public class AddressContainer
{
    public Address ad1 = new Address() { Street = "Papanikolaou 3" };
    public Address ad2 = new Address() { Street = "Papanikolaou 7" };
}

public class Address
{
    public string Street { get; set; }
}

If they're simply local variables, it requires your code to be knowledgeable of all the objects accessible to the current app domain. It is not easy to do. The following posts may be of an interest to you:

In practice, you should obtain a reference to the list as Michael and Sajeetharan suggested so you can manipulate it as needed.

Community
  • 1
  • 1
Leo Nix
  • 2,085
  • 2
  • 24
  • 37
0

Well, you could do as pers Michael's answer or you could move the instantiation to a method and end up with something like:

lst.add(BuildAddress(1, "Papanikolau 3", 1, "13245", 1, 1, 1, true));
// etc

Where BuildAddress just creates an address instance based on those parameters

private Address BuildAddress(int ownerId, string street, int provinceId, string postalCode, int townId, int countryId, int typeId, bool isActive)

    return new Address()
    {
        OwnerId = ownerId,
        Street = street,
        ProvinceId = provinceId,
        PostalCode = postalCode,
        TownId = townId,
        CountryId = countryId,
        TypeId = typeId,
        Active = isActive
    };

Just be careful with this, as it can quickly become quite ugly if you keep adding parameters to the method.

Community
  • 1
  • 1
async
  • 1,537
  • 11
  • 28