0

Looking for some assistance on public statics and if they are the cause of an issue I am facing.

On my code page I have;

public static string FirstName;

Further down on the page, I am using this string as one of my SqlCmd Paramaters (AddWithValue). This is working fine and when the record is saved to the database, it is visible there that the FIrstName was entered in properly.

The problem I am now having is when i made the application publicly available, that the FirstName value that gets entered into the database is not mine. It's someone else's and I am unsure how this is happening.

Is the fact I am declaring this as 'Public' mean that the web server makes this value visible and accessible to other people? I thought from reading other SO pages here, that it was only publicly available to the class it is in.

Looking for some assistance on HOW another persons name could be saved against my record.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Stephen85
  • 250
  • 1
  • 15
  • 1
    why do you need to have this property as static? – Stucky Jul 05 '19 at 06:52
  • 5
    Possible duplicate of [Scope of static Variable in multi-user ASP.NET web application](https://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application) . I can't think of any reason why you'd declare such a variable static either. It's clearly an instance value which is meant to associate with a particular postback and to a particular user. It's not a class-wide variable, never mind an application-wide one – ADyson Jul 05 '19 at 06:52
  • 1
    `public` surely means **publicily available to any other class**. When it should be accessable only to the containing class, use `private`` instead. `Static` on the other hand means "the member does not belong to a specific **instance**". You should definitly read abook or tutorial about access-modifiers, or at least the [page from MSDN](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/access-modifiers). – MakePeaceGreatAgain Jul 05 '19 at 06:59
  • public make the variable available to any other class. Not sure what you mean with "visible and accessible to other people": as long as other "people" don't write the code running in your application, they won't change tha value of a variable! Very likely, there is some code in your app changing the value of your your `FileName` static field. – Gian Paolo Jul 05 '19 at 07:31
  • @GianPaolo read the duplicate. This is an issue to do with the way asp.net applications run, in a context where lots of separate users are indeed running the same code in the same block of memory in the same server. Static variables are shared across the whole application (and thus all users). They are not set separately within each user session. The `public` modifier is not the problem here, but the `static` modifier is – ADyson Jul 07 '19 at 08:09

0 Answers0