3

I defined a struct like this :

 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 kind i should define my function to can return struct

2
  • this sounds a lot like a place where you'd have a class.. Commented Sep 19, 2013 at 8:55
  • Little notification: C# doesn't use functions. It uses Methods Commented Jul 5 at 12:45

2 Answers 2

9

Add the name of your struct:

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

maybe Averages GoldValues = new Averages();
I Used public Averages AssignValues() { Averages GoldValues; GoldValues.Buy_Gram_18 = 0; return GoldValues; } As you said
That's something else, add in your function : Averages GoldValues = new Averages();
can you just tell me what is the diffrence between i declare variable like Averages G; or Averages G=new Averages();
8
public Averages AssignValues()

write it, you can return Structs just like classes, but remember that fields in structs are initalized with default values, so your definition of struct 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;
};

So when you write Avereges a = new Avereges() -> a.Buy_Gram_24 will be 0 because thats int's default value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.