我收到此错误消息。我想做点什么,但我觉得这完全错了。无论如何,请检查我在下面写的代码。这些代码是荷兰语,但是如果你们不能理解这些代码,我会把它翻译成英语。
欢呼声,
class Program
{
static void Main(string[] args)
{
Pc pc = new Pc();
PcShop pcShop = new PcShop();
Console.WriteLine(pc.GeefOmschrijving());
string result = pcShop.PCToevoegen(pc); //here i am getting the fault message.
switch (result)
{
case "1":
pcShop.PCToevoegen(new Pc("DF6", "i7", 16, 500));
pcShop.PCToevoegen(new Pc("ZER6D23", "i7", 8, 1000));
pcShop.PCToevoegen(new Pc("6SDS", "i7", 32, 2000));
break;
}
Console.ReadLine();
class Pc
{
private string code;
private int geheugen;
private int hardeschrijf;
private string processor;
public string Processor
{
get { return processor; }
set { processor = value; }
}
public int Hardeschrijf
{
get { return hardeschrijf; }
set { hardeschrijf = value; }
}
public int Geheugen
{
get { return geheugen; }
set { geheugen = value; }
}
public string Code
{
get { return code; }
set { code = value; }
}
public Pc()
{
processor = "";
code = "";
geheugen = 0;
hardeschrijf = 0;
}
public Pc(string code, string processor, int geheugen, int hardeschrijf)
{
this.code = code;
this.geheugen = geheugen;
this.processor = processor;
this.hardeschrijf = hardeschrijf;
}
public string GeefOmschrijving()
{
string omschrijving = "Welkom in de PC shop.Kies een van de volgende opties:" + Environment.NewLine;
omschrijving += "1. Overzicht winkel " + Environment.NewLine;
omschrijving += "2. Overzicht - geheugen selectie <Gb> " + Environment.NewLine;
omschrijving +="3. Overzicht - processor type selectie "+ Environment.NewLine;
omschrijving += "4. Overzicht - harde schrijf selectie <Gb>"+Environment.NewLine;
omschrijving +="9. Stoppen ";
return omschrijving;
}
class PcShop
{
Pc pc = new Pc();
private string pcVoorad;
public string PcVoorad
{
get { return pcVoorad; }
set {
pcVoorad = value; }
}
public void PCToevoegen(Pc pc)
{
Console.WriteLine("Code: "+ pc.Code +" CPU "+ pc.Processor + " Geheugen " + pc.Geheugen+ " Harddisk: "+ pc.Geheugen);
return;
}
答案 0 :(得分:2)
让我们知道问题所在的行号是有帮助的......
无论如何,方法PCToevoegen
会返回void
,但您尝试将其分配给string
:
string result = pcShop.PCToevoegen(pc);
你想要建模什么?如果要将PC特性输出为字符串,请在ToString
类的PC
方法中实现。您可以根据其他逻辑将PC添加到商店,不要将其与输出PC文本表示的方法混淆。
答案 1 :(得分:1)
此行不起作用
string result = pcShop.PCToevoegen(pc); //here i am getting the fault message.
因为PCToevoegen
具有void
返回类型(即它不返回任何内容)。您需要更新此方法以将输出的string
返回到控制台,即
public string PCToevoegen(Pc pc)
{
return String.Format("Code: "+ pc.Code +" CPU "+ pc.Processor + " Geheugen " + pc.Geheugen+ " Harddisk: "+ pc.Geheugen);
}
或者只是打电话给方法,不要指望任何东西返回,即
pcShop.PCToevoegen(pc);
答案 2 :(得分:1)
方法
public void PCToevoegen(Pc pc)
不会返回任何内容(void),但您的代码会尝试获取字符串作为结果
string result = pcShop.PCToevoegen(pc);
这会导致错误。
如果你想从上面的方法返回一些内容你应该改变它的签名并返回一个字符串,否则你不应该尝试将一个变量(任何类型)分配给方法的假定返回值
选项1 更改签名并返回字符串
public string PCToevoegen(Pc pc)
{
Console.WriteLine("Code: "+ pc.Code +" CPU "+ pc.Processor + " Geheugen " + pc.Geheugen+ " Harddisk: "+ pc.Geheugen);
return "1";
}
选项2 不要尝试分配void方法的返回值
pcShop.PCToevoegen(pc);
答案 3 :(得分:0)
您从void
返回PCToevoegen
(无),但之后您正尝试使用PCToevoegen
返回值。