Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current User Global Variable C# Desktop app

I know that you can use session variables in web apps, I can't figure out how to use one in a desktop (windows forms and not a mobile android) one.

I have a Player class that is used to associate players with other classes in the app/SQLDb (they may or may not be the active user). But I want to have a CurrentPlayer (the person who actually has the application running) set.

In the Program class at start up, I'm running checks to see if the current user exists and if not, add them to the SQLdb. But at that time I'd like to set up an application-wide object for the user I've just added. In fact, if they're not in the db they can't run the app.

Obviously static doesn't work because I don't want to make the Player class static all the time. But I really do need to have this object set and the info easily available. EDIT: The reason it can't be static all the time is that in every other instance but the current user, I definitely want the creation of a player object to take place. I guess it's like the old Marine Corps thing. There are many players but this one is mine.

Yes, I know, global variables are not generally good, but here it makes sense (well I think so, if you all have other ideas I'm all ears), I just can't figure out how to implement it.

EDIT: I may not be being clear here. I guess I kind of need a singleton maybe? but I don't know in which file to declare the instance or how.

I have a MYAPPPlayer class that is used generally for many many instances, it can/should not be static (unless I want to be a crappy dev). But I need a MYAPPPlayer global singleton object variable for the current user of a non-web, desktop csharp app.

like image 966
analias Avatar asked Mar 31 '14 19:03

analias


2 Answers

In your application you should have a static User CurrentUser property.

When you log into the application, set the Current User to the instance of that user.

If you are using WPF, I would make this a dependency property and have the rest of your application's interface bind to that property. If you are using forms, you'll have to manually handle the updating of all the UI when the user changes.

Edit: Short example of how something like this could be implemented with a static running program and a static INSTANCE of an object. I would also recommend you read more about what static means, and what a class is, it might be a bit more than you think... I'll explain a bit more at the bottom

class Program
{
    static Player CurrentUser;

    static void Main(string[] args)
    {
        string username;
        bool isValidUser;

        //get and validate user credentials

        if (isValidUser)
            CurrentUser = new Player(username);

        SomeMethod();
    }

    static void SomeMethod()
    {
        if (CurrentUser == null)
            return;

        //do stuff with user
    }
}

public class Player
{
    public string Name { get; private set; }
    //... more properties

    public Player(string name)
    {
        Name = name;
        //... more properties
    }
}

Think about static vs non-static as such..... Non static classes are the blueprints for objects that you can construct and have multiple instances of. A static class is created when you have static members in a class. All the static members are separated out and a single instance of this static class is declared and referenced throughout your entire program whenever you reference one of those static members.... a better example of what I am saying is....

class Program
{
    static Person John;

    static void Main()
    {
        Console.WriteLine("Persons who exist {0}", Person.Total);

        John = new Person("John");
        John.Born();
        John.Birthday();

        Person Jane = new Person("Jane");
        Jane.Born();

        Console.WriteLine("Persons who exist {0}", Person.Total);
        Console.WriteLine("John's Age {0}", John.Age);
        Console.WriteLine("Jane's Age {0}", Jane.Age);

        Console.ReadKey(true); //Pause program
    }
}

class Person
{
    public static int Total { get; private set; }

    public static Person()
    {
        Total = 0;
    }

    public string Name {get; private set;}
    public int Age { get; private set; }

    public Person(string name)
    {
        Name = name;
        Age = 0;
    }

    public void Born()
    {
        Total++;
    }

    public void Birthday()
    {
        Age++;
    }

    public void Death()
    {
        Total--;
    }
}

As you can see above I have a static main method who has access to a John Person. The Person Class is non-static, however it does have a static member. Notice that the instances of Person internally can access the static variables, but from the main method you must say Person.Total to get the total. Also note that the static part of the Person class has no access to any of the instances of Person, but all instances of Person have access to the static members of Person, this is how they can iterate up the total amount of Persons when one is born, or iterate down when one dies.

like image 159
BenVlodgi Avatar answered Sep 30 '22 13:09

BenVlodgi


The CurrentPlayer could be static since it's the current player.

internal class Data
{
     public Player CurrentPlayer { get; set; }
     .... // other variables
}

Usage: Data.CurrentPlayer = ResponseFromWebServiceCall.Player;

There's nothing wrong with having a static variable to represent the current player if there can be only one current player at a time.

Edit, to clarify: If there is only to be a SINGLE representation of an object, then using a static variable to hold that information is perfectly acceptable. In your classes, it's up to you if you want to reference that player variable as I have shown above with usage, or if you want to accept the player variable through a constructor parameter, for example. Actually, doing it that way would give you the best trade-off for portability.

like image 32
Derreck Dean Avatar answered Sep 30 '22 12:09

Derreck Dean