C#
C#

help

Root Question Message

CaptainBulba
CaptainBulba1/30/2023
Storing login data

Hello everyone!

I am trying to build a simple school management system in console. Once a user login I want to store some of the data in order to avoid querying for that data again. What would be the best approach?

I thought of using static such as

 public static class Login
    {
        public static string login { get; set; }
        public static bool teacherFlag { get; set; }
        public static bool adminFlag { get; set; }

    }


But from what I read this approach is not good as multiple applications can mix up the information.
Ero
Ero1/30/2023
"Multiple applications" can't really mess with your information
Ero
Ero1/30/2023
But i wouldn't do it that way either way. You could just have a "currently logged in user" in your main code flow
Ero
Ero1/30/2023
record User(
  string Username,
  bool IsTeacher,
  bool IsAdmin);
CaptainBulba
CaptainBulba1/30/2023
aha, got it. Thanks
CaptainBulba
CaptainBulba1/30/2023
Sorry, just to clarify.

I would still store my User in the static in order to get info from anywhere. Do I follow it correctly?

public static Pet pet;

// somewhere

pet = new Pet()
{
    Name = "Bob",
    Age = 12
};

// later

Console.WriteLine("Welcome " + StartMenu.pet.Name);
Atakan / Cracker
Atakan / Cracker1/30/2023
that would work, of course if you only have one user, otherwise you can use dictionary with userId as key
Atakan / Cracker
Atakan / Cracker1/30/2023
Multiple applications (meaning other exe instances of console app) not have access to other instances so no worries of mixing up between apps. But if you are setting the Pet from different lines of code then that will be overridden
CaptainBulba
CaptainBulba1/30/2023
yep, that makes sense. thanks a lot
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy