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 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...
>> 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
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:
> 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
>
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...
>> 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...
>>> 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,
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:
>> 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
>>
>>
>
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...
>> 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...
>>> 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
>>
>>
Here are a few explanations:
http://www.panopticoncentral.net/ar.../07/10/149.aspx
http://www.novicksoftware.com/TipsA...ast-dot-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...
>>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...
>>>> 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
>>>
>>>
>>
>>
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...ast-dot-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...
>>>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...
>>>>> 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
>>>>
>>>>
>>>
>>>
>>
>>
It's a pointless debate. I've used it too (back when I was doing VB.Net
programming), and then I stopped using it.
To be honest though, I wouldn't recommend to someone on these newsgroups to
start using DirectCast unless they were asking (directly or not) about
performance of conversions. I certainly wouldn't simply recommend it
without making provisions for the possible sideeffects (ie, you lose all the
goodness the other ones provide). It _IS_ a micro optimization
Anyone following this thread should also consider taking a quick glance at:
http://msdn.microsoft.com/library/d...etinternals.asp
(do a search for DirectCast)
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mr Newbie" <here@.now.com> wrote in message
news:usI8aYV5FHA.3312@.TK2MSFTNGP15.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...ast-dot-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...
>>>>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...
>>>>>> 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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
Mr. Newbie,
I'm pretty certain that I didn't say you shouldn't use DirectCast. In fact
here's a quote: "I attended a conference at Tech Ed 2003 where everyone was
told to be very leery of DirectCast." Please note the words: "very leery".
And the reason to be leery is stated in the articles I referenced. Not once
did I say don't use DirectCast. I was just making certain that people new to
this language know that DirectCast has to be used very carefully.
--
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:usI8aYV5FHA.3312@.TK2MSFTNGP15.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...ast-dot-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...
>>>>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...
>>>>>> 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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
OK
--
Best Regards
The Inimitable Mr Newbie
"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:eXHnr0W5FHA.3276@.TK2MSFTNGP10.phx.gbl...
> Mr. Newbie,
> I'm pretty certain that I didn't say you shouldn't use DirectCast. In fact
> here's a quote: "I attended a conference at Tech Ed 2003 where everyone
> was told to be very leery of DirectCast." Please note the words: "very
> leery". And the reason to be leery is stated in the articles I referenced.
> Not once did I say don't use DirectCast. I was just making certain that
> people new to this language know that DirectCast has to be used very
> carefully.
> --
> 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:usI8aYV5FHA.3312@.TK2MSFTNGP15.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...ast-dot-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...
>>>>>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...
>>>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
OK
--
Best Regards
The Inimitable Mr Newbie
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uaxGP4V5FHA.3276@.TK2MSFTNGP10.phx.gbl...
> It's a pointless debate. I've used it too (back when I was doing VB.Net
> programming), and then I stopped using it.
> To be honest though, I wouldn't recommend to someone on these newsgroups
> to start using DirectCast unless they were asking (directly or not) about
> performance of conversions. I certainly wouldn't simply recommend it
> without making provisions for the possible sideeffects (ie, you lose all
> the goodness the other ones provide). It _IS_ a micro optimization
> Anyone following this thread should also consider taking a quick glance
> at:
> http://msdn.microsoft.com/library/d...etinternals.asp
> (do a search for DirectCast)
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
> "Mr Newbie" <here@.now.com> wrote in message
> news:usI8aYV5FHA.3312@.TK2MSFTNGP15.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...ast-dot-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...
>>>>>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...
>>>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
0 comments:
Post a Comment