Member Avatar for geekman92

hi i was wondering if (and how) you can create nested methods in c#.

c# seems to do it with size and other things.

something.Size returns a value but also something.Size.X returns a value.

how can i do something like this myself?

Thanks Ollie

Recommended Answers

All 7 Replies

Do you perhaps have some pseudo code? Are you talking about returning multiple values?

If you are talking about returning mulitple values then perhaps look at creating a class that contains the data properties you are going to need otherwise you could look at using a method which makes use of 'out' parameters.

Again, pseudo code would be extremly helpful in figuring out how to solve your problem

Member Avatar for geekman92

thanks for the quick reply! =)

class Cop
{
     bool[] _Licenses = new bool[5];

     public bool[] Licenses
     {
        get { return _Licenses; }
        set { _Licenses = value; }

        public bool Driving
        {
            get { return _Licenses[0]; 
            set { _Licenses[0] = value; }
        }
     }
}

class MainClass
{
      Cop cop = new Cop();
      bool[] AllLicenses = cop.Licenses;
      bool DrivingLicense = cop.Licenses.Driving;
}

Hope this helps you understand what i am looking for

Thanks Ollie

there is a bit of a flaw in your model. First up you are going to have to create some sort of object model to properly achieve this.

Something like

class Cop{
		private IList<License> _licenseList;

		public IList<License> LicenseList {
                           // the ?? is a null check so if the _licenseList is null
                           // it will create a new instance of it for you. Short hand for
                           // an if statement checking if it is null.
			get { return _licenseList ?? (_licenseList = new List<License>()); }
			set { _licenseList = value; }
		}
	}

class License {
		public LicenseType LicenseType { get; set; }
	}

enum LicenseType {
		Driver,
		Pilot,
		Diver
	}

Then to achieve what you are doing you could do something like

var cop = new Cop();
//This will probably be populated from some data store.
cop.LicenseList.Add(new License{LicenseType = LicenseType.Driver});

//To query the licenses you can use lambda expressions
//List of all Drivers licenses
var driversLicenseList = cop.LicenseList.Where(x => x.LicenseType == LicenseType.Drivers);
//The first drivers license in the list
var firstDriversLicense = cop.LicenseList.FirstOrDefault(x=>x.LicenseType == LicenseType.Drivers);

This will give you the license. You can increase the declerations of the license class to include things like id numbers or the person the license belongs to, expiry date etc etc.

Hope it makes sense

Member Avatar for geekman92

okay great thanks for your help! =) i'll try this out know!

how does c# do something.Size.X ?

Thanks Ollie

I need a bit more info on your last question :) Perhaps create a new thread?

how does c# do something.Size.X ?

class MyClass {
    public Size Size {get; set;}
}

class Size {
    public int X {get; set;}
    public int Y {get; set;}
}
Member Avatar for geekman92

thanks guys! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.