开发者

VB.NET namespace and class syntax

What am I missing in the code below? Maybe a scope problem? A syntax problem?

I've added a new file, class1.vb, to my VB.NET project containing:

Namespace MyFunc1
    Public Class MyFunc2

        Public Function Add(ByVal n1 As Int16, ByVal n2 As Int16) As Int16
            return n1 + n2  ' Edited from: "Add = n1 + n2" (same thing)
        End Function

    End Class
End Namespace

Back in my form1.vb this is near the top:

Imports MyCode.MyFunc1  ' A handful of generic functions

In form1.vb I thought I used to be able to call my functions with:

n = MyFunc1.Add(15, 16)

The error says "it's not a member". These also don't work as expected:

n = MyFunc2.Add(15, 16)
n = MyFunc1.MyFunc2.Add(15, 16)
n = Add(15, 16)

I th开发者_StackOverflowought for sure, this used to work:

n = MyFunc1.Add(15, 16)


In order to do it in the manner you want, you either need to make an instance of the classes as @Chipmunk suggests or make the method Shared. You also should get away from the old VB6 method of doing this. Your method should look like:

Public Shared Function Add(ByVal n1 As Int16, ByVal n2 As Int16) As Int16
    Return n1 + n2
End Function

Edit:
This would then be called using:

Dim x as Int16 = MyFunc1.MyFunc2.Add(15, 16)

Using Call assumes you are executing a sub and not a function. The purpose of a function is to return data. Simply Calling it won't result in the desired effect.

Edit 2 (example)

You can use a module for this as @Chipmunk states, or you can use a class. My preference is class only because MS hasn't made their minds up about modules (they did away with them for one of the versions - I forget which - and then brought them back).

Class method

Namespace MyFunc1
    Public Class MyFunc2

        Public Shared Function Add(ByVal n1 As Int16, ByVal n2 As Int16) As Int16
            Return n1 + n2    
        End Function

    End Class
End Namespace

Usage in Form1.vb

Imports MyFunc1

...

Public Sub DoAdd()
    Dim x as Int16 = MyFunc2.Add(15, 16)  ' MyFunc1 Namespace imported, MyFunc2
                                          ' is assumed. No instance is created
End Sub

Module Method

Public Module MyFunctions

    ' Notice no shared modifier here. The module uses the legacy module
    ' concept to assume that it is shared
    Public Function Add(ByVal n1 as Int16, ByVal n2 as Int16) As Int16
        Return n1 + n2
    End Function

End Module

Usage in Form1.vb

Since the module would be in a referenced namespace in your project, you would just call it directly:

Public Sub DoAdd()
    Dim x as Int16 = MyFunctions.Add(15, 16)  ' This assumes that your MyFunctions
                                              ' module is in an imported namespace
End Sub


You have to create an instance first or if you don't want to use objects, you might want to create a Module, not a Class.

You may be missing a return, and it should probably be the following.

Namespace MyFunc1
    Public Class MyFunc2

        Public Function Add(ByVal n1 As Int16, ByVal n2 As Int16) As Int16
            return n1 + n2
        End Function

    End Class
End Namespace

If you want to use a Module:

Namespace MyFunc1
    Module MyFunc2

        Function Add(ByVal n1 As Int16, ByVal n2 As Int16) As Int16
            return n1 + n2
        End Function

    End Module
End Namespace

If you want to use a class:

You have to first create an instance of MyFunc2

Dim myFuncObj as MyFunc2 = new MyFunc2

And then call the method on the object with the parameters:

dim result as Int16 = myFuncObj.Add(15,16)

I am guessing you are coming from a Visual Basic 6.0 background.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜