|
-
Oct 14th, 2004, 02:02 PM
#1
Thread Starter
New Member
Saving a Function for future programs.
I am in a C++ class, and we were making function prototypes. I have created some function prototypes in Visual Basic.Net and I wondered how to save them for use on other progs.
In C++ you save the file as a header file, for instance function.h
and include it as an include file #include "function.h"
Is there a similar way to do this in visual basic?
I have no signature

-
Oct 14th, 2004, 02:42 PM
#2
not exactly... you can put them in classes and then add those classes to any programs either by just adding the .vb file to the future project (kind of like having a header file in a way) or you can make a dll from them (a class library) and reference it in future apps
-
Oct 14th, 2004, 02:49 PM
#3
Thread Starter
New Member
Thanks
Dang. I'm not that great with classes and such. I have no background on them, and I'm not in any courses for Visual Studio yet. If you know of any tutorials or source code that is failry simplistic, I'd appreciate it if you threw one my way
If you know of any off hand. Don't go out of your way or anything.
I have no signature

-
Oct 14th, 2004, 02:59 PM
#4
Actually your probably do know classes and dont realize it. You have created and used forms right? The code of a form is just a class.
-
Oct 14th, 2004, 03:37 PM
#5
Thread Starter
New Member
Hmm. Interesting, I never quite looked at it like that. I looked in the solutions explorer at class view and it boggled my mind. Thanks for pointing that out. I'm still not sure of how to include the funtions in a class library. Do I have to create one and copy the code into it? Or can I save the current form as a class library or .dll? Or is this question totally impossible and makes no sense?
I have no signature

-
Oct 14th, 2004, 03:59 PM
#6
Just right clcik the project name in your solutions explorer. Chose Add New Item. Choose class. Put your functions in there.
-
Oct 14th, 2004, 11:24 PM
#7
You can also compile it as a DLL (Class Library) and the next time you create a project, you add a reference to that DLL.
-
Oct 15th, 2004, 12:52 PM
#8
Thread Starter
New Member
VB Code:
Public Class Class1
Public Function addfour(ByVal pNumber As Integer) As Integer
Return pNumber + 4
End Function
End Class
It is saying that my function is undeclared in my private Sub button1.click event procedure.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number As Integer
number = Val(Label1.Text)
addfour(number) 'addfour() is undeclared
Label1.Text = number
End Sub
If I add the same function into a module, the program works. This doesn't achieve the disired outcome of making the function useable in other programs by use of reference. At least not to my knowledge it doesn't.
VB Code:
Module Module1
Public Function addfour(ByVal pNumber As Integer) As Integer
Return pNumber + 4
End Function
End Module
Last edited by dremelpoo; Oct 15th, 2004 at 01:42 PM.
I have no signature

-
Oct 15th, 2004, 01:45 PM
#9
Frenzied Member
You have to make sure you've added the class to your project, and in my projects (.Net 2002) I have to add an Imports ProjectName.Class1 statement to the form I'm using it in. Otherwise, you'd have to declare an object of Class1 in order to access its methods - Dim clsYada as New Class1. I don't like doing that for classes that are just methods, so do the Imports.
That said, a .dll would do the trick as mentioned above.
Tengo mas preguntas que contestas
-
Oct 15th, 2004, 02:05 PM
#10
Originally posted by salvelinus
You have to make sure you've added the class to your project, and in my projects (.Net 2002) I have to add an Imports ProjectName.Class1 statement to the form I'm using it in. Otherwise, you'd have to declare an object of Class1 in order to access its methods - Dim clsYada as New Class1. I don't like doing that for classes that are just methods, so do the Imports.
That said, a .dll would do the trick as mentioned above.
Thats incorrect. Imports has nothing to do with wether you need to instantiate the object or not.
You have to isntantiate your class then you can call the method
Dim myClass as New Class1
myClass.addfour
-
Oct 15th, 2004, 09:38 PM
#11
Thread Starter
New Member
That did infact work fix the declaration problem.
I thank you for the help.

My function however doesn't work
For every problem I fix, a new one seems to show its face. Debugging is a quality that I need to improve...
Last edited by dremelpoo; Oct 15th, 2004 at 09:42 PM.
I have no signature

-
Oct 17th, 2004, 03:11 AM
#12
Frenzied Member
Originally posted by Cander
Thats incorrect. Imports has nothing to do with wether you need to instantiate the object or not.
You have to isntantiate your class then you can call the method
Dim myClass as New Class1
myClass.addfour
Maybe I was unclear. If I have a class that is just methods, say a class that does stuff like filling listboxes, comboboxes, connecting to dbs, etc, and I don't want to instantiate an instance of the class, using Imports allows me to use those methods.
I know you'd normally create an instance of the class to use it, but for just methods, I prefer not to do so.
Tengo mas preguntas que contestas
-
Oct 17th, 2004, 04:23 AM
#13
Originally posted by salvelinus
Maybe I was unclear. If I have a class that is just methods, say a class that does stuff like filling listboxes, comboboxes, connecting to dbs, etc, and I don't want to instantiate an instance of the class, using Imports allows me to use those methods.
I know you'd normally create an instance of the class to use it, but for just methods, I prefer not to do so.
That is still incorrect. You'd have to make the methods shared to not instantiate them, but then you still wouldn't need Imports.
Imports is simply a shortcut for namespaces, you import a namespace. It just saves you typing thats all sort of like With did in VB6 or still in .NET.
VB Code:
'the following two statements are the same
'with imports
Imports MyCustomDLL
Dim myclass1 As New myclass()
'without imports
Dim myclass1 As New MyCustomDLL.myclass()
-
Oct 17th, 2004, 09:13 AM
#14
Frenzied Member
Well, maybe I've got a weird installation of .Net.
I'm not instantiating any instance of the class. I'm calling the methods in it.
VB Code:
'Not doing this - I know I could
Dim dh as New DataHelper 'DataHelper = name of class
dh.FillListbox(x, x, x) 'FillListbox is a method in the class DataHelper
'Instead...
Imports ProjectName.DataHelper
FillListbox(x, x, x)
This works just fine. Maybe it's not the best way. Maybe it's not the preferred way. OK. I know it's a departure from strict OOP. I just don't like creating objects that don't have properties, only methods. If there are good reasons I shouldn't do this, please let me know. But it works. And yes, I know, making the class a .dll and adding a reference could accomplish the same thing, but I have to make things simpler for the in house IT environment I'm in. No one else there has had any exposure to anything beyond VBA and old C.
Tengo mas preguntas que contestas
-
Oct 17th, 2004, 12:52 PM
#15
Show the code declaration of the method. I has to be declared as Shared, it's not Imports that is doing it.
-
Oct 18th, 2004, 09:00 AM
#16
Frenzied Member
Yes, the method is shared, but it doesn't work (for me, at least) w/o using the Imports.
Tengo mas preguntas que contestas
-
Oct 18th, 2004, 10:45 PM
#17
If you don't import the namespace then you have to list the full namespace to reference it.
VB Code:
'instead of
Imports ProjectName.DataHelper
FillListbox(x, x, x)
'use this
ProjectName.DataHelper.FillListbox(x, x, x)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|