我写了一段这样的结构函数,调用时提示 “使用了未赋值的局部变量“myOrder””大家帮忙看看是怎么回事。class Class1
{
struct order
{
public string itemName;
public int unitCount;
public double unitCost;
public string total()
{
return Convert.ToString(unitCost* Convert.ToDouble(unitCount));
}
}
[STAThread]
static void Main(string[] args)
{
order myOrder;
Console.WriteLine("Please give me the unitCount:");
myOrder.unitCount=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please give me the unitCost");
myOrder.unitCost=Convert.ToDouble(Console.ReadLine());
//调用结构函数输出费用合计。
Console.WriteLine(myOrder.total()); }
}

解决方案 »

  1.   

    把public string total改为静态方法试一下
      

  2.   

    静态方法不可以,提示这句出错:
    return Convert.ToString(unitCost * Convert.ToDouble(unitCount));unitCost 和unitCount 是非静态的字段方法或属性。
      

  3.   

    找到问题了,是结构引用时出的错的,改一下就好了。
    重点是order myOrder;这句。
    改为:order myOrder();就好了。
    谢谢大家,还是欢迎指出这是为什么。
      

  4.   

    order myOrder;——>order myOrder = new order();
      

  5.   

    order myOrder;——>order myOrder = new order();