1

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 :)

1
  • Can you put some code up for what you are doing. You maybe want to read up on namespaces and statics for some of this. Commented Mar 11, 2011 at 12:00

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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

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.