Passing If Typeof...Is...X as a parameter

  • Thread starter Thread starter AlexB
  • Start date Start date
A

AlexB

I have a sub routine that performs an operation on all of
the controls on an ASP.NET page using the If TypeOf..Is X
statement. I want to pass the class, X, in as a
parameter. Is this possible?

Sub(byval x as ???)
For i = 0 to Controls.count -1
If Typeof controls(i) Is X then
'Do something
End if
Next
End sub
 
Alex,
Use the alternate form:
Sub(byval x as System.Type)
For i = 0 to Controls.count -1
If controls(i).GetType() Is x then
'Do something
End if

Of course then the types need to match exactly, control cannot be a subtype
of x.

If you care about subtypes, then you can try:

If x.IsAssignableFrom(controls(i).GetType()) Then

Double check the IsAssignableFrom, I may have got x & control's type
backwards.

Hope this helps
Jay
 
Doh!

I should add when you call your sub you need to use GetType(TextBox) to pass
the type of TextBox, replace TextBox with the name of the actual control you
are interested in.

However I'm not entirely sure what you think this is going to buy you...

Hope this helps
Jay
 
Back
Top