2

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 thought for sure, this used to work:

n = MyFunc1.Add(15, 16)
1
  • Susan has your question has been answered? If it has would you be so kind as to accept the answer you think best solved your problem. Commented Apr 14, 2011 at 11:07

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Millions of hours on vb6. Fairly new to vb.net. What would be the "right" vb.net to do something like this: I just want to make a separate file, with 50 very generic functions, that I can quickly/easily call where ever I need. I thought the "namespace" and "MyFunc.Add()" way WAS the correct way to keep things organized (separate file) and avoid conflicts (by using MyFunc.Add) syntax. No?
Then if you want to continue VB6 style you might want to stick to Modules. Or are you starting out on Object Oriented Programming?
@Susan: Yah, I work with many of the same. While the syntax is the same and some of the paradigms transfer over, so much is different between 6 and .Net (such as Namespaces) that a lot can trip you up. One thing you might try (as an exercise) is removing the Microsoft.VisualBasic namespace from your references. This will break any code that has legacy VB6 syntax in it and force you to use all .Net constructs. It will be aggravating at first but will get better with time.
I'm trying to avoid creating countless "instances" just to call simple, generic functions. That's really the "correct" way? It makes my small programs just harder and harder to write, test, debug. I don't have a single problem creating 50 generic functions, and remembering their names.... with 0 conflicts with other parts of my code. Isn't this 1 time when "functions" beat out "create individal classes and instances of classes" for everything.
@Susan: For shared methods you don't create instances. I'll post an edit as example.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.