1

Is it possible to give a start value to a variable that has a get and setter? So it should look something like this:

public static float myVariable = 10 {get; set;};

Thanks in advance.

Edit:

Working in C#

Edit 2:

So I tried this:

public static class GlobalVariables {

    public static float groundSearchRayDistance{get;}

        static GlobalVariables()
    {
        groundSearchRayDistance = 10;
    }
}

But it doesn't work.

Community
  • 1
  • 1
Mike Ottink
  • 1,229
  • 2
  • 23
  • 55
  • 1
    http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value – Joe Feb 24 '16 at 17:35

2 Answers2

0

Dunno what language you are using, but usually you would put that within the constructor. You would just override the constructor of that variable to take a number and make that the initial value. Hope that helps! if not, give details!

Silvertail
  • 169
  • 1
  • 11
  • I am using c# and aaah ofcourse I can use a constructor in a static class as well. Did not think about that. Thanks. – Mike Ottink Feb 24 '16 at 17:45
  • Yea, I'm not hugely experienced in c#, but I'd think that would work. If it does, please mark question as answered :) – Silvertail Feb 24 '16 at 17:50
  • It didn't work :O. I added the thing I tried to my origional question. – Mike Ottink Feb 24 '16 at 17:56
  • 1
    Like I've said, it's been ages since I've done a c-like language, but you may need that to be this.groundSearchRayDistance = 10; so that you are changing it in the actual object instance, not the class. – Silvertail Feb 24 '16 at 18:19
0

Try this:

 public class ClassName{
     public ClassName(){
        myVariable = 10;
    }
    public static float myVariable {get; set;} 
 }
M B
  • 2,241
  • 18
  • 32