Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# OOP: sub-property?

C# OOP: sub-property?

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    Ok, this is probably a very basic question, but how would I go about creating the following (bear with me. I'm new to oop): Lets say I'm in the automotive industry and I build a c# class called "Cars". Within this class, I have 2 properties: -Model -Year So I can call them via Cars.Model & Cars.Year via code like this:

    public class Cars
    {
    public string Model { get; set; }
    public string Year { get; set; }
    }

    So far, so good....HOWEVER....now, I want to do a sub-property of Model, called Color. I would want to call it with something like this: Cars.Model.Color = "green"; How would I create this "Color" property so that it becomes a sub-property of "Model"? Thanks

    D P P 3 Replies Last reply
    0
    • G Goalie35

      Ok, this is probably a very basic question, but how would I go about creating the following (bear with me. I'm new to oop): Lets say I'm in the automotive industry and I build a c# class called "Cars". Within this class, I have 2 properties: -Model -Year So I can call them via Cars.Model & Cars.Year via code like this:

      public class Cars
      {
      public string Model { get; set; }
      public string Year { get; set; }
      }

      So far, so good....HOWEVER....now, I want to do a sub-property of Model, called Color. I would want to call it with something like this: Cars.Model.Color = "green"; How would I create this "Color" property so that it becomes a sub-property of "Model"? Thanks

      D Offline
      D Offline
      David1987
      wrote on last edited by
      #2

      You give Model a type that has a property called Color

      P 1 Reply Last reply
      0
      • G Goalie35

        Ok, this is probably a very basic question, but how would I go about creating the following (bear with me. I'm new to oop): Lets say I'm in the automotive industry and I build a c# class called "Cars". Within this class, I have 2 properties: -Model -Year So I can call them via Cars.Model & Cars.Year via code like this:

        public class Cars
        {
        public string Model { get; set; }
        public string Year { get; set; }
        }

        So far, so good....HOWEVER....now, I want to do a sub-property of Model, called Color. I would want to call it with something like this: Cars.Model.Color = "green"; How would I create this "Color" property so that it becomes a sub-property of "Model"? Thanks

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        As your code currently stands, you can't. What you need to do is create a class that Model implements (let's call it the Model class). Now, in this Model class, you'll have your Color property, so you end up with something like the following:

        public class Cars
        {
        public Model Model { get; set; }
        public string Year { get; set; }
        }
        public class Model
        {
        public string Color { get; set; }
        }

        Now, a couple of things to consider here. First of all, you shouldn't be using a string for the Year property - it's really a number. Secondly, you shouldn't really call your class Cars - it's describing a single car, so it should really be called Car.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        1 Reply Last reply
        0
        • D David1987

          You give Model a type that has a property called Color

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I don't think somebody likes us giving this poster answers.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          D L 2 Replies Last reply
          0
          • P Pete OHanlon

            I don't think somebody likes us giving this poster answers.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            D Offline
            D Offline
            David1987
            wrote on last edited by
            #5

            So it would seem.. Oh well, we probably got more upvotes because of it :)

            1 Reply Last reply
            0
            • P Pete OHanlon

              I don't think somebody likes us giving this poster answers.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I have been repeatedly asking to make comments mandatory for downvotes. I understand that the voter may put some garbage in the comments just for the sake of it. But it would help us to identify the trolls and banish them. (It can also help Chris to delete those downvotes).

              1 Reply Last reply
              0
              • G Goalie35

                Ok, this is probably a very basic question, but how would I go about creating the following (bear with me. I'm new to oop): Lets say I'm in the automotive industry and I build a c# class called "Cars". Within this class, I have 2 properties: -Model -Year So I can call them via Cars.Model & Cars.Year via code like this:

                public class Cars
                {
                public string Model { get; set; }
                public string Year { get; set; }
                }

                So far, so good....HOWEVER....now, I want to do a sub-property of Model, called Color. I would want to call it with something like this: Cars.Model.Color = "green"; How would I create this "Color" property so that it becomes a sub-property of "Model"? Thanks

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I think you're going about it wrong. I think the Model class should have the Year property, but not the Color property. Although the Model class could have an AvailableColors collections.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups