What is the C # equivalent for CType in VB.NET?

I am trying to convert the example presented in the MSDN article Creating Dynamic Data Entry User Interfaces in C #, but I am stuck in the following code

CType(dq, IUIBuildingBlock).QuestionText = reader("QuestionText") 

How to convert the above VB.NET statement to C #?

+7
casting vb.net-to-c # ctype equivalent
source share
1 answer

In C #, you can specify a cast by placing the type you want to apply in brackets in front of the reference variable you want to distinguish ( (type)instance ).

So, to pass an object ( dq ) to an IUIBuildingBlock type, you can use the following code:

 ((IUIBuildingBlock)dq).QuestionText = reader("QuestionText"); 

(Note that this will throw an exception if the throw is made for an object that does not implement IUIBuildingBlock , but will be CType , so I assume this is not a problem.)

+9
source share

All Articles