CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    [RESOLVED] Nested Get Methods

    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
    Thanks Ollie

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Nested Get Methods

    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;

  3. #3
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    Re: Nested Get Methods

    great thanks alot just what i was looking for!! =D

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Smile Re: [RESOLVED] Nested Get Methods

    No problem. Please rate my post to improve my reputation.

  5. #5
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    Re: [RESOLVED] Nested Get Methods

    okay got another question! =P

    how do i get civ.License to return a bool[] and not a class instense?!

    Thanks

  6. #6
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: [RESOLVED] Nested Get Methods

    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:

    Code:
    namespace WindowsFormsTest
    {
        public enum NewLicence
        {
            Driving,
            Aviation
        }
    }
    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;
                }
            }
    
        }
    }

  7. #7
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    Re: [RESOLVED] Nested Get Methods

    thanks for that! you have been very helpful! =D

  8. #8
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    Re: [RESOLVED] Nested Get Methods

    you say you cant have a bool with sub-propertie, well how does c# have an int with sub-properties with size?

    ie something.Size returns a value and something.Size.X returns a value

    this would help unconfuse me a litte! =)

    Thanks
    Last edited by geekman92; April 7th, 2011 at 07:02 AM. Reason: typo

  9. #9
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: [RESOLVED] Nested Get Methods

    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.

  10. #10
    Join Date
    Apr 2011
    Location
    England
    Posts
    14

    Re: [RESOLVED] Nested Get Methods

    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!!!

    Thanks alot for all your help!! =D

  11. #11
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: [RESOLVED] Nested Get Methods

    My pleasure. Enjoy your coding!

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Thumbs up Re: [RESOLVED] Nested Get Methods

    Quote Originally Posted by RedBully View Post
    No problem. Please rate my post to improve my reputation.
    Reputation can be misleading look at mine! LOL!!!

    Just keep posting your good posts, as you have been doing, and people will see that you're a very talented and gifted programmer

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured