how can i send values that the user input, to my constructor in another class. because im trying to achieve this but i cant seem to get it.
for example
class car
{
public car(string x, int y)
{
console.write("This is your favorite car: " + x);// here i want to show the values the user input.
console.write("This is your favorite year: " +y);
}
class main
{
string x;
int y;
car brand = new car (x, y)//here i want the values that the user input to be sent to my constructor in the class car.
console.write("whats you favorite car: ");
brand.x = console.readline();//here i want the user to input any car he wants
console.write("what year is your favorite car: ");
brand.y = double.parse(console.readline());//here i want the user to input any year he wants
}
Loading
Sanjeeb LenkaPosted Oct 9, 2013, 12:55 AM
try this:
using System;
class Car
{
public Car(string x, int y)
{
Console.WriteLine("This is your favorite car: " + x);
Console.WriteLine("This is your favorite year: " + y);
}
}
class MainCar
{
static void Main()
{
Console.WriteLine("whats you favorite car: ");
string x = Console.ReadLine();//here i want the user to input any car he wants;
Console.WriteLine("what year is your favorite car: ");
int y = int.Parse(Console.ReadLine());//here i want the user to input any year he wants
Car brand = new Car(x, y);//here i want the values that the user input to be sent to my constructor in the class car.
Console.ReadLine();
}
}