3 Replies - 282 Views - Last Post: 04 August 2013 - 12:03 PM Rate Topic: -----

#1 AZE2013  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 10-July 13

wanting to output 2 methods called type, to return make and colour

Posted 03 August 2013 - 07:51 PM

All the rest of the code works, just having difficulty assigning the last to methods to my code.
methods called type. need to return "make" and "colour" of the Subaru. Working with class called "Vehicle". I haven't sourced any other help online, just here. Any feedback would be appreciated. AZE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoreC
{
    class Program
    {
        static void Main(string[] args)
        {
            Vehicle subaru = new Vehicle();
            subaru.make = "subaru";
            subaru.locked = true;
            subaru.colour = "red";
            

            if (subaru.lockCar())
                Console.WriteLine("beep");
            else
                Console.WriteLine("beep beep");
            if (subaru.lockCar())
                Console.WriteLine("beep");
            else
                Console.WriteLine("beep beep");

            subaru.acc(53.7);
            Console.WriteLine(subaru.mySpeed().ToString());

            subaru.type());
            //Console.ReadKey();
            Console.WriteLine("red subaru");


            subaru.changegear(2);
            Console.ReadKey();




        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoreC
{
    class Vehicle
    {
        public int gear = 1;
        public double speed;
        public string colour;
        public string make;
        public bool locked;

        public void acc(double speed) 
        {
            this.speed = speed;
        }
        public int mySpeed() 
        {
            return (int)speed;
        }
        public void changegear(int gear) 
        {
            this.gear = gear;
            Console.WriteLine(gear.ToString());
        }
        public string type(string colour)
        {
            this.colour = colour;
            Console.WriteLine(colour.ToString());
        }
        public string make(string subaru)
        {
            return (string)subaru;
        }

        public bool lockCar()
        {
           locked =!locked;
           Console.WriteLine("lock pressed");
           return locked;
       }

    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: wanting to output 2 methods called type, to return make and colour

#2 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: wanting to output 2 methods called type, to return make and colour

Posted 03 August 2013 - 08:59 PM

I'm sorry, but I'm having a very hard time trying to understand what you want by your question. It looks like you want the method "type" to output both make and colour. To do that, you can use string concatenation (combining two strings) and output them both on one line, or you can call Console.WriteLine() twice to output both the make and colour. If you also want to change the make from the methods signature, you could add an additional parameter. In the end, if this is what you want to do, your function should look a little bit like this:

        public string type(string colour, string make)
        {
            this.colour = colour;
            this.make = make;
            Console.WriteLine("Colour:  " + colour.ToString() + " - Make:  " + make.ToString());
        }



If you pass that function the colour "Red" and the make "Toyota" it should output the following:
Colour: Red - Make: Toyota

If this is not what you're looking for, I'm sorry, but you'll have to rephrase your question to make it a little more clear.
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: wanting to output 2 methods called type, to return make and colour

Posted 04 August 2013 - 02:53 AM

I wouldn't use Console.Write in the type method, you did good Matt but remove the Console.Readline() part and use return instead. Methods with return type other than void should always return something(like if the type is string it needs to return string).
After you instantiate your class and your color and make properties like OP did on line 13 - 16 you can print that like
Console.WriteLine(subaru.make); Console.Writeline(subaru.color);  


If you want to use the Type method try something like this.

public Vehicle GetVehicleType(string VehicleMake, string VehicleColor)
{
    //You already made instance, so we are using the existing one.

    subaru.color= VehicleColor;

    subaru.make= VehicleMake;

    return subaru;
}

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: wanting to output 2 methods called type, to return make and colour

Posted 04 August 2013 - 12:03 PM

A property called "type" is just asking for trouble. You know that 'type' is already a keyword in C#, right? You aren't paying for variable names by the byte. Get in the habit of not complicating things with conflicting names like this.

I strongly urge you to read the tutorial on properties linked in my signature block. What you have here is just... odd and hard to make any sense of. Also, look at the tutorial link for objects while you are at it. I use the idea of a Vehicle class as the parent then, inherit for Truck, Car... Then inherit from Truck for DodgeRam... etc.

What the heck is this? You call it 'type' and set the 'color'?? Are you deliberately going out of your way to make it hard on all the developers using this code, including yourself?
74        public string type(string colour)
75        {
76            this.colour = colour;
77            Console.WriteLine(colour.ToString());
78        }



How about a color property?

public string CarColor
{
    get; set;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1