0

Is there a way to have something similar to a web session in c#?

For example if I set a integer somewhere integer id = 5; Could I access that integer at a later time in a different class?

I am writing an app that communicates with a web service and I need to keep the id of the authenticated user around for queries to the web.

Or is my best option to use a database and store the id and access it from the database when needed.

16
  • 2
    Classes can have properties, and properties can hold data. Object Oriented Design allows for the passing of objects through your application. If you just want some big global variable, thats available too, but not recommended. Commented Aug 7, 2014 at 22:20
  • 2
    What's wrong with having a class that represents your connection to the web service? That class could hold the id. Commented Aug 7, 2014 at 22:20
  • 2
    You could create a dedicated session object that represents your session and pass that along where needed. Commented Aug 7, 2014 at 22:22
  • 2
    @Deekor available where? - in the language itself. A static class with public static properties is probably what @paqogomez meant. I would definitely recommend against that, given your skill level and the debugging nightmare "global variables" like that can introduce. You should invest your time in learning more about OO design and structure. Commented Aug 7, 2014 at 22:35
  • 2
    It's one thing to say you "want to access it at any time" but you're not envisioning correctly how that has to be filled in. Yes, you could make a static class that holds that for you but global state is considered an anti-patten for a reason. What makes you not want to pass it around as an argument? Commented Aug 7, 2014 at 22:38

1 Answer 1

2

Simply enough.

No need for a database. If the only thing that needs to persist is a simple ID, you can save it in a binary file, xml file or plain text.

The best equivalent to session variables I can think of is :

  • Right click on your project
  • Click on properties
  • Click on settings
  • Set you variable there by selecting the type, the name, the scope of the variable (Application or User) and set the value

By the way, the C# type is int

Sign up to request clarification or add additional context in comments.

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.