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