A Basic Question

  • Thread starter Thread starter sherri
  • Start date Start date
S

sherri

I was wondering what the difference was between calling a method this
way:
SomeMethod()

and this way:
this.SomeMethod();

when calling one method from another within a form. Is one preferred
over the other? When should one be used over the other?

Thanks!

S
 
Hello sherri,

"this" means the same class.
Use direct method calls without this.

"this" solves ambiguity, for example you have class with 2 variables, method
CallMe() that takes the similiar variables as params and you need to assign
method param to the class variable

class MyClass
{
private string variable1;
private string variable2

public void CallMe(string variable1)
{
// which variable to assign?
// variable1 = variable1

// this solve this. we assign MyClass.variable1 to the method's variable1
this.variable1 = variable1
}
}

s> I was wondering what the difference was between calling a method this
s> way:
s> SomeMethod()
s> and this way:
s> this.SomeMethod();
s> when calling one method from another within a form. Is one preferred
s> over the other? When should one be used over the other?
s>
s> Thanks!
s>
s> S
s>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Sherri,

I (try to) use only the this if it is needed, than it gives direct the
signal that there is an ambiguity as Michael wrote. I have to admit that
that "try to" is not always fullfiled.

Cor
 
What is a collection of one or more programs designed to complete a specific
task.
A. application
B. program lifecycle
C. windows service
D. web service
 
Back
Top