I just started with C# and a windows form application. I'm fairly new to OOP as well. I trying to create an application where the user can modify some settings from a meny that opens a second form. I found a way to move the user input data from the "settings form" to the main form using an additional "settings class". I am not however sure if I'm doing this in an good way and is seeking advice on best practice.
My code for the "Main form" looks like:
namespace Scheduler
{
public partial class FormScheduler : Form
{
public FormScheduler()
{
InitializeComponent();
}
private PeriodSettings mPeriodSettings = new PeriodSettings();
public string StartDate
{
get{ return mPeriodSettings.StartDate; }
set{ mPeriodSettings.StartDate = value; }
}
private void settingsSchPer_Click(object sender, EventArgs e)
{
FormSchemaPeriodSettings formSchPer = new FormSchemaPeriodSettings();
formSchPer.formScheduler = this;
formSchPer.Show();
}
}
}
And the code for the "settings form" looks like:
namespace Scheduler
{
public partial class FormSchemaPeriodSettings : Form
{
public FormSchemaPeriodSettings()
{
InitializeComponent();
}
public FormScheduler formScheduler = new FormScheduler();
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.formScheduler.StartDate = this.startDate.Value.ToShortDateString();
this.Close();
}
}
}
For example, must I define the properity for SetDate in the "Main form", i.e. the part
public string StartDate
{
get{ return mPeriodSettings.StartDate; }
set{ mPeriodSettings.StartDate = value; }
}
in order to be able to access the StartDate of class PeriodSettings or is there a way to be able to directly access the already defined mPeriodSettings.StartDate?
My PeriodSettings class looks like:
namespace Scheduler
{
class PeriodSettings
{
private string mStartDate;
private string mEndDate;
public PeriodSettings()
{
}
public string StartDate
{
get{ return mStartDate; }
set{ mStartDate = value; }
}
public string EndDate
{
get{ return mEndDate; }
set{ mEndDate = value; }
}
}
}
Thankful for any advice!