|
You only need to use it to disambiguify tell which variable you actually mean. I.e.
private string parameter;
private void Method(string parameter)
{
this.parameter = "Class scope variable";
parameter = "Method scope variable";
} Or when you pass the current instance to a method:
private void Method(MyClass)
{
...
}
private void AnotherMethod()
{
Method(this);
} Using it unnecessarily just clutters the code and makes it harder to read IMO.
All those who believe in psycho kinesis, raise my hand.
My 's gonna unleash hell on your ass. tastic!
|
|
|
|
|
Okay, that makes plenty of sense. Thanks everyone for you input!
|
|
|
|
|
Unfortunately, this is a suggested requirement of FXCOP and it is a very poor programming practice. It is my opinion that the over-user of the this keyword came from ignorant programmers who did not know of the keyboard shortcut for intellisense (ctrl + space) these ignorant programmers then went on to define a standard because they wanted code to be "readable" thus it became a standard. It then made it into FXCOP which is a MS tool and then, because it is an MS tool other companies "architects" decided that if MS does it then it must be good. However, MS code rarely, if ever passed through the recommended FXCOP settings without warnings ... to make a long story short:
Don't use this unless you really need to. The only real good time to use the keyword is when calling a method from another object that required a reference to the calling object such as:
public class Foo{
public Foo(){
Bar bar = new Bar(this);
}
}
public class Bar{
public Bar(Foo foo){}
}
In all other cases where you would be tempted to use the keyword you have inappropriately named your local and member variables. Fix them.
|
|
|
|
|
A lot of MS's generated code uses this.xxx. I don't especially mind it but like you say it's not really necessary. It seems to be a hangover from Java where it appears to be the house style.
Kevin
|
|
|
|
|
Kevin McFarlane wrote: A lot of MS's generated code uses this.xxx
A FormX.Designer.cs is full of it. I guess the principles are:
1. In a container (such as a form) that holds many different objects, explicitly stating the object that you are using a method or property of is probably good practice and leaves no room for ambiguity.
2. It allows for parameters and fields to have the same name (much more likely to occur in auto generated code). If you always access the field with this then there will be no issues in this situation.
Personally, I only use it in my hand written code in case 2 and only where needed.
|
|
|
|
|
Those are good rules of thumb.
Kevin
|
|
|
|
|
I find I use this whenever I refer to a form object ie this.Close() but almost nowhere else, not even when referring to a control on the form, weird!
Still I hated using
Me in VB whereas this in c# doesn't offend me nearly as much!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: keyboard shortcut for intellisense (ctrl + space)
Thanks for the tip! How about this:
private SomeType i{get{return this;}}
public SomeMethod()
{
i.Close();
}
|
|
|
|
|
aspdotnetdev wrote: this (aka Me, aka i)
aka notThat
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
I use it to piss people off.
|
|
|
|
|
Hello once again. I just made a program to draw triangles in a way that makes a nice effect, here's a picture:
http://i276.photobucket.com/albums/kk2/Raztor0/Capture.jpg[^]
Now, the way I'm drawing this is I have an array of 80 points which I'm telling C# to draw with the DrawPolygon command. Can you see that line that goes through the middle of the triangles? That line is a line connecting the first and last point in the array, can I somehow prevent C# from connecting that last line? Is there a different command that lets me connect every point in sequence?
Thanks for your time
|
|
|
|
|
Hi,
a polygon[^] is a closed path by definition, and Graphics.DrawPolygon closes the path for you if you haven't done so yourself. That is all documented in MSDN. The class holds more drawing methods though.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Thank you for the info, but is there a way to connect each point without saying Graphics.DrawLine(pen, point, point)? I will do that if I have to but I really don't want to
|
|
|
|
|
Raztor0 wrote: is there a way to connect each point without ...
yes, and I already said that.
like always, it starts by reading the documentation.
I tend not to spoon feed lazy people.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Ah, alright. I didn't get that, I thought you were trying to say that DrawPolygon is explained there, which made me think that it's explaining what I already know. Thanks anyway, I'll go check it out.
|
|
|
|
|
Okay so I found DrawLines, this is exactly what I need :P Thank you for your help once again
|
|
|
|
|
I want to write a program communicating a modem to send and receive special data(in format of string) to/from other modems.I have some questions:
1- For this program, should I use kdtele library or TAPI?
2- I couldn't find any complete user reference for kdtele! can anyone post me any link or other documentations?
3- After I couldn't work with kdtele, as I said why, I started searching about TAPI and realized there isn't any c# library for it and I have to use Platform Invokation to use tapi3.dll COM component, but here
's my question:
Do I need to PInvoke all c++ functions, messages, members and structs of tapi3.dll which are needed to write my program in c++ and then use the invoked class in c#? or does any one did this PInvoke job?!
|
|
|
|
|
|
Hi,
I have a problem, because I cannot find a method like xmlWriter.WriteAttributeString, which would add an attribute to a single node (see below the underlined node <instdamt> with an attribute Ccy="EUR").
The WriteAttributeString adds nicely an attribute to an element, like for example <Amt Ccy="EUR"> , if I put it after xmlWriter.WriteStartElement("Amt"). But that is not the problem. I will get an exception if I put it after the xmlWriter.WriteElementString("InstdAmt", txtInstdAmt.Text).
So, what should I do to get the attribute added to a node, like the example below? It doesen't seem to be the most common need, so therefore I didn't find much from google either.
<PmtId>
<EndToEndId>20090821-E000001</EndToEndId>
</PmtId>
<Amt>
<InstdAmt Ccy="EUR">100.01</InstdAmt>
</Amt>
<CdtrAgt>
<FinInstnId>
Thanks
Petri
|
|
|
|
|
I don't know much about xmlWriter(DOM in general). Haven't used for any but the simplest things.
This functionnal XLINQ creation/construction sure does work:
XDocument yourDoc=
new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Amt",
new XElement("InstdAmt","101.01")
new XAttribute("Ccy", "EUR")
|
|
|
|
|
Petri Luoto wrote: So, what should I do to get the attribute added to a node,
Use the tools correctly. It won't work the way you are trying because it would produce an invalid document.
writer.WriteStartElement("Amt");
writer.WriteStartElement("InstdAmt");
writer.WriteAttributeString("Ccv", "EUR");
writer.WriteValue("100.01");
writer.WriteEndElement();
writer.WriteEndElement();
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Great!
I obviously tought that you can put WriteAttributeString also after WriteElementString as you can do with WriteStartElement.
This solved my problem, for now
Thanks for prompt answers.
Petri
|
|
|
|
|
Hi all, I have two questions .
1. How do I copy one datagridview to another.
2. how do I sort a specefic column. for example I have
col A, colB, colC. I want to sort colB only when data is loaded.
for copying or loading from one grid to another I have done something like this
and it is working but not sure it is a right thing to do or not.
for (int j = 0; j < dataGridView1.Rows.Count; j++)<br />
{<br />
dataGridView2.Rows.Add();
for (int i = 0; i < 5; i++)<br />
{<br />
dataGridView2.Rows[j].Cells[i].Value = dataGridView1.Rows[j].Cells[i].Value;<br />
}<br />
}<br />
thanks in advance
|
|
|
|
|
Hi, can someone give any advice on the following problem I have in C# 2...
I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx"
so I have "moretexthere" as a result.
The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence.
Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times.
Many thanks for any advice.
|
|
|
|
|
A Regular Expression, or IndexOf and Substring.
|
|
|
|