Vaiable without AS

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
 
S

sloan

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

Göran Andersson

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)
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top