I have a class used in several places in my code (Credentials) there is a method that will require much more data I want all of it in one class (ArchiveData) including the original (Credentials) class
So when I create the class I simply want to assign all the data from the smaller class into the larger class. What is the best practices method of doing this?
There was a similar question here Are class initializers possible in C#? and it was recommended not to overwrite the initialize propery. I don't necessarily want to I just want an overload method which adds known values for me so I can have cleaner code, but giving it an overload will override the default initializer.
Although in this case I will always have the subclass so removing the original initializer is fine in this example. I can see scenarios where I would not want this.
Edit: Removed Stack Overflow Error The main question is about best pactices and pros/cons from various ways of "eating a subclass."
Code below:
Desired end result is a clean way of making a larger class have all data of a smaller class:
Credentials credentials = new Credentials("My User Name", "some password");
ArchiveData AD = new ArchiveData(credentials);
Console.Write(AD.credentials.UserID);
Console.Write(AD.credentials.password);
Setup of Classes:
public partial class ArchiveData
{
public string DocumentType { get; set; }
public int RTKD { get; set; }
public string RTMSG { get; set; }
public Credentials credentials;
public ArchiveData(Credentials credentials)
{
this.credentials = credentials;
}
}
public class Credentials
{
public string userId { get; set; }
public string password { get; set; }
public Credentials(string userId, string password)
{
this.userId = userId;
this.password = password;
}
}