Taobitz
Taobitz

Reputation: 556

Calling function from VB.Net Class

Just wondering how I do this, Im creating a class that will have my common database functions. So for example I've created a function db_con in a class called db_functions.

How do i use that db_con function in for example my homepage vb code?

Do I import the class? I've tried typing the full class and method name, no joy so far?

Basic of the Basic questions I know :)

Upvotes: 1

Views: 24373

Answers (2)

Ali Alsser Ali
Ali Alsser Ali

Reputation: 1

This is what I just learned :
lets say we have:

1-Module1
2-class1
3-class2
4-function1()

and you want to call the function to in class2:

  1. If you create the function in the module as public, then you can call it just by the name:

    function1()

    that's what you write it in class2 or any class in the project

  2. If you create the function in class1 and want to use it in class2, now we have two ways to declare the function in class1:

A- Public function1 (ByVal ---- as datatype ) as datatype
This make you have to create instance to use the function, like the follow:

dim ins as New class1
ins.function1()

that's what you write it in class2 or any class in the project,

B- Public Shared function1 (ByVal ---- as datatype ) as datatype this make you don't need to use the instanse when you call the function,like the follow:

class1.function1()

That's what you write it in class2 It works with me

Upvotes: 0

Chris Diver
Chris Diver

Reputation: 19802

You import the namespace, not the class.

It sounds like you have created a class in the same namespace.

You need to create an instance of db_functions and call your function

Dim func as New db_functions
func.db_con()

It sounds like you are trying to call db_functions.db_con(), to do this create your function as a static function with the shared keyword as below

Public Shared Function db_con() As ReturnType
  .....
  Return Value
End Function  

If all your functions in that class are called that way and you never need an instance it should be a static class, in vb.net this is a Module (or therabouts).

Upvotes: 4

Related Questions