Error: Expression is not a method

Posted on 2009-04-18
Last Modified: 2012-05-06

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

Question by:Unimatrix_001
    LVL 4

    Expert Comment

    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

    LVL 4

    Expert Comment

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

    if it is then you need

    CTestMethods = DirectCast(CObjectManager.getActiveObject(), CTest)
    LVL 3

    Author Comment

    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,
    LVL 4

    Accepted Solution

    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)

    LVL 4

    Expert Comment

    oh I read your code now

    you want it in variable testObject right?

    testObject = DirectCast(CObjectManager.getActiveObject(), CTest)
    LVL 3

    Author Closing Comment

    That's lovely - thank you. :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now