I have a assignment for my C# course, but I cant figure it out. I want to show the cars I have, but it doesn't work. Can someone please help me ?
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace auto
{
class Program
{
static void Main(string[] args)
{
List<auto> autos = new List<auto>();
auto auto1 = new auto("Lamborgini" , "Aventador" , "2004");
display();
}
void display(List<auto> auto)
{
foreach (auto item in auto)
{
Console.WriteLine("Merk :" + item.merk );
Console.WriteLine("Model :" + item.model);
Console.WriteLine("Jaar :" + item.jaar);
Console.ReadKey();
}
}
}
}
Class:
using System;
using System.Collections.Generic;
using System.Text;
namespace auto
{
class auto
{
public string merk { get; set; }
public string model { get; set; }
public string jaar { get; set; }
public auto(string merk , string model, string jaar)
{
this.merk = merk;
this.model = model;
this.jaar = jaar;
}
}
}
This is the error I get:
Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'auto' of 'Program.display(List)' auto C:\\Users\\nickg\\source\\repos\\auto\\auto\\Program.cs 14 Active
You are going to get a compilation error. So all you need to do is pass the parameter to the function.
static void Main(string[] args)
{
List<auto> autos = new List<auto>();
autos.Add(new auto("Lamborgini" , "Aventador" , "2004"));
display(autos);
}
Make the display method static and pass autos
as the parameter when you call it.
The posted code uses auto
as the namespace, class name and parameter name, so I suspect sooner or later the compiler will get confused. Consider giving them different names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.