I want to return an object of type Period
when my dialog form (windows form application) closes.
I know that you can create simple properties string
or int
or something similar, that can be accessed with form.property
when the result came back with OK
. The thing is that Period
consists of a List
, two DateTime
, a string
and three decimal
properties.
If i try to make a Period
property i get the error message CS0053 Inconsistent accessibility: property type 'Period' is less accessible than property 'NewPeriod.Period'
. But i don't really believe that i have to return that object in parts and make a property for every single property in Period
.
Also when trying to make a List<Entry>
property i get the same CS0053
error, although with Entry
instead of Period
. Entry
consists of four string
, one double
and one DateTime
property.
Here is my shortened DialogForm:
public partial class NewPeriod : Form
{
public Period Period { get; set; }
private void btn_ok_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("OK");
Period = behind.AddPeriod(dtp_begin.Value, dtp_end.Value, num_invoiceAmount.Value);
}
}
This is the Period
model class:
class Period
{
public List<Entry> Entries { get; set; }
public DateTime Begin { get; set; }
public DateTime End { get; set; }
public string Name { get; set; }
public decimal ServerInvoiceAmount { get; set; }
public decimal SplitCostAmount { get; set; }
public decimal DonationAmount { get; set; }
}
I want to use the result like this:
var newPeriodForm = new NewPeriod();
var result = newPeriodForm.ShowDialog();
if(result == DialogResult.OK)
{
periods.Add(newPeriodForm.Period);
}
This is the first time im writing something in C# completely on my own and i couldn't find anything more than returning simple properties while researching.
CodePudding user response:
See where you wrote
class Period
Write this instead:
public class Period
Omitting the word "public" means it defaults to "internal", which in turn means you can't make a public property out of it, because "internal" is not as "widely visible" an access level as "public". You can downgrade accessibility (make something "less visible") but you can't upgrade it - if the class was public your property could be internal, protected, private etc but not the other way round.
As a new developer, if you're not sure what to put, just make all your classes public. Eventually you'll start to use other access levels (in various places) as situations crop up where you need the facility, but right now I'd say save yourself the headache of dealing with arcane compiler messages and use public for classes and properties, and private for fields