当前位置: 动力学知识库 > 问答 > 编程问答 >

c# 4.0 - How to assign values to the class attributes in C#?

问题描述:

I'm new to C# programming and trying to write the following program using "Vihicle" interface properties that are inherited in 'Car', 'Truck' classes. The problem I'm facing is this error:

An unhandled exception of type 'System.StackOverflowException' occurred"

I get this while assigning the values to the Car properties. Here is my code:

namespace Inheritance_Assignment_2

{

interface Vihicle

{

string Make

{

get;

set;

}

String Model

{

get;

set;

}

int Year

{

get;

set;

}

void DisplayInfo();

float calculateMileage();

}

class Car : Vihicle

{

private string make;

public string Make // read-write instance property

{

get

{

return Make;

}

set

{

Make = value;

}

}

private string model;

public string Model // read-write instance property

{

get

{

return Model;

}

set

{

Model = value;

}

}

private int year;

public int Year // read-write instance property

{

get

{

return Year;

}

set

{

Year = value;

}

}

public void DisplayInfo()

{

Console.WriteLine(Make);

Console.WriteLine(Model);

Console.WriteLine(Year);

}

public float calculateMileage()

{

Random random = new Random();

float value = random.Next(10, 20);

return value;

}

}

class Truck : Vihicle

{

private string make;

public string Make // read-write instance property

{

get

{

return Make;

}

set

{

Make = value;

}

}

private string model;

public string Model // read-write instance property

{

get

{

return Model;

}

set

{

Model = value;

}

}

private int year;

public int Year // read-write instance property

{

get

{

return Year;

}

set

{

Year = value;

}

}

public void DisplayInfo()

{

Console.WriteLine(Make);

Console.WriteLine(Model);

Console.WriteLine(Year);

}

public float calculateMileage()

{

throw new NotImplementedException();

}

}

class TowingTruck : Truck

{

public string TowingCapacity // read-write instance property

{

get

{

return TowingCapacity;

}

set

{

TowingCapacity = value;

}

}

public void DisplayInfo() // Overrided function of class truck because this function doing some extra printing of

{ //TowingCapacity that is present in this TowingTruck Child of Truck Class

Console.WriteLine(Make);

Console.WriteLine(Model);

Console.WriteLine(Year);

Console.WriteLine(TowingCapacity);

}

public float calculateMileage()

{

throw new NotImplementedException();

}

}

class DeliveryTruck : Truck

{

public string Make // read-write instance property

{

get

{

return Make;

}

set

{

Make = value;

}

}

public string Model // read-write instance property

{

get

{

return Model;

}

set

{

Model = value;

}

}

public int Year // read-write instance property

{

get

{

return Year;

}

set

{

Year = value;

}

}

/*

public void DisplayInfo()

{

Console.WriteLine(Make);

Console.WriteLine(Model);

Console.WriteLine(Year);

}

public float calculateMileage()

{

// throw new NotImplementedException();

return 0;

}

*/

}

class Program

{

static void Main(string[] args)

{

//while (true) // Loop indefinitely

//{

// string name;

// int age;

// double height;

// Console.Write("Enter your name: ");

// name = Console.ReadLine();

// Console.Write("Enter your age: ");

// age = Convert.ToInt32(Console.ReadLine());

// Console.Write("Enter your height: ");

// height = Convert.ToDouble(Console.ReadLine());

// //Print a blank line

// Console.WriteLine();

// //Show the details you typed

// Console.WriteLine( name);

// Console.WriteLine( age);

// Console.WriteLine("Height is ", height);

// Console.WriteLine('\n');

//}

Car C = new Car();

float rnum = C.calculateMileage();

Console.WriteLine("Here is the Milage : " + rnum);

C.Make = System.Console.ReadLine();

System.Console.WriteLine("The employee information:");

System.Console.WriteLine("Employee name: {0}", C.Make);

//Console.Write("Enter your Model : ");

//C.Model = Console.ReadLine();

//Console.WriteLine(C.Model);

//Console.ReadLine();

}

}

}

网友答案:

Look at your properties:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return Make;
    }
    set
    {
        Make = value;
    }
}

When you read a value from Make, it internally reads a value from Make (same with writing a value), which results in an infinite recursion. You need to read/write from the variable which holds the value:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

A property's internal logic can't reference itself. Something actually has to store the value.

Edit: Unless there's any particular reason to use properties like this (such as requiring more logic in the getters/setters), you can just use auto-generated properties to simplify the code. So instead of this:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

You can just use this:

public string Make { get; set; }

The compiler automatically converts the latter into something very similar to the former (maybe with just a different backing variable name).

分享给朋友:
您可能感兴趣的文章:
随机阅读: