954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Nested Get Methods

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

geekman92
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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

RabidDog5150
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 5
 

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

geekman92
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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

RabidDog5150
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 5
 

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

how does c# do something.Size.X ?

Thanks Ollie

geekman92
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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

RabidDog5150
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 5
 
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;}
}
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

thanks guys! =D

geekman92
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: