C# OOP: sub-property?
-
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
-
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
-
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
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
-
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
-
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
-
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
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).
-
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
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.