3

我定义了一个这样的结构:

 public struct Averages
    {
        public decimal Sell_GoldOunce = 0;
        public decimal Buy_GoldOunce = 0;
        public decimal Sell_SilverOunce = 0;
        public decimal Buy_SilverOunce = 0;
        public int Sell_Mazene = 0;
        public int Buy_Mazene = 0;
        public int Sell_Gram_18 = 0;
        public int Buy_Gram_18 = 0;
        public int Sell_Gram_24 = 0;
        public int Buy_Gram_24 = 0;
    };

现在我想在我的函数中使用它然后返回它

    public (?) AssignValues()// I WANT TO KNOW WHAT SHOULD I PUT INSTITE OF (?)
    {
        Averages GoldValues;
        GoldValues.Sell_GoldOunce = somevalue;
        GoldValues.Buy_GoldOunce = somevalue;
        GoldValues.Sell_SilverOunce = somevalue;
        GoldValues.Buy_SilverOunce = somevalue;
        GoldValues.Sell_Mazene = somevalue;
        GoldValues.Buy_Mazene = somevalue;
        GoldValues.Sell_Gram_24 = somevalue;
        GoldValues.Buy_Gram_24 = somevalue;
        GoldValues.Sell_Gram_18 = somevalue;
        GoldValues.Buy_Gram_18 = somevalue;

        return GoldValues;
    }

正如我所说,我想知道我应该定义什么样的函数才能返回结构

4

2 回答 2

8

添加结构的名称:

public Averages AssignValues()
{
    Averages GoldValues = new Averages();
    GoldValues.Sell_GoldOunce = somevalue;
    GoldValues.Buy_GoldOunce = somevalue;
    GoldValues.Sell_SilverOunce = somevalue;
    GoldValues.Buy_SilverOunce = somevalue;
    GoldValues.Sell_Mazene = somevalue;
    GoldValues.Buy_Mazene = somevalue;
    GoldValues.Sell_Gram_24 = somevalue;
    GoldValues.Buy_Gram_24 = somevalue;
    GoldValues.Sell_Gram_18 = somevalue;
    GoldValues.Buy_Gram_18 = somevalue;

    return GoldValues;
}
于 2013-09-19T08:52:00.160 回答
8
public Averages AssignValues()

写它,你可以像类一样返回结构,但记住结构中的字段是用默认值初始化的,所以你对结构的定义应该是:

public struct Averages
{
    public decimal Sell_GoldOunce;
    public decimal Buy_GoldOunce;
    public decimal Sell_SilverOunce;
    public decimal Buy_SilverOunce;
    public int Sell_Mazene;
    public int Buy_Mazene;
    public int Sell_Gram_18;
    public int Buy_Gram_18;
    public int Sell_Gram_24;
    public int Buy_Gram_24;
};

所以当你写Avereges a = new Avereges()->a.Buy_Gram_24将是 0 因为那是 int 的默认值。

于 2013-09-19T08:54:56.083 回答