Vaiable without AS

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

In VS 2005 I was using a generic variable for referencing objects such as
textbox, dropdown, calendar controls, etc. using something similar to below.
In VS 2008 the variable is "marked" and assumed an object.

Dim varControl
varControl = row.FindControl("txtExpireDate")

Is there a better way, e.g. using the actual control such as Dim varControl
As TextBox, etc.? Thanks.

David
 
You're using VS2008 (EIGHT) and you still don't have
OPTION EXPLICIT ON
OPTION STRICT ON

Wow.
I repeat.
Wow.

....................

I would research
http://www.google.com/search?hl=en&q=TryCast&aq=f&oq=
TryCast

or

If Not (varControl Is Nothing) then
end if

..................

Dude, dim varControl (as object) ....(the replacement for dim x as variant)
is WAY OUT.
Put your VB6 ways behind you. Its 2009 (t-minus 3 days) for goodness sake.
 
David said:
In VS 2005 I was using a generic variable

There is no such thing. If you don't specify a data type, the data type
used is Object.
for referencing objects such as
textbox, dropdown, calendar controls, etc. using something similar to below.
In VS 2008 the variable is "marked" and assumed an object.

The way that the data type is determined changed in VB 9.0, so that the
data type can be inferred from an initializing value, but if there is no
initializing value the data type is still Object.
Dim varControl
varControl = row.FindControl("txtExpireDate")

Is there a better way, e.g. using the actual control such as Dim varControl
As TextBox, etc.? Thanks.

Yes. You do exactly that, then you cast the reference that you get from
FindControl:

Dim varControl As TextBox
varControl = DirectCast(row.FindControl("txtExpireDate"), TextBox)
 
Back
Top