473,395 Members | 1,668 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,395 software developers and data experts.

Option Strict With Ctype(sender....

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
Nov 19 '05 #1
13 1623
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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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

Nov 19 '05 #2
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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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

Nov 19 '05 #3
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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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


Nov 19 '05 #4
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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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


Nov 19 '05 #5
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/arc...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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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



Nov 19 '05 #6
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" <Ca*********@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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


Nov 19 '05 #7
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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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



Nov 19 '05 #8
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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



Nov 19 '05 #9
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:u4*************@TK2MSFTNGP15.phx.gbl...
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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



Nov 19 '05 #10
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/de...tinternals.asp
(do a search for DirectCast)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Mr Newbie" <he**@now.com> wrote in message
news:us**************@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:u4*************@TK2MSFTNGP15.phx.gbl...
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in
> message news:A6**********************************@microsof t.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
>
>



Nov 19 '05 #11
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" <he**@now.com> wrote in message
news:us**************@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:u4*************@TK2MSFTNGP15.phx.gbl...
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in
> message news:A6**********************************@microsof t.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
>
>



Nov 19 '05 #12
OK

--
Best Regards

The Inimitable Mr Newbie º¿º
"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:eX**************@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" <he**@now.com> wrote in message
news:us**************@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:u4*************@TK2MSFTNGP15.phx.gbl...
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
> news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in
>> message news:A6**********************************@microsof t.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
>>
>>
>
>



Nov 19 '05 #13
OK

--
Best Regards

The Inimitable Mr Newbie º¿º
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:ua**************@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/de...tinternals.asp
(do a search for DirectCast)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Mr Newbie" <he**@now.com> wrote in message
news:us**************@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:u4*************@TK2MSFTNGP15.phx.gbl...
Here are a few explanations:

http://www.panopticoncentral.net/arc...07/10/149.aspx

http://www.novicksoftware.com/TipsAn...st-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" <he**@now.com> wrote in message
news:OK*************@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:Of**************@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" <he**@now.com> wrote in message
> news:eW**************@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" <Ca*********@discussions.microsoft.com> wrote in
>> message news:A6**********************************@microsof t.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
>>
>>
>
>



Nov 19 '05 #14

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

Similar topics

3
by: Marty | last post by:
Hi, I have this event that is performed by three different controls, how can I know (from sender object) wich control is used to trigger the event? Private Sub HighlightSelectedRow(ByVal...
11
by: tshad | last post by:
I have the following: sub submitQuestion_click(Sender as Object, e as EventArgs) Dim submitButton as Button = CType(sender,Button) This gives me an error that says: "...
8
by: Merlin | last post by:
Hi, I can't remember how to get the sending controls name; used it before but can't find which of my programs has it:- sender.GetType.GetProperty(???????????????? Please help. Thanks,
4
by: Howard Kaikow | last post by:
I am trying to retrive some WMI properties using Option Strict On. This requires the use of InvokeMember. I know that there are alternative ways to get the values, but I want to learn how to...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
2
by: Marcus Kwok | last post by:
I was following the tutorial given at: http://msdn.microsoft.com/netframework/programming/winforms/?pull=/library/en-us/dndotnet/html/mdiappsmenus.asp except writing Managed C++ instead of...
15
by: sam | last post by:
Hi, I have a bound dropdownlist with an event handler OnSelectedIndexChanged. If I select a value I'm redirected to another page. I come back using the browser's back button, and the option is...
4
by: Rob | last post by:
I see that MsgBox(sender.ToString()) will return a string that includes the Text value of a button... Is there a way to return only the Text value (i.e., some additional property designate) ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.