Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Why does refactoring generate private static method?

Reply
Thread Tools

Why does refactoring generate private static method?

 
 
csharper
Guest
Posts: n/a
 
      06-16-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
 
      06-17-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
 
      06-17-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
 
      06-18-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

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
private(true) || private :make_time, what does it do? why? Rie! Ruby 3 03-26-2008 12:40 PM
Why is define_method private? Plus,what's the point of private methods? Daniel Finnie Ruby 3 12-16-2006 10:09 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why does DJ v3.7.7.81 generate weird-looking static fields like class$exercise$MyClass and static methods _mthclass$(String)? Bruce .J Sam Java 0 06-16-2005 02:34 AM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM



Advertisments