2

As you seen in this picture below, for some reason my DirectCast wont except ANYTHING for the second argument. It says it requires a type, but, it won't take any object at all!

enter image description here

Thanks for any help! I'm using VB.net so all .net answers are acceptable :)

EDIT

Ok, so apparently I'm not giving it the right kind of type. Could somebody please clarify this? Assuming the type it needs to cast to is gridElement, what should I replace objType with?

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • 'It says it requires a type, but, it won't take any object at all'; perhaps because it requires _a type_? DbType, is a good example of a type – sehe Sep 23 '11 at 21:14

2 Answers2

7

DirectCast requires an object prototype (i.e. just giving it the intended class name) rather than a System.Type descriptor object. To cast an object using a System.Type, you will want to utilize CTypeDynamic():

Return CTypeDynamic(createElementByIdAndLayer.MemberwiseClone(), objType)

The error is essentially telling you a class with the type name "objType" does not exist.

lsuarez
  • 4,952
  • 1
  • 29
  • 51
  • VS isn't recognizing CTypeDynamic, do I need to reference something? – Freesnöw Sep 23 '11 at 21:02
  • `CTypeDynamic` is part of the .NET 4.0 Framework. I assume you aren't targeting that particular library. – lsuarez Sep 23 '11 at 21:04
  • Hmm... sadly not. I was trying to target the most commonly installed framework. I've got it downgraded all the way to 2.0 I believe... Sorry. +1 anyways. – Freesnöw Sep 23 '11 at 21:13
  • I'm pretty sure you can safely target 3.5 these days. It ships standard with Server 2008 and Windows 7 and is flagged critical update for XP. Unfortunately that won't help this situation anyway. Does your return type conform to an interface? – lsuarez Sep 23 '11 at 21:17
  • It returns a class `gridElement` that inherits from a class called `luaPlugin` that I made. I don't have a current understanding of interfaces, I should probably go read about them... So no, I don't believe that it conforms to an interface. I'll update my project to 3.5 as well... – Freesnöw Sep 23 '11 at 22:45
  • 2
    If you know ahead of time that you always WANT a gridElement and the object can be cast to a gridElement, you can call Return DirectCast(createElementByIdAndLayer.MemberwiseClone(), gridElement) – lsuarez Sep 24 '11 at 01:52
2

Its expecting a "Type", not a "Type Object".

What is the return value of the function?

StingyJack
  • 19,041
  • 10
  • 63
  • 122