{"id":4974,"date":"2014-03-30T17:19:49","date_gmt":"2014-03-30T17:19:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ctype-in-vb-net-with-dynamic-second-parameter-type-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:19:49","modified_gmt":"2014-03-30T17:19:49","slug":"ctype-in-vb-net-with-dynamic-second-parameter-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ctype-in-vb-net-with-dynamic-second-parameter-type-collection-of-common-programming-errors\/","title":{"rendered":"CType in VB.NET with dynamic second parameter (type)-Collection of common programming errors"},"content":{"rendered":"<p>If doesn&#8217;t make sense to cast an object to a variable type (a type that is unknown at compile time). The whole point of casting is to specify the exact type. I suspect, though, that rather than using a <code>Type<\/code> object, what you really need is a generic method or a generic class. For instance:<\/p>\n<pre><code>Public Function DoSomethingGenerically(Of T)(MyParameter As MyType) As T\n    Return CType(MyParameter.MyProperty, T)\nEnd Function\n<\/code><\/pre>\n<p>That&#8217;s a really pointless method, since all it does is cast the property and return it as that type, but it shows how you can cast to type <code>T<\/code> without knowing what, specifically, <code>T<\/code> happens to be. Then, you could call it like this:<\/p>\n<pre><code>Dim x As New MyType()\nDim y As MyType2 = DoSomethingGenerically(Of MyType2)(x)\n<\/code><\/pre>\n<p>That is precisely how the <code>List(Of T)<\/code> class, and other similar generic classes are implemented. Except, instead of making an individual method generic, they make the whole class generic, for instance:<\/p>\n<pre><code>Public Class MyList(Of T)\n    Public Sub Add(item As T)\n        ' ...\n    End Sub\nEnd Class\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If doesn&#8217;t make sense to cast an object to a variable type (a type that is unknown at compile time). The whole point of casting is to specify the exact type. I suspect, though, that rather than using a Type object, what you really need is a generic method or a generic class. For instance: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4974","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4974"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4974\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}