Csharp_crack
Csharp_crack

Reputation: 246

How to dynamically add class instances in c#

what i would ask might be silly.

for ex: I have two classes like

public class Login
{
    public String Username;
    public String Pwd;
    public address address1;
}    

Address.cs

public class address
{
    public String location;
    public string phonenumber;
}

can i increment the members of the class Login with address2 and address3 as the instances of the class Address using for loop at runtime. Can this be done.. I have an api which is used like that, rather than using address as list/array. please let me know if im confusing.. thanks in advance.

Upvotes: 0

Views: 153

Answers (3)

user2277061
user2277061

Reputation: 7

Try using Dictionary, example

class Something
{
   public string Username;
   public string Password;
   public Dictionary<string,address> Address;

   public Something(string username, string password)
   {
      this.Username = username;
      this.Password = password;
      this.Address = new Dictionary<string, address>();
   }
}

add the new variable like this:

void example()
{
     var Something x = new Something("username", "password");
     x.Address.Add("Address1",new address());
     x.Address.Add("Address2",new address());
     x.Address.Add("Address3",new address());
     x.Address.Add("Address4",new address());
     //next address what you want
}

reference: Set array key as string not int?

Upvotes: 0

Michael Bray
Michael Bray

Reputation: 15265

You should create an array of address classes:

public class Login
{
    public String Username;
    public String Pwd;
    public address[] address;
} 

Then use them:

Login l = new Login();
l.address = new address[3];
l.address[0].location = ...
l.address[2].phonenumber = ...
l.address[1].location = ...
l.address[2].location = ...
...etc...

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500335

can i increment the members of the class Login with address2 and address3 as the instances of the class Address using for loop at runtime.

No. If you want more than one address, you should change your class to have a collection of addresses instead of a single field. You can't add fields to an object at execution time. I would suggest using a List<Address>.

Additionally, I'd strongly suggest using properties instead of public fields, and following .NET naming conventions.

Exactly how you expose the collection is up to you, but I'd either expose the List<Address> via a readonly property, or expose methods of AddAddress and RemoveAddress. I'd also try to make Address itself immutable, with a constructor taking all the aspects of it, e.g.

public sealed class Address
{
    private readonly string location;
    private readonly string phoneNumber;

    public string Location { get { return location; } }
    public string PhoneNumber { get { return phoneNumber; } }

    public Address(string location, string phoneNumer)
    {
        // TODO: Validate that it's a valid phone number? Possibly the same
        // for location? Almost certainly prevent either from being null...
        this.location = location;
        this.phoneNumber = phoneNumber;
    }
}

Upvotes: 2

Related Questions