hi i was wondering if (and how) you can create nested methods in c#.
c# seems to do it with size like below.
something.Size returns a value but also something.Size.X returns a value.
how can i do something like this myself?
i would like to do sumit like:
Code:
Civilian civ = new Civilian();
bool[] licenses = civ.Licenses; // returns bool array of all the licenses
bool drivingLicense = civ.Licenses.Driving; // returns true/false if they have a driving license
bool aviationLicense = civ.Licenses.Aviation; // returns true/false if they have an aviation license
You would need to create a class for Licence, for example:
Code:
namespace WindowsFormsTest
{
public class Licence
{
private bool driving = false;
private bool aviation = false;
public bool Driving
{
get
{
return this.driving;
}
}
public bool Aviation
{
get
{
return this.aviation;
}
}
}
}
You would then give your Civilian class a Licence property:
Code:
namespace WindowsFormsTest
{
public class Civilian
{
private Licence licence = new Licence();
public Licence Licence
{
get
{
return this.licence;
}
}
}
}
You could then access the properties in the way you descrive
Code:
Civilian civ = new Civilian();
bool av = civ.Licence.Aviation;
You can have a property of Civilian that returns a bool[]
E.g.
Code:
public bool[] MyBooleanArray
{
get
{
return new bool[10];
}
}
But you can't have bools with sub properties as you like
But I think from what I can gather, you want to change your design a little.
I think you're saying that each Civilian can have a number of licences and that each licence is either a Driving one or Aviation one. You may be better using an enumerator for the Licence:
and chance Civilian to be able answer the questions you have of it. Note that I use the List<NewLicence> instead of an array. You can use them in the same way if you've not touched on them before.
Code:
using System.Collections.Generic;
namespace WindowsFormsTest
{
/// <summary>
/// A Dude
/// </summary>
public class Civilian
{
/// <summary>
/// A list of licences that a civilian can hold
/// </summary>
private List<NewLicence> licences = null;
public Civilian()
{
//By default we give all civilians an aviation
//and driving licence
this.licences = new List<NewLicence>();
this.licences.Add(NewLicence.Aviation)
this.licences.Add(NewLicence.Driving);
}
/// <summary>
/// Establish if a civilian has a licence of the specified type
/// </summary>
public bool HasLicence(NewLicence licenceType)
{
foreach (NewLicence licence in this.licences)
{
if (licence == licenceType)
{
return true;
}
}
return false;
}
/// <summary>
/// Return a list of licences held by the civilian
/// </summary>
public List<NewLicence> Licences
{
get
{
return this.licences;
}
}
}
}
Could you send me an example of your Size that behaves this way. It may be implementing an interface that I'm not familiary with. What you describe sounds like implicit conversion. Update the Licence class to the following:
Code:
namespace WindowsFormsTest
{
public class Licence
{
private bool driving = false;
private bool aviation = false;
public bool Driving
{
get
{
return this.driving;
}
}
public bool Aviation
{
get
{
return this.aviation;
}
}
public static implicit operator bool(Licence rhs)
{
//Return true only if both licences are held
return rhs.Aviation && rhs.Driving;
}
}
}
Then try it out with someting like:
Code:
Licence lic = new Licence();
bool result = lic;
bool result2 = lic.Aviation;
However, be very careful with implicit conversion, it's better to do explicit conversion because it leads to fewer bugs especially runtime errors that can be a bit of a nightmare to solve.
okay whilst writing out a sample to give you i realised that i was being silly! lol
i ment to say something.Size and something.Size.Height were something was like a textbox.
obv something.Size is just an instance of the class Size right?! and something.Size.Height is a property within the class Size!
and that is what you gave me, so yer sorry for the confusion! =P
i've never used implicit conversions like what you showed me above so i am not gona use it esp say if it can cause bugs!!!
Bookmarks