C
C#2y ago
0_00

❔ working with classes and they arent working

FIRST FILE CODE:
namespace GetAndSetMethods
{
class Automobile
{
private string make;

private string model;
private string vin;
private string color;
private int year;

private string type;
public enum autoType
{
Sedan,
Truck,
Van,
SUV
}
public string GetMake(){
return make;
}
public void SetMake(string newMake){
make = newMake;
}

public string GetModel(){
return model;
}
public void SetModel(string newModel){
model = newModel;
}
public string GetVin(){
return vin;
}
public void SetVin(string newVin){
vin = newVin;
}
public string GetColor(){
return color;
}
public void SetColor(string newColor){
color = newColor;
}
public int GetYear(){
return year;
}
public void SetYear(int newYear){
year = newYear;
}
public string GetType(){
return type;
}
public void SetType(string AutoType){
type = autoType;
}
}
}
namespace GetAndSetMethods
{
class Automobile
{
private string make;

private string model;
private string vin;
private string color;
private int year;

private string type;
public enum autoType
{
Sedan,
Truck,
Van,
SUV
}
public string GetMake(){
return make;
}
public void SetMake(string newMake){
make = newMake;
}

public string GetModel(){
return model;
}
public void SetModel(string newModel){
model = newModel;
}
public string GetVin(){
return vin;
}
public void SetVin(string newVin){
vin = newVin;
}
public string GetColor(){
return color;
}
public void SetColor(string newColor){
color = newColor;
}
public int GetYear(){
return year;
}
public void SetYear(int newYear){
year = newYear;
}
public string GetType(){
return type;
}
public void SetType(string AutoType){
type = autoType;
}
}
}
second file code:
namespace class_assignment;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nCreating the first Automobile\n---------------");
Automobile auto1 = new Automobile("Tesla", "Model X", 2020, "12345", "blue", AutoType.Sedan);
Console.WriteLine($"Make: {auto1.getMake()} \nModel: {auto1.getModel()}\nYear: {auto1.getYear()}\nType: {auto1.getType()} \nVIN: {auto1.getVin()}");
Console.WriteLine($"Color: {auto1.getColor()}");
Console.WriteLine("\nChanging the Colour\n---------------");
auto1.setColor("black");
Console.WriteLine($"Color: {auto1.getColor()}");

Console.WriteLine("\nCreating the second Automobile\n---------------");
Automobile auto2 = new Automobile("Mercedes", "G-Wagon", 2017, "24578", "silver", AutoType.SUV);
Console.WriteLine($"Make: {auto2.getMake()}\nModel: {auto2.getModel()}\nYear: {auto2.getYear()}\nType: {auto2.getType()}\nVIN: {auto2.getVin()}");

Console.WriteLine("\nPrinting Automobile Ages\n---------------");
Console.WriteLine($"Auto1 Age: {auto1.getAutoAge()} years");
Console.WriteLine($"Auto2 Age: {auto2.getAutoAge()} years");

}

}
namespace class_assignment;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nCreating the first Automobile\n---------------");
Automobile auto1 = new Automobile("Tesla", "Model X", 2020, "12345", "blue", AutoType.Sedan);
Console.WriteLine($"Make: {auto1.getMake()} \nModel: {auto1.getModel()}\nYear: {auto1.getYear()}\nType: {auto1.getType()} \nVIN: {auto1.getVin()}");
Console.WriteLine($"Color: {auto1.getColor()}");
Console.WriteLine("\nChanging the Colour\n---------------");
auto1.setColor("black");
Console.WriteLine($"Color: {auto1.getColor()}");

Console.WriteLine("\nCreating the second Automobile\n---------------");
Automobile auto2 = new Automobile("Mercedes", "G-Wagon", 2017, "24578", "silver", AutoType.SUV);
Console.WriteLine($"Make: {auto2.getMake()}\nModel: {auto2.getModel()}\nYear: {auto2.getYear()}\nType: {auto2.getType()}\nVIN: {auto2.getVin()}");

Console.WriteLine("\nPrinting Automobile Ages\n---------------");
Console.WriteLine($"Auto1 Age: {auto1.getAutoAge()} years");
Console.WriteLine($"Auto2 Age: {auto2.getAutoAge()} years");

}

}
my current errors are shown in the screenshots provided...
6 Replies
0_00
0_00OP2y ago
autoType is a public enum
circles.png
circles.png2y ago
Automobile is internal? what error is it also GetType is already a method defined on object i think in SetType AutoType is misspelt and your enum's name should have a capital at the start
Pobiega
Pobiega2y ago
also, you are coding with explicit setter and getter methods, which is common in Java but not done in C#
circles.png
circles.png2y ago
just use properties
Pobiega
Pobiega2y ago
we have "auto properties" for this purpose: public int Year { get; set; } does the same thing but better. also, GetType is a reserved method name, so I would avoid naming your own method that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server