Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Conversion of Types

Status
Not open for further replies.

vbkris

Programmer
Jan 20, 2003
5,994
IN
hi,

some controls have common properties like Attributes property of textbox, hidden boxes etc.

now i want to write a script that reads through the entire page and gets all the controls in the page.

what i want to know is, is there a way to automatically convert them rather than something like this:

if(typeof ChildControl is TextBox) then
ChildControl=ctype(ChildControl,TextBox)
elseif(typeof ChildControl is HtmlInputHidden) then
ChildControl=ctype(ChildControl,HtmlInputHidden)
end if

the code that i have provided is just a sample listing of the controls that i intend to cover.

what i am looking for is:
ChildControl=ctype(ChildControl,GetControlTypeDynamically)

one more thing. i tried this:
ChildControl=ctype(ChildControl,ChildControl.GetType())

this gave me an error. on searching on the net i got some info that said that this is a compile time info and therefore cannot be used in the code...

Known is handfull, Unknown is worldfull
 
The GetType method should return a String of the control's type. You should be able to use it to cast a control. Also, have a look at the TryCast method which won't error if it fails (it will simply return Nothing).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>>You should be able to use it to cast a control

thats what i thought too, but that didnt work :-(

will look into the try cast method, but from the first look that i had it also requires that i explicitly set the control type:
ChildControl=TryCast(ChildControl,HtmlInputHidden)

what i was looking was:
ChildControl=ctype(ChildControl,AutoGetType)

so that i need not have a huge list of Ifs.

:-(

Known is handfull, Unknown is worldfull
 
The cast method does work as in it will return a String of a control. What it's maybe getting confused at, is that you are trying to cast the same control to it's own type.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
could u give me a sample code?

Known is handfull, Unknown is worldfull
 
Well, you won't be able to do exactly what you want as the CType function states:
CType is compiled inline, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to accomplish the conversion.
But, in your example you were trying to do this (it wasn't that the GetType method fails) and you were also casting the same object to itself (which seems strange anyway!). So, an example of how the GetType method would work would be:
Code:
        Dim myControl As Control
        For Each ctl As Control In Me.Controls
            Select Case ctl.GetType.ToString.ToUpper
                Case "SYSTEM.TEXTBOX"
                    myControl = New Control
                    myControl = CType(ctl, TextBox)
            End Select
        Next
It's still not dynamic in that you'll have to know which types you are expecting, but it shows how a different object should be used in conjunction with the GetType method.

As for dynamically assinging a type, I'm pretty sure there isn't an easy method (although there may be a way through reflection or other similar methods that someone else could point out).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
k. thanks. guess there is not other go...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top