开发者

My vb 2003 code can't compile

This is my first post. Please forgive me for asking a basic question as I'm new to programming.

I have following code and it just didn't compile

 Module Module1

     Public Sub Test

         dim a as New TestClass() 
         dim b as string
         b = a.ReturnString()

     End Sub

 End Module

Public Class TestCl开发者_运维百科ass

     Public Function ReturnString() as string
         Return "Hello World"
     End Function
End Class

EDIT: problem solved

Lesson: Need to instantiate class before using it, many thanks to Gens and all of you!


You have 2 End Class statements, remove one.


It looks like you need to put your Test method inside a Module in order for it to Compile

Module Module1
    Public Sub Test
        dim a as TestClass() 
        dim b as string
        b = a.ReturnString()
    End Sub
End Module

Public Class TestClass

    Public Function ReturnString() as string
        Return "Hello World"
    End Function
End Class

EDIT

As was pointed out by Blindy, you had double End Class statements


Try something like this

vbc <filename>.vb

with

Public Class Main

        Shared Sub Main
        Dim main as New Main
        main.Test()
    End Sub

    Public Sub Test
        dim a as New TestClass
        dim b as string
        b = a.ReturnString()
    End Sub

    Public Class TestClass

        Public Function ReturnString() as string
            Return "Hello World"
        End Function
    End Class
End Class


Your TestClass need to be instantiated before use. To instantiate a class, use new keyword before the class name

dim a as new TestClass()

dim b as string

b = a.ReturnString()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜