0

What's the best practice for holding a variable from startup for use in later forms?

For example, I have a username I need to store for logging if they made any changes to records. The application has multiple forms and it doesn't seem practical to keep passing it back and forth.

Jordan Gray
  • 16,306
  • 3
  • 53
  • 69

3 Answers3

2

You can keep these variables as static members anywhere in your assembly and use them anywhere, without having to use a singleton, or else, use something like a static configuration class, but if you really want to get this right, I would recommend a user related session object, that could be returned using a singleton just like Pierre said.

UPDATE

Someone who did not seem to read the whole post downvoted my answer, so I thought I should clarify it a bit more. There are no issues in using static variables if you know what you're doing and that applies to everything, but in this case I even said that I would use a non-static user related object, so I hope the OP does a wise choice.

MeTitus
  • 3,390
  • 2
  • 25
  • 49
  • down voting as static members used without careful consideration can cause issues later on - see question http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil/7027536#7027536 – Johnv2020 Jun 18 '13 at 21:57
  • Your downvote was rather pathetic because I pointed out the right way of doing that. And being a Java/C# developer since 2001 I know the "dangerous" or using static members. Write something constructive instead. – MeTitus Jun 18 '13 at 22:01
0

Why not using the current infrastructure available to you such as Properties.Settings.Default which is a singleton by the way ?

http://msdn.microsoft.com/en-us/library/bb397750.aspx

aybe
  • 15,516
  • 9
  • 57
  • 105
0

I would recommend not using anything static quite easily. First think, when are these variables going to be needed, and, who(or what) is going to need them ? Could you not use a class that contains them, and instanciate it once, and share it between the objects that need it ?

If it really is required everywhere, and "should" be static, then maybe make it static in some helper class.

squelos
  • 1,189
  • 6
  • 16