PC Review


Reply
Thread Tools Rate Thread

Why does refactoring generate private static method?

 
 
csharper
Guest
Posts: n/a
 
      16th Jun 2010
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.
 
Reply With Quote
 
 
 
 
csharper
Guest
Posts: n/a
 
      17th Jun 2010
On Jun 16, 9:25*am, csharper <(E-Mail Removed)> 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/5...static-methods

 
Reply With Quote
 
 
 
 
Harlan Messinger
Guest
Posts: n/a
 
      17th Jun 2010
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.
 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      18th Jun 2010
csharper wrote:
> On Jun 16, 9:25 am, csharper <(E-Mail Removed)> 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/5...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/...c-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Refactoring Support: Generate Method Stub Seth Rowe [MVP] Microsoft VB .NET 11 25th Jun 2008 06:51 PM
private static or static private in a field declaration? Charlie@CBFC Microsoft C# .NET 1 1st May 2007 01:26 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger Microsoft ASP .NET 2 2nd Dec 2006 04:46 PM
What is difference between lock(private instance) and lock(private static instance)? nytimescnn@gmail.com Microsoft C# .NET 8 1st Sep 2006 04:49 PM
Why Visual Basic 2005 does not let refactoring as C# does? =?Utf-8?B?YXJpZWxxcw==?= Microsoft Dot NET Framework 3 14th Aug 2006 03:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:06 PM.