473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

optimization

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?&thr...%40tkmsftngp05

I actually tried to understand and apply the solution proposed by Eric
Gunnerson without any success...
Nov 15 '05 #1
7 2057
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(butto n,"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.
"Zoury" <ya*************@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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?&thr...%40tkmsftngp05

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

Nov 15 '05 #2
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)

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #3
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" <ya*************@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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?&thr...%40tkmsftngp05

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

Nov 15 '05 #4
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 [MS]" <er****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <ya*************@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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?&thr...%40tkmsftngp05

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


Nov 15 '05 #5
Zoury <ya*************@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
: 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..).

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #7
Zoury <ya*************@hotmail.com> wrote:
: 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Vinokur | last post by:
For instance, we need to measure performance of assignment 'ch1 = ch2' where ch1 and ch2 are of char type. We need to do that for different optimization levels of the same compiler. Here is...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
5
by: AC Slater | last post by:
Whats the simplest way to change a single stored procedures query optimization level? In UDB8 that is. /F
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
21
by: mjbackues at yahoo | last post by:
Hello. I'm having a problem with the Visual Studio .net (2003) C++ speed optimization, and hope someone can suggest a workaround. My project includes many C++ files, most of which work fine...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
2
by: db2admin | last post by:
hi, I have query which runs great when optimization level is changed to 3 but does not run fine with default optimization level of 5. since this is a query in java code, i do not know how can i...
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.