Forum C# Static const array

Static const array

Postby wgfunstormcontest » September 28th, 2012, 9:03 am

I want this to be available to all classes. I do not need to modify the data within the array.

Code: Select all
String[] AVATARS = { "Boy", "Girl", "Dog", "Etc" };


First I tried to move it to the top, after the namespace and before my class. Nope, can't put it there.

Then I tried to create a 'Constants' class and declare the array as a const. Nope, const doesn't work for arrays.

Then I tried to declare it as static readonly. Nope again, static variables don't work in playerio.

Arghjlasdhasl; how do I make this simple information available to all classes?
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Static const array

Postby Benjaminsen » September 28th, 2012, 11:02 am

As part of the security sandbox and the scaling abstraction the Player.IO server does not support static variables in any form or fashion.

Thus, for you to have data that can be accessed from multiple classes you must pass it to the class instance somehow. I personally simply pass an instance of a singleton class to my constructors when initialising my class instances.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Static const array

Postby wgfunstormcontest » September 28th, 2012, 11:21 am

How can I pass variables to my room class constructor?
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Static const array

Postby Benjaminsen » September 28th, 2012, 11:29 am

wgfunstormcontest wrote:How can I pass variables to my room class constructor?


You would initialise an instance of your singleton in GameStarted as that's your first point of code execution. You could compare it to your constructor.

From here you can pass it to any other constructor used by your game.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Static const array

Postby numagames » October 28th, 2012, 7:08 am

Benjaminsen wrote:
wgfunstormcontest wrote:How can I pass variables to my room class constructor?


You would initialise an instance of your singleton in GameStarted as that's your first point of code execution. You could compare it to your constructor.

From here you can pass it to any other constructor used by your game.


Hi Benjaminsen,

Please Explain how could You implement singleton pattern with no static vars allowed? Classic singleton needs it for implementation:

Code: Select all
using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
numagames
 
Posts: 4
Joined: August 27th, 2012, 6:04 pm

Re: Static const array

Postby Henrik » October 28th, 2012, 11:25 am

numagames wrote:Please Explain how could You implement singleton pattern with no static vars allowed? Classic singleton needs it for implementation:

Instead of having one instance per virtual machine, have one instance per Game instance.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm


Return to C#



cron