Calling functions from files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I want to make a project that calls and executes a function (VB code) made
in a seperate file in the Application Folder. I know I can create the
function in my project and call it internally, but I want to put the
function's code in an external file, so that future updates to the function
will require a replacing the function file instead of re-installing the whole
project.

Can you give me ideas on how I can make small updates to my application
project without uninstalling and then re-installing the whole project?

Thanks,
Amjad
 
Make two projects. One that is a class library project. This will compile
to a dll. The other your executable. You can then reference these function
in your project and when you want to update the dll you can.

Chris
 
Thanks for the idea.

- In order for the project to compile to a DLL, do I just include the
function's code in a module only? How do I make DLL files?
- Assuming that I made the DLL file. Can you give me an example on calling a
DLL file in my project?

Amjad
 
To make the dll, open a new project. Instead of calling it a type "Windows
Application", make a Class Library instead. The easiest way to do this is
to make add a second project in the same solution.

All of the classes you use right now are dlls. System.Windows.Forms.Form
which is what inherit when you make a form for your windows application is
just a dll with the namespace of System.Windows.Forms. So when you make
your dll you can just reference by its name. For example.

In the second project which is a class library make a new class:

'Not Tested!
Public Class MathFunction

'This is shared so I don't have to make an instance of the class
Public Shared Function SquareNumber(Value as Integer) as Long
return Value * Value
End Sub

End Class

Now have your main form use it

Public Class MainForm
inherits Form

Private MainForm_Load(....) handles Me.Load
messagebox.Show(MathFunction.SquareNumber(5))
End Sub

End Class


That's the idea. Depending how you set things up you may have to reference
your new dll in your project. You also might want to give a namespace to
your class library. If they are in the same solution I don't think you'll
have any issues seeing you new class.

Chris
 
Just so you are aware (in case you don't see it)... the project type "Class
Library" is not available for all versions of VB.NET.

(There are work arounds).

Greg
 
Thanks for the reply.

That idea makes sense. I created a "Class Library" project and a "Windows
Form" project in the same solution, however I'm unable to reference the DLL
class in the Windows Form (i.e. "Name 'MathFunction' is not declared" in your
code). Can you lead me to some online examples or documentation on how to do
this?

Amjad
 
Open the References folder in your solution explorer for the Windows Form
project. On the Projects tab select you Class Library.

From code in your Windows Form try this:

Dim i as integer = ClassLibrary1.MathFunction.SomeSharedMethod()

"ClassLibrary1" is the root namespace of your Class Library project (unless
you changed it).

Greg
 
Thanks Greg.
I added my ClassLibrary1 to the References of my Windows Form project, and
now I can see the class and its methods within my windows form code.

That's what I was looking for.

Amjad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top