• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

Error: Expression is not a method


From the below code, the problem I'm having is on the second last line starting with DirectCast - I'm placing an object into CObjectManager and attempting to get it back casting it to a CTest class but the error I'm getting is:

"Expression is not a method."

I would appreciate some insight.

Public Interface IObject
End Interface
Public Class CTest
    implements IObject
    //Misc methods.
End Class
Public Module CObjectManager
    Private activeObject As IObject
    //set and get methods for activeObject
End Module
Public Sub Main()
    Dim testObject as New CTest
    DirectCast(CObjectManager.getActiveObject(), CTest).CTestMethods()
End Sub

Open in new window

  • 4
  • 2
1 Solution
the way you have formatted
DirectCast(CObjectManager.getActiveObject(), CTest).CTestMethods()
is what you do to run a METHOD i.e.... Screen().ClearScreen()  would be a method to clear the screen (A Method does something)

you need to format an expression for example

SOMETHING = DirectCast(CObjectManager.getActiveObject(), CTest).CTestMethods()

hope this helps

Is CTestMethods your data type you want the data to be mapped into.

if it is then you need

CTestMethods = DirectCast(CObjectManager.getActiveObject(), CTest)
Unimatrix_001Author Commented:
Hi there,

Hm, I am not understanding this very well. See, my class is CTest that's what I'm wanting to typecast into. So I could do something such as:

CTest returnedValue=CObjectManager.getActiveObject()

But clearly I cannot do that (can I???) because getActiveObject returns an IObject, which needs to be cast.

Thank you,
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

I am presuming your error is during compiling and not during run time

as DirectCast can compile but throw a runtime error if the data types are not compatible for casting.

so try the following as you cant cast into a data type only into a variable of that data type.

Dim MyVar as CTest;

MyVar = DirectCast(CObjectManager.getActiveObject(), CTest)

oh I read your code now

you want it in variable testObject right?

testObject = DirectCast(CObjectManager.getActiveObject(), CTest)
Unimatrix_001Author Commented:
That's lovely - thank you. :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now