0

I have one method as,

public function DoSomethingGenricWithUIControls(ByVal incomingData As Object)
     //Fun Stuff
End Function

This method will be get called and can get passed the Page,UserControl, or any other type.

I want to check the type of incoming object, if it's Page,UserControl or other type.

But I was unable to do that. Whenever I tried using the GetType() of typeOf() on System.Web.UI.UserControl. It gives,

'UserControl' is a type in 'UI' and cannot be used as an expression.

When I tried other methods like .IsAssignableFrom() and .IsSubclassOf() but I was still unable to do that.

One more note my incoming usercontrols or page can be multiple inherited from different controls/pages. So its immediate base type is not that of System.Web.UI.<Type>.

Let me know in case of any confusion. VB/C# any way would work for me.

UPDATE

I have tried like,

 if( ncomingPage.GetType() Is System.Web.UI.UserControl)

This gives me same issue as above,

'UserControl' is a type in 'UI' and cannot be used as an expression.
Mahesh
  • 8,694
  • 2
  • 32
  • 53
  • You can check type with [`is`](https://msdn.microsoft.com/en-us/library/scekt9xw.aspx) or simply **try** to cast with [`as`](https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx) and if it's not `null` - then you have needed type instance. – Sinatr Nov 06 '15 at 07:56
  • I dont want to fall into the `try catch` casting, it s not good practice and I have tried with `Is` but still the same issue – Mahesh Nov 06 '15 at 08:03
  • You don't need `try/catch` when using `as/null` [check](http://stackoverflow.com/a/2139818/1997232). Can you post not working code with `is`? – Sinatr Nov 06 '15 at 08:06
  • [Type Checking: typeof, GetType, or is?](http://stackoverflow.com/questions/983030/type-checking-typeof-gettype-or-is). – Han Nov 06 '15 at 09:05

1 Answers1

3

Instead of

if( ncomingPage.GetType() is System.Web.UI.UserControl)

you have to use

// c#
if( ncomingPage is System.Web.UI.UserControl)
// vb.net fist line of code in my life ever! hopefully will compile
If TypeOf ncomingPage Is System.Web.UI.UserControl Then

Notice absence of getting object type. is does that for you under hoof.

You can check type with simple as/null check pattern:

var page = ncomgingPage as UserControl;
if(page != null)
{
    ... // ncomingPage is inherited from UserControl
}

it is more efficient (only single cast) than using is as you will probably do something like

// checking type
if( ncomingPage is System.Web.UI.UserControl)
{
    // casting
    ((UserControl)ncomingPage).SomeMethod();
    ...
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • When I do the ` If (incomingPage Is System.Web.UI.UserControl) Then` it still gives the error `'UserControl' is a type in 'UI' and cannot be used as an expression.` – Mahesh Nov 06 '15 at 08:37
  • My answer is in `C#`. It seems in vb.net [`Is`](https://msdn.microsoft.com/en-us/library/kb136x1y.aspx) *is* [different](http://stackoverflow.com/q/3167479/1997232), right side expression should be an object. See edit (vb.net part, where I am trying to use `TypeOf`). – Sinatr Nov 06 '15 at 09:00