Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I had a class say:

 class Car
    {
        private string make, model;
        private string Cars(string make, string model)
        {
            make = make + model;
            return make;
        }
        public string MyProperty
        { 
            get { return make; }
            set { make = Cars(value, value); } 
        {
}
 class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            Console.WriteLine("What is the Make of the car: ");
            car.MyProperty = Console.ReadLine();
            Console.WriteLine("What is the Model of the car: ");
            car.MyProperty = Console.ReadLine();
            Console.WriteLine("The Make of the car is: {0}\nAnd the Model of the car                  is: {0}", car.MyProperty);
            Console.ReadLine();
        }
    }


How can I get 2 value with my property concat the strings and have it output properly.
Posted
Comments
Herboren 20-Jul-11 8:35am    
Gee I don't think your opinion matters now seeing how it got answered correctly.

First, I would make the make and model separate properties:

C#
public string Make { get; set; }
public string Model { get; set; }

0) You could override the ToString() method to return a frmatted string containing the info you wanted.

C#
public override string ToString()
{
    return string.Format("{0} {1}", this.Make, this.Model);
}


And use it like this:

C#
Console.WriteLine(car.ToString());
// or like this:
Console.WriteLine(car);


1) You could write a method to return the formatted info you wanted.

C#
public string GetMakeAndModel()
{
    return string.Format("{0} {1}", this.Make, this.Model);
}


And use it like this:

C#
Console.WriteLine(car.GetMakeAndModel());
 
Share this answer
 
v3
public class Car
   {
   public string Make { get; set; }
   public string Model {get; set;}
   public string Car(string make, string model)
      {
      Make = make;
      Model = model;
      }
   public override string ToString()
      {
      return string.Format("{0}:{1}", Make, Model);
      }
   }
class Program
   {
   static void Main(string[] args)
      {
      Console.WriteLine("What is the Make of the car: ");
      string make = Console.ReadLine();
      Console.WriteLine("What is the Model of the car: ");
      string model = Console.ReadLine();
      Car car = new Car(make, model);
      Console.WriteLine("The Make of the car is: {0}\nAnd the Model of the car is: {1}", car.Make, car.Model);
      Console.WriteLine("The car is {0}", car);
      Console.ReadLine();
      }
   }



"It worked, but what I didn't understand is how 'Console.WriteLine("Your Car: {0}", car);' knew where to get the formatted string. Because 'public override string ToString()' doesn't have any type of identifier involved in it to show ownership of what its trying to accomplish."


If you feed any variable into a string.Format method (and Console.WriteLine does just that - it feeds your parameters into string.Format and prints the result to the console) then it does an implicit yourVariable.ToString() call in order to get something it can print - if the variable isn't a string already.
The ToString method above accesses the two properties of the current instance of the Car class (also known as this) and fetches the relevant data. It could be re-written as:
public override string ToString()
   {
   return string.Format("{0}:{1}", this.Make, this.Model);
   }
But the "this." bit is implied in all non-static methods, so can be left out for readability. Normally, I only include it if there is a duplication of names between a method parameter and a property or field.

Does that make sense?
 
Share this answer
 
v2
Comments
Herboren 19-Jul-11 10:32am    
It worked, but what I didn't understand is how 'Console.WriteLine("Your Car: {0}", car);' knew where to get the formatted string. Because 'public override string ToString()' doesn't have any type of identifier involved in it to show ownership of what its trying to accomplish.
OriginalGriff 19-Jul-11 12:22pm    
Answer updated
#realJSOP 19-Jul-11 14:56pm    
When you normally do that, the ToString() method returns the object type (unless it's a string). When you override the ToString() method, your override is called instead of the base class' version, and whatever code you put into your override will be executed. You should google "polymorphism" to understand the mechanism behind it.
OriginalGriff 19-Jul-11 15:04pm    
Good points - I should should have mentioned them.
You will need a property of a structured data type, struct or an array, with two components.

But is it really worth the complication ???


Alternatively:
C#
public string MyProperty
{
    get { return string.Concat(make, '|', model); }
    set { make = value.Split('|')[0]; model = value.Split('|')[1]; }
}


The code of the setter shows you how to retrieve the string parts.
 
Share this answer
 
v2
your problem is that you only store make, not model because make = make+model
so you have a 'citroen2CV' instead of 'citroen 2CV'.

If you had a delimiter you could find the differnce between make and model again, so if you had make = make +'|' +model you had a 'citroen|2CV'

and then you could do:
string[] mycar = car.MyProperty.Split('|');
Console.WriteLine("The Make of the car is: {0}\nAnd the Model of the car                  is: {1}", mycar[0], mycar[1]);


The question remains why not store the model?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900