How to return Struct From Function in C #?

I defined this structure:

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; }; 

Now I want to use it in my function, and then RETURN IT

  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; } 

as I said, I want to know what type I should determine, my function can return a struct

+4
source share
2 answers
 public Averages AssignValues() 

write it, you can return Structs in the same way as classes, but remember that the fields in the structures are initialized with default values, so your struct definition should be:

 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; }; 

Therefore, when you write Avereges a = new Avereges()a.Buy_Gram_24 , it will be 0, because the value of ints int is by default.

+6
source

Add the name of your structure:

 public Averages AssignValues() { 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; } 
+7
source

All Articles