|
-
May 1st, 2007, 10:14 PM
#1
Thread Starter
Fanatic Member
which is better code
Look at the code below.
Code:
((Form1(this.Owner).textBox1.Text = "new value";
((Form1(this.Owner).textBox2.Text = "new value";
((Form1(this.Owner).textBox3.Text = "new value";
vs
Code:
Form1 f1 = (Form1)this.Owner;
f1.textBox1.Text = "new value";
f1.textBox2.Text = "new value";
f1.textBox3.Text = "new value";
thanks
-
May 1st, 2007, 10:30 PM
#2
Re: which is better code
The second one because it's less resource intensive. If you have to use the same property value more than once, in your case Owner, it's more efficient to assign it to a local variable and use that. That's because a property is actually a get and a set method, so if you're accessing the same property twice you're calling the same method twice. Calling a method is more resource intensive than creating a local variable.
Having said that, if you're only using the property value twice then you might choose to forgo the local variable for brevity. Any more than twice though and I'd definitely use the variable.
-
May 2nd, 2007, 10:55 AM
#3
Fanatic Member
Re: which is better code
I agree with jmcilhinney totally and the second method is also more firendley on the eyes.
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
May 3rd, 2007, 02:23 AM
#4
Registered User
Re: which is better code
Second method is the best. I agree with jmcilhinney.
-
May 3rd, 2007, 03:09 AM
#5
Re: which is better code
I agree with jmcilhinney. I also think he's way cool.
-
May 4th, 2007, 07:17 PM
#6
Hyperactive Member
Re: which is better code
jmcilhinney is there anything you don't know???
I put you in my "cool book" a long time ago!
Jennifer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|