-
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
-
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.
-
Re: which is better code
I agree with jmcilhinney totally and the second method is also more firendley on the eyes.
-
Re: which is better code
Second method is the best. I agree with jmcilhinney.
-
Re: which is better code
I agree with jmcilhinney. I also think he's way cool. ;)
-
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