2

I have a WinForms application. In this app I have some Forms and Static class which have a public static property and some public static methods that need the property to be set because the forms use on those methods.

Now, if I set this property in the First Form, so when I use some methods on the Second Form will I get an error or the property is still set (saved the first set on the first form)?

Elior
  • 3,178
  • 6
  • 37
  • 67

4 Answers4

2

Static variable stay saving its value

Maadh
  • 643
  • 4
  • 24
1

The property is still set. This is how static properties are actually work.

Denys Denysenko
  • 7,598
  • 1
  • 20
  • 30
1

Static Fields are shared across the App Domain. As a result, the second form will have access to the data set by the first form.

Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
1

What you really want is a Singleton instead of a static class. There is some overlap, but if I understand your desire correctly, the Singleton is the proper approach. See this SO post: for more info and a code listing: Example of Singleton pattern

Community
  • 1
  • 1
Todd Sprang
  • 2,899
  • 2
  • 23
  • 40
  • +1 on moving the functionality out of the form - it's debatable whether singletons are preferable over static helper classes...though I think for the most part people prefer singletons. This is an interesting post: http://stackoverflow.com/questions/2765060/why-use-a-singleton-instead-of-static-methods – NDJ Mar 12 '13 at 13:21
  • wow! thank you both Todd Sprang and NDJ – Elior Mar 12 '13 at 13:23
  • @NDJ, check out this in-depth discussion: http://leedumond.com/blog/singletons-vs-static-classes/ and even more here: http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp – Todd Sprang Mar 12 '13 at 13:26
  • @ToddSprang for more details, I have a database, and in those two forms I can insert or update data into a different tables, so what I want is to have one instance of the DB ConnectionString and each of the form will call to the appropriate method to insert the data – Elior Mar 12 '13 at 13:29
  • @ToddSprang the first link is broken.. – Elior Mar 12 '13 at 13:31
  • 2
    @Elior: Oh, what you really want to do is build a data-access layer (separate referenced .csproj). Basically something that will provide a method to do things like UpdateCustomers(CustomerInfo customerInfo) { ... }. Here are details in ASP.NET, but the principle applies to WinForms too. http://www.asp.net/web-forms/tutorials/data-access/introduction/creating-a-data-access-layer-cs More details are way beyond the scope of a SO reply. :) Note your app.config allows you to store the connection string in a central place: http://msdn.microsoft.com/en-us/library/vstudio/bf7sd233(v=vs.100).aspx – Todd Sprang Mar 12 '13 at 13:43
  • @ToddSprang so just to be calm, is the static class I created for queries is a good solution? – Elior Mar 12 '13 at 14:03
  • @Elior IMO without a lot more info I would say not. I would build a data-access layer as described. I realize this may be more than you want to do, but it's a decision between good architecture and just-works architecture. Your static class should meet your need, but it's probably not the best approach. – Todd Sprang Mar 12 '13 at 17:43