Error: Expression is not a method

Hi,

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.

Thanks,
Uni
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
    CObjectManager.setActiveObject(testObject)
    DirectCast(CObjectManager.getActiveObject(), CTest).CTestMethods()
End Sub

Open in new window

LVL 3
Unimatrix_001Asked:
Who is Participating?
 
The_KingCommented:
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)



0
 
The_KingCommented:
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

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

if it is then you need

CTestMethods = DirectCast(CObjectManager.getActiveObject(), CTest)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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,
Uni
0
 
The_KingCommented:
oh I read your code now

you want it in variable testObject right?

testObject = DirectCast(CObjectManager.getActiveObject(), CTest)
0
 
Unimatrix_001Author Commented:
That's lovely - thank you. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.