Hello there,
I am currently in the process of 'getting strict' with some of our aspx.vb
files. I have a function that is called either when a datarepeater is bound
or when a button on the datareader is selected. The function accepts a
'sender' object, which may be of a different type depending on how the
function is ran.
In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and on
others it is "System.Web.UI.HtmlControls.HtmlTableCell". Where i could simpl
y
use sender.FindControl previously, I now have to check the
'sender.GetType.ToString' value and get each of the controls using if
statements based on this so I can cast my sender object to the correct type.
This has worked but I can't help feeling there must be an easier and neater
way of doing this!
If anybody has any suggestions they'd be gratefully received.
Thanks very much,
Carl HowarthNope. You are on track. You may want to use DirectCast instead as its faster
amongst other things.
Best Regards
The Inimitable Mr Newbie
"Carl Howarth" <CarlHowarth@.discussions.microsoft.com> wrote in message
news:A638D40E-B787-4D4A-8E5D-284E1962A6EC@.microsoft.com...
> Hello there,
> I am currently in the process of 'getting strict' with some of our aspx.vb
> files. I have a function that is called either when a datarepeater is
> bound
> or when a button on the datareader is selected. The function accepts a
> 'sender' object, which may be of a different type depending on how the
> function is ran.
> In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and
> on
> others it is "System.Web.UI.HtmlControls.HtmlTableCell". Where i could
> simply
> use sender.FindControl previously, I now have to check the
> 'sender.GetType.ToString' value and get each of the controls using if
> statements based on this so I can cast my sender object to the correct
> type.
> This has worked but I can't help feeling there must be an easier and
> neater
> way of doing this!
> If anybody has any suggestions they'd be gratefully received.
> Thanks very much,
> Carl Howarth
Carl,
It depends on what you need to access in each control.
All the controls have a base type System.Web.UI.HtmlControls.HtmlControl
which all the other controls inherit from.
If you only need to acces base control properties like: ClientId to
determine which control was clicked then you can cast any HtmlControl like
the repeater or the table cell to a HtmlControl and access those base
properties. If you need to access properties that are specific to that type
of control then you need to cast as you are doing.
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"Carl Howarth" <CarlHowarth@.discussions.microsoft.com> wrote in message
news:A638D40E-B787-4D4A-8E5D-284E1962A6EC@.microsoft.com...
> Hello there,
> I am currently in the process of 'getting strict' with some of our aspx.vb
> files. I have a function that is called either when a datarepeater is
> bound
> or when a button on the datareader is selected. The function accepts a
> 'sender' object, which may be of a different type depending on how the
> function is ran.
> In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and
> on
> others it is "System.Web.UI.HtmlControls.HtmlTableCell". Where i could
> simply
> use sender.FindControl previously, I now have to check the
> 'sender.GetType.ToString' value and get each of the controls using if
> statements based on this so I can cast my sender object to the correct
> type.
> This has worked but I can't help feeling there must be an easier and
> neater
> way of doing this!
> If anybody has any suggestions they'd be gratefully received.
> Thanks very much,
> Carl Howarth
I attended a conference at Tech Ed 2003 where everyone was told to be very
leery of DirectCast. Under the hood CType does a lot of things that protect
your code and DirectCast can cause problems.
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"Mr Newbie" <here@.now.com> wrote in message
news:eWNohES5FHA.2092@.TK2MSFTNGP12.phx.gbl...
> Nope. You are on track. You may want to use DirectCast instead as its
> faster amongst other things.
> --
> Best Regards
> The Inimitable Mr Newbie
>
> "Carl Howarth" <CarlHowarth@.discussions.microsoft.com> wrote in message
> news:A638D40E-B787-4D4A-8E5D-284E1962A6EC@.microsoft.com...
>
Hi,
Thanks for the quick response - I tried the following:
txtExample = CType(CType(sender, HtmlControl).FindControl("txtExample"),
TextBox)
Though unfortunately this gave me a 'Specified cast is not valid' error,
which leads me to assue that the way I had originally dealt with this must b
e
the only option - does that suond about right to you at all?
Thanks, Carl
Carl Howarth
"S. Justin Gengo" wrote:
> Carl,
> It depends on what you need to access in each control.
> All the controls have a base type System.Web.UI.HtmlControls.HtmlControl
> which all the other controls inherit from.
> If you only need to acces base control properties like: ClientId to
> determine which control was clicked then you can cast any HtmlControl like
> the repeater or the table cell to a HtmlControl and access those base
> properties. If you need to access properties that are specific to that typ
e
> of control then you need to cast as you are doing.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Carl Howarth" <CarlHowarth@.discussions.microsoft.com> wrote in message
> news:A638D40E-B787-4D4A-8E5D-284E1962A6EC@.microsoft.com...
>
>
Yes,
don't use DirectCast It's a micro performance optimization you shouldn't do
unless profiling proves that you need it.
" In this case, I know that the thing I'm getting out of the ArrayList is an
Integer, so I skip the helper and go straight to DirectCast. However, I only
suggestion you use this in situations where you know it will work, and you
think you need that extra little bit of performance. In the real world,
you're hardly ever going to notice the difference, so you might as well go
with the more flexible conversion operators like CType, CInt, etc. But when
you identify some place you need that extra little "oomph," it can come in
handy. "
From paul vick, #1 Microsoft VB.Net guy:
http://www.panopticoncentral.net/ar.../07/10/149.aspx
Karl
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!
"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:Of1rAIS5FHA.2484@.TK2MSFTNGP09.phx.gbl...
>I attended a conference at Tech Ed 2003 where everyone was told to be very
>leery of DirectCast. Under the hood CType does a lot of things that protect
>your code and DirectCast can cause problems.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Mr Newbie" <here@.now.com> wrote in message
> news:eWNohES5FHA.2092@.TK2MSFTNGP12.phx.gbl...
>
Carl,
I may have identified the wrong base control. (And as I typed that I
realized I can check the object browser to see what the html repeater and
table cell are inheriting.) I didn't go into enough detail about the control
structure for you. Using the object browser built into VS.NET you can look
at each control's base type. Going backwards through inherited base types
until you find the common control that both types of controls are inheriting
from. In your case you need to cast the two types of controls you mention to
System.Web.UI.Control which by tracing backward both controls are
inheriting.
Of course as I mentioned previously the further back you go the less
properties you have access to. But if the common base type has the
properties you need to access then you can always cast the control to the
common inherited class.
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"Carl Howarth" <CarlHowarth@.discussions.microsoft.com> wrote in message
news:FF278946-86A3-4B15-8364-34743BFE312F@.microsoft.com...
> Hi,
> Thanks for the quick response - I tried the following:
> txtExample = CType(CType(sender, HtmlControl).FindControl("txtExample"),
> TextBox)
> Though unfortunately this gave me a 'Specified cast is not valid' error,
> which leads me to assue that the way I had originally dealt with this must
> be
> the only option - does that suond about right to you at all?
> Thanks, Carl
> --
> Carl Howarth
>
> "S. Justin Gengo" wrote:
>
Problems such as what ?, Ive never had any problems with it and I have used
it extensively.
Best Regards
The Inimitable Mr Newbie
"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:Of1rAIS5FHA.2484@.TK2MSFTNGP09.phx.gbl...
>I attended a conference at Tech Ed 2003 where everyone was told to be very
>leery of DirectCast. Under the hood CType does a lot of things that protect
>your code and DirectCast can cause problems.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Mr Newbie" <here@.now.com> wrote in message
> news:eWNohES5FHA.2092@.TK2MSFTNGP12.phx.gbl...
>
Here are a few explanations:
http://www.panopticoncentral.net/ar.../07/10/149.aspx
http://www.novicksoftware.com/TipsA...
net.htm
http://advisor.com/doc/12798
As you may see from the articles it's easy to get into trouble with
directcast and the performance benefit may be negligible. The rule of thumb
is just that you shouldn't use direct cast if you aren't 100% certain that
an object type will ALWAYS be the same. If you are certain that an object
type will always be the same then it's fine to use it.
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"Mr Newbie" <here@.now.com> wrote in message
news:OK5D$5T5FHA.620@.TK2MSFTNGP10.phx.gbl...
> Problems such as what ?, Ive never had any problems with it and I have
> used it extensively.
> --
> Best Regards
> The Inimitable Mr Newbie
> "S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
> message news:Of1rAIS5FHA.2484@.TK2MSFTNGP09.phx.gbl...
>
Sorry, but not ONE of those articles refers to any 'trouble' with
DirectCast. The only caviat is that you must know what your object type is
before you cast it.
If you test the object type then you can be sure so there is no problem.
If fact all three articles say you can use it, just be sure you know what
your casting.
As I say, I have used DirectCast hundreds or probably thousands of time by
now and never had a problem.
Best Regards
The Inimitable Mr Newbie
"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:u4LM1OU5FHA.476@.TK2MSFTNGP15.phx.gbl...
> Here are a few explanations:
> http://www.panopticoncentral.net/ar.../07/10/149.aspx
> http://www.novicksoftware.com/TipsA...r />
t-net.htm
> http://advisor.com/doc/12798
> As you may see from the articles it's easy to get into trouble with
> directcast and the performance benefit may be negligible. The rule of
> thumb is just that you shouldn't use direct cast if you aren't 100%
> certain that an object type will ALWAYS be the same. If you are certain
> that an object type will always be the same then it's fine to use it.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Mr Newbie" <here@.now.com> wrote in message
> news:OK5D$5T5FHA.620@.TK2MSFTNGP10.phx.gbl...
>
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment