Option Strict With Ctype(sender....

G

Guest

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
 
M

Mr Newbie

Nope. You are on track. You may want to use DirectCast instead as its faster
amongst other things.
 
S

S. Justin Gengo

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
 
S

S. Justin Gengo

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
 
G

Guest

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
 
K

Karl Seguin

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/archive/2003/07/10/149.aspx

Karl
 
S

S. Justin Gengo

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
 
M

Mr Newbie

Problems such as what ?, Ive never had any problems with it and I have used
it extensively.
 
S

S. Justin Gengo

Here are a few explanations:

http://www.panopticoncentral.net/archive/2003/07/10/149.aspx

http://www.novicksoftware.com/TipsAndTricks/tip-visual-basic-ctype-directcast-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
 
M

Mr Newbie

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.
 
K

Karl Seguin

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...tml/vbtchmicrosoftvisualbasicnetinternals.asp
(do a search for DirectCast)

Karl
 
S

S. Justin Gengo

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
 

Ask a Question

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

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

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top