|
Hi,
I am a beginner level programmer and I would be happy
if somebody can clarify me this small doubt.
I want to know,if using 'this' to refer to the local variables inside a class is a good practice. using 'this' would be a bit costly or it would infact aid performance ?
Please clarify this , would be more thankful if you can give me some links for improving code performance.
Thanks a ton.
|
|
|
|
|
It doesn't make any difference. Whether you explicitly use "this" or not, the compiler generated code will have to use it to refer to member variables and methods.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
As far as I know, it doesn't hurt or boost the performance. It's rather a question of taste. Do you like the style of using 'this' for calling private variables ?
In my opinion it doesn't matter if you use 'this', but I prefer only using it when there's no other way. An example:
private int var;<br />
<br />
public SomeClass(int var) {<br />
this.var = var;<br />
}
WM.
What about weapons of mass-construction?
|
|
|
|
|
A local variable is one which is within the scope of the method or property, not the class. I think what you mean is member variable (aka field in .NET)
Using the this keyword is just making explicit what the compiler will do implicitly anyway. There is no perfomance advantage to using it or not as the resulting code will be compiled to be the same anyway.
The only time where it does make a difference is where you have a local variable and a member variable with the same name. You must use the this keyword to refer to the member variable in that instance otherwise the compiler will assume you are referring to the local variable.
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
There is no difference in the created IL code wether you use this or not.
(try Lutz Röders excelent Reflector tool to check this).
(field accesses will always resolve to a ldfld opcode and method calls to call or callvirt )
I personally changed my way of using the this keyword.
I started by not using it at all beeing happy not to have to use it as in the old c++ days...
Then i switched to use it on fields and properties. Simpy for readability.
Now i went as far as using it for method calls too.
Even though it seems a little overdone i think the code readability gains a lot.
Especially for everyone who has not written the code but is reading it.
By using the this keyword it is immediatly clear that you refere to a member of the current class.
Currrently i am not as far as using the base keyword for any derieved method.
As long as a method, field or property is not ambigious it is completely up to you and your personal style whether you use the this keyword or not.
/cadi
|
|
|
|
|
Also, using 'this' will give you intellisense to speed your typing, or just to let you browse for the item you are looking for.
|
|
|
|
|
I'm using a System.Timers.Timer to send some data to a serial port every so often. Problem is, if the main thread gets tied up, with internal processing, or hitting a breakpoint for example. A long queue of events gets queued up, then once the app is back in business, all of the messages get handled almost simultaniously. I've tried to come up with a work around, but I'm stumped, any suggestions.
Thanks,
sam
|
|
|
|
|
do not use a timer but use a thread.
Timers use the WM_TIMER message internally. If the message queue dispatching is deleayed (i.e. by some other messages that take a long time to process) a lot of WM_TIMER messages will be queued and then be dispatched all at once.
a thread function could look like this:
private ManualeResetEvent m_Done = new ManualeResetEvent(false);
private int m_Delay = 1000;
public void SendToPort()
{
while (true)
{
this.m_Port.Send(new byte[1]{1});
if (m_Done.WaitOne(this.Delay,false) )
break;
}
}
use the m_Done event to abort the loop.
/cadi
|
|
|
|
|
The OP is using System.Timers.Timer which does not depend on the application running a message pump. But yeah, your idea is right, a thread will solve the problem.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hey everybody.
sorry for the ignorence but I keep on having problems with this stuff.
I'm asking it in a general way, not specific to the app I'm building.
lets say I have 2 window forms, Main (form) and Modal (form).
the Main form has a memeber, lets say "m_strText" which is a public memeber.
now, from the Main form I'm opening the Modal form:
Modal dlgModal = new Modal();
dlgModal.ShowDialog();
at this moment of course the Modal form opens as a modal form.
the thing is that now I want to control the "m_strText" member from the Modal form.
as far as I know you do it like this:
Main dlgMain = new Main();
dlgMain.m_strText..... bla bla bla
but it won't work! because I created a new Main object, and I want to change the one that I am using...
how can I control my Main form without creating a new object?
Thanks!
p.s.
in my case it is a statusbar item that I want to change.
|
|
|
|
|
You just need to pass MainForm to the ModalForm, maybe as a constructor parameter. Like this
Modal dlgModal = new Modal(this);
dlgModal.ShowDialog();
Your Modal class will then look like this
class Modal : Form
{
Form parentForm;
public Modal(Form parentForm)
{
this.parentForm = parentForm;
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thank You!!!
|
|
|
|
|
Try using a third party in this one. A simple class that acts like a messenger between the two objects.
public delegate void MessagePostedEventHandler(object sender,string message);
public class Messenger {
public event MessagePostedEventHandler MessagePosted;
public void PostMessage(String message) {
if(MessagePosted != null) {
MessagePosted(this,message);
}
}
}
There are a few steps to be taken:
1. Create the messenger object
2. Register the MessagePosted event with the main object
3. Create the Modal object and pass the messenger object to the Modal object
4. Call PostMessage in the Messenger object in the Modal object
5. Handle the event in the main object (and change the statustext)
And viola, you have a perfectly working system that posts status messages
in the correct way without much more work
WM.
What about weapons of mass-construction?
|
|
|
|
|
i want reg exp for allow only Numeric value and only one decimal point
this is need for enter the price/rate..
ex- 1000.99,.09 (allow)
and not allowed for any other cherector ex- [ a32.%],100$,}
thanx in advace
uregent
|
|
|
|
|
Try this one:
^\d*\.\d{2}$
this should work fine for you.
/cadi
|
|
|
|
|
Hi you can check www.Regexlib.comfor any regex and you can use the Regulator[^] Expresso[^], Regulator is better as it uses one webservice to search regexlib.com, you will find a repository of regexes there, enjoy it !
Kareem Shaker
|
|
|
|
|
g'day folks,
i am stuck on a small problem relating to the usage of outlook dll in c#. The problem is that I am creating an email using the outlook dll and appending all files listed in a listview as hyperlinks. My copy of outlook displays my signature in every email it creates. Therefore all hyperlinks inserted in the email are inserted at the bottom of the email. What change do I need to make to my code so that it inserts the hyperlinks at the start of the body ?? I am hoping someone could point me in the right direction.
At the bottom is a snipet of my code. As you can see from the code that the line " oMailItem.Display(false);" is repeated twice. The first declaration is done because it displays the email itself and allows the signature in the email to be displayed. If this line is commented then you get an email with no signature but only hyperlinks. Which is not what i want.
I would really appreciate some assistance on this.
thanks
Vee
<code>
private void createEmail(object sender, System.EventArgs e)
{
string InternalPosting ="";
string PostingNotice ="";
string EmailBody="";
string sHtml="";
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
oNameSpace.Logon(null,null,false,true);
oOutlookFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.Display(false);
foreach (ListViewItem lvitem in listView3.Items)
{
int index = int.Parse(lvitem.Index.ToString()); string filename = EMAIL_ATTACH_LIST[index].ToString();
int lastindex = filename.LastIndexOf('\\');
string displayFilename = filename.Substring(lastindex+1, ((filename.Length) - (lastindex+1)));
sHtml = "<html>\n"+ "<head>\n"+ "</head>\n"+ "<body>\n"+
"<a href= "+"\""+InternalPosting+"\">"+displayFilename+"</a>\n"+
"</body>\n"+"<br>\n"+"</html>";
oMailItem.HTMLBody += sHtml;
}
oMailItem.Display(false);
}
</code>
|
|
|
|
|
ekynox wrote:
oMailItem.HTMLBody += sHtml;
Instead, try
oMailItem.HTMLBody = sHtml + oMailItem.HTMLBody;
Does that work?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
hello Senthil
thank you thank you thank you so much. Yes it does work. I couldnt believe it was so simple ..
thanks
Vee
|
|
|
|
|
Hi All
Export to excel data from ultrawebgrid rows, Each data row contain one worksheet in excel sheet like as
row1 data contain sheet1
row2 data contain sheet2
row3 data contain sheet3
this opertaion getting at a time on pressing one button.
we have pressing one button ultrawebgrid rows data to export excel sheet, In excel sheet each worksheet contain one grid row deatails,
for example grid have 10 rows ,pressing any button,
In excel sheet 10 worksheets will occure and each worksheet contain 1 row data of grid pls send me a solution for this example
sunny
|
|
|
|
|
I have created nothing but a new C# Form1. I want to draw an X and Y axis that has an origin at the center of the form. I when the event 'fires' the code works as desired. However, when I resize the form the Y axis no longer is in the center of the Form1. Please help and thanks a lot...
//Problem is here ->
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(Color.Black, 1);
Rectangle rect = new Rectangle();
g.DrawLine(myPen, rect.Size.Width/2, rect.Size.Height, rect.Width/2, rect.Size.Height);
}
//
|
|
|
|
|
Try this in your form's constructor:
<br />
SetStyle(ControlStyles.ResizeRedraw, true);<br />
Wout
www.woutware.com (DXF reading/creating/writing/viewing .NET component)
|
|
|
|
|
Try this :
this.ResizeRedraw = true;
override void Form1_Paint(object sender, Forms.PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black,new rectangle());
}
void Form_Resize()
{
this.Invalidate();
}
|
|
|
|
|
i've changed in my application the standard cursor with my own.
the trouble came when i load it infact it is showed with 0,0 on the top left area of the arrow.
I want set that zone as the central of my own cursor, how can i do?
|
|
|
|
|
If you are referring to the HotSpot in the cursor, cursor editing programs let you specify where this is. VisualStudio.NET's editor I believe offers the feature as well (try right-clicking? - i'm not in front of my system at the moment).
|
|
|
|