optimization

Z

Zoury

Hi everyone! :O)

I need a C# equivalent for the VB's CallByName() function.. I've looked at
the Delegate information in the MSDN and I'm familiar with Reflection (i've
listed the members of an assembly). I'm quite new to all these topics though
and I couldn't figured out the best way to do the following :


/***/
private void Form1_Load(object sender, System.EventArgs e)
{
Hashtable htProperties = new Hashtable();
htProperties["Text"] = "Salut!";
htProperties["BackColor"] = Color.DarkBlue;
htProperties["ForeColor"] = Color.Blue;
htProperties["Font"] = new Font("Comic Sans MS", 10, FontStyle.Bold);
ApplyCustomProperties(htProperties, label1);
}
private void ApplyCustomProperties(
System.Collections.Hashtable ht,
System.Windows.Forms.Label lbl)
{
foreach(DictionaryEntry entry in ht)
{
switch(entry.Key.ToString())
{
case "Text":
lbl.Text = entry.Value.ToString();
break;
case "BackColor":
lbl.BackColor = (Color)entry.Value;
break;
case "ForeColor":
lbl.ForeColor = (Color)entry.Value;
break;
case "Font":
lbl.Font = (Font)entry.Value;
break;
default: break;
}
}
}
/***/

I'll actually need to implement a similar "system" for a couple of homemade
control. We'll pass the hashtable or whatever else data structure to our
control in order to set it's properties. As I said I've take a look at the
delegates but it seems to be way to much overhead for my needs.

thanks in advance for the input :O)

--
Best Regards
Yanick Lefebvre

ps : I've read the following thread :
http://groups.google.com/groups?&threadm=OJTIoYp1BHA.2536@tkmsftngp05

I actually tried to understand and apply the solution proposed by Eric
Gunnerson without any success...
 
D

Daniel O'Connell

If i undersatnd you correctly, this is the answer i'd give:
Instead of reinventing the wheel(i'm sure the CallByName function uses
reflection itself), why not just reference the vb runtime and call the
CallByName Function?

Button button = new Button();
Microsoft.VisualBasic.Interaction.CallByName(button,"Text",Microsoft.VisualB
asic.CallType.Set,"MyText");



It will work fine from within C#, as it is just another class, i doubt there
is any significant overhead that makes it unusable. It is visible slower on
my machine(the time between clicking the button and having the text change
was noticable). It wouldn't be too difficult to write an equivilent method
using reflection, but i wouldn't expect much of a performance boost,
reflection simply isn't that fast.
 
Z

Zoury

Hi Daniel! :O)
If i undersatnd you correctly, this is the answer i'd give:

and you did.. ;O)
It will work fine from within C#, as it is just another class, i doubt there
is any significant overhead that makes it unusable. It is visible slower on
my machine(the time between clicking the button and having the text change
was noticable).
It wouldn't be too difficult to write an equivilent method
using reflection, but i wouldn't expect much of a performance boost,
reflection simply isn't that fast.

Well that a sad news.. :O/

The control I mentionned are actually web controls and they might be a lot
of them on page so speed might become an issue..

I'll try and benchmark both methods though. thanks for the input. :O)
 
E

Eric Gunnerson [MS]

Zoury,

As you noted in one of your other posts, you're concerned about performance.
If that's true, you should see if you can use an interface-based approach
rather than a lookup-by-name approach. If one of the web objects implements
one of your interfaces, you can cast it to that interface and then call
through the interface.

If you still need to go call-by-name, I think you can get some speedup by
creating a delegate based upon the MethodInfo of the method, and then
calling through that.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Z

Zoury

Hi Eric!

I'm using a switch statement to do it, since I haven't enough time for now
to explore others ways. I'll take a look at your propositions has soon as
possible. :O)

I do got another question though, will the solutions proposed be faster at
the execution the switch statement? It seems to me that it may take more
time (cpu cycle) to get the results..

Thanks for your input!

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Eric Gunnerson said:
Zoury,

As you noted in one of your other posts, you're concerned about performance.
If that's true, you should see if you can use an interface-based approach
rather than a lookup-by-name approach. If one of the web objects implements
one of your interfaces, you can cast it to that interface and then call
through the interface.

If you still need to go call-by-name, I think you can get some speedup by
creating a delegate based upon the MethodInfo of the method, and then
calling through that.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
Zoury said:
Hi everyone! :O)

I need a C# equivalent for the VB's CallByName() function.. I've looked at
the Delegate information in the MSDN and I'm familiar with Reflection (i've
listed the members of an assembly). I'm quite new to all these topics though
and I couldn't figured out the best way to do the following :


/***/
private void Form1_Load(object sender, System.EventArgs e)
{
Hashtable htProperties = new Hashtable();
htProperties["Text"] = "Salut!";
htProperties["BackColor"] = Color.DarkBlue;
htProperties["ForeColor"] = Color.Blue;
htProperties["Font"] = new Font("Comic Sans MS", 10, FontStyle.Bold);
ApplyCustomProperties(htProperties, label1);
}
private void ApplyCustomProperties(
System.Collections.Hashtable ht,
System.Windows.Forms.Label lbl)
{
foreach(DictionaryEntry entry in ht)
{
switch(entry.Key.ToString())
{
case "Text":
lbl.Text = entry.Value.ToString();
break;
case "BackColor":
lbl.BackColor = (Color)entry.Value;
break;
case "ForeColor":
lbl.ForeColor = (Color)entry.Value;
break;
case "Font":
lbl.Font = (Font)entry.Value;
break;
default: break;
}
}
}
/***/

I'll actually need to implement a similar "system" for a couple of homemade
control. We'll pass the hashtable or whatever else data structure to our
control in order to set it's properties. As I said I've take a look at the
delegates but it seems to be way to much overhead for my needs.

thanks in advance for the input :O)

--
Best Regards
Yanick Lefebvre

ps : I've read the following thread :
http://groups.google.com/groups?&threadm=OJTIoYp1BHA.2536@tkmsftngp05

I actually tried to understand and apply the solution proposed by Eric
Gunnerson without any success...
 
J

Jon Skeet

Zoury said:
I'm using a switch statement to do it, since I haven't enough time for now
to explore others ways. I'll take a look at your propositions has soon as
possible. :O)

I do got another question though, will the solutions proposed be faster at
the execution the switch statement? It seems to me that it may take more
time (cpu cycle) to get the results..

I suspect the switch way will probably be pretty optimal - but have you
actually tested to make sure that it's this precise part of your code
which is the bottleneck? Just how often are you calling this method?
 
Z

Zoury

: This process will be done each time a page is request for each controls on
it.

Actually it's each time the page is *loaded*. I'm not to familiar with web
concepts here but we have to keep an history of the user navigation, like in
the msdn libary. So I guess that each time a page is removed from the
history we'll unload it and reload it (using the whole fillControl()
process) if the user needs it. (I don't know if you have such control over a
WebForm..).
 
J

Jon Skeet

Zoury said:
: This process will be done each time a page is request for each
: controls on it.

Actually it's each time the page is *loaded*.

Right. And have you measured how long it takes, and found that to be
the bottleneck? I'd *very* much doubt that it is. Don't worry too much
about micro-optimization until you've found out for sure what's taking
the time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top