I must admit I am still trying to determine the best way of using statics. I have the following code:Code:using System; using System.Windows.Forms; namespace WindowsApplication1 { internal class Class1 { internal Class1(){} internal static void Main() { Class2.TestString = ""; } } }When I try to run the code I get: An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Class2.TestString' at the line Class2.TestString = "";Code:using System; using System.Windows.Forms; namespace WindowsApplication1 { public class Class2 { string myTestString = null; public string TestString { get {return myTestString;} set {myTestString = value;} } } }
If I change Class2 and the TestString property to a static I am able to compile the code. In VB I was able to add properties to any of my public classes without any problems.
I can use static but I am not sure why I should and why it worked and what repercussions it has on my code. Would somebody be able to explain this to me?




Reply With Quote