Prev: simple q
Next: Data sotrage design question
From: csharper on 16 Jun 2010 09:25 I am using Visual Studio 2010 for my web application in C# 4. When I refactor my code to create a method, Visual Studio always generates private static method such as private static void MyMethod(blah blah blah){...} instead of non-static protected method such as protected void MyMethod(blah blah blah){...} Is Visual Studio trying to tell us that we should write private static method instead of non-static protected method for our web application? I am confused.
From: csharper on 17 Jun 2010 00:18 On Jun 16, 9:25 am, csharper <gnewsgr...(a)gmail.com> wrote: > I am using Visual Studio 2010 for my web application in C# 4. When I > refactor my code to create a method, Visual Studio always generates > private static method such as > > private static void MyMethod(blah blah blah){...} > > instead of non-static protected method such as > > protected void MyMethod(blah blah blah){...} > > Is Visual Studio trying to tell us that we should write private static > method instead of non-static protected method for our web > application? I am confused. Looks like I am not the only one who's wondering about this. http://stackoverflow.com/questions/511211/why-does-the-extract-method-command-in-visual-studio-create-static-methods
From: Harlan Messinger on 17 Jun 2010 07:52 csharper wrote: > I am using Visual Studio 2010 for my web application in C# 4. When I > refactor my code to create a method, Visual Studio always generates > private static method such as > > private static void MyMethod(blah blah blah){...} > > instead of non-static protected method such as > > protected void MyMethod(blah blah blah){...} > > Is Visual Studio trying to tell us that we should write private static > method instead of non-static protected method for our web > application? I am confused. It isn't telling you anything of a general nature like that. It has to generate a valid method, so it has to make some initial choice about access modifiers and so on. It makes good sense that private would be the default in this situation, because the code you are extracting from method A in class T into method B is code that, before the extraction, was only executed within method A. Therefore, unless you make any other changes, the new method B will only be called from within method A, so there is no reason why method B should be visible outside of class T. As far as I can tell, the generated method will be static if the code you are extracting contains no mentions of instance members. For example, given this test code: class Class1 { private int i; private string s; public void A() { i = i + 2; s = i.ToString(); System.Diagnostics.Debug.Print("Hello"); System.Diagnostics.Debug.IndentLevel = 7; s = s + s; i = i + 3; i = i + s.Length; s = i.ToString(); i = i + 1; i = i + s.Length; s = i.ToString(); } } When I extract the two lines that invoke Debug members into method B and the three lines after it into method C, I get class Class1 { private int i; private string s; public void A() { i = i + 2; s = i.ToString(); B(); C(); s = i.ToString(); i = i + 1; i = i + s.Length; s = i.ToString(); } private void C() { s = s + s; i = i + 3; i = i + s.Length; } private static void B() { System.Diagnostics.Debug.Print("Hello"); System.Diagnostics.Debug.IndentLevel = 7; } It figures that if the code makes no use of the instance, then the method created from it doesn't need to be instance-specific.
From: Mr. Arnold on 17 Jun 2010 20:01 csharper wrote: > On Jun 16, 9:25 am, csharper <gnewsgr...(a)gmail.com> wrote: >> I am using Visual Studio 2010 for my web application in C# 4. When I >> refactor my code to create a method, Visual Studio always generates >> private static method such as >> >> private static void MyMethod(blah blah blah){...} >> >> instead of non-static protected method such as >> >> protected void MyMethod(blah blah blah){...} >> >> Is Visual Studio trying to tell us that we should write private static >> method instead of non-static protected method for our web >> application? I am confused. > > Looks like I am not the only one who's wondering about this. > > http://stackoverflow.com/questions/511211/why-does-the-extract-method-command-in-visual-studio-create-static-methods > It doesn't matter Java or .NET OOP is OOP (Object Oriented Programming). If the method was public static, then another class can access the public static method without having to instantiate an class as an instance of an object first, before accessing the public method in the class/object. http://leepoint.net/notes-java/flow/methods/50static-methods.html Why declare a method static? The above mean() method would work just as well if it wasn't declared static, as long as it was called from within the same class. If called from outside the class and it wasn't declared static, it would have to be qualified (uselessly) with an object. Even when used within the class, there are good reasons to define a method as static when it could be. �Documentation. Anyone seeing that a method is static will know how to call it (see below). Similarly, any programmer looking at the code will know that a static method can't interact with instance variables, which makes reading and debugging easier. �Efficiency. A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the method. http://www.eggheadcafe.com/articles/20020206.asp You access static methods and fields associated with a class differently that you access them as objects (instances of a class). Instead of specifying the name of the instance to access the method (cp1.GetMaxCashAdvance) you will specify the name of the class: CreditCardAccountPremium.GetMaxCashAdvance(). The static modifier can be used with fields, methods, properties, operators, and constructors, but cannot be used with indexers, destructors, or types. C# trashes the whole concept of global functions, variables, and constants. Instead, you can create static class members, making your C# code not only easier to read, but less likely to cause naming and other conflicts.
|
Pages: 1 Prev: simple q Next: Data sotrage design question |