Results 1 to 17 of 17

Thread: Saving a Function for future programs.

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    6

    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

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    6

    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

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    6
    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

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Just right clcik the project name in your solutions explorer. Chose Add New Item. Choose class. Put your functions in there.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    6

    Unhappy

    VB Code:
    1. Public Class Class1
    2.  
    3. Public Function addfour(ByVal pNumber As Integer) As Integer
    4.             Return pNumber + 4
    5. End Function
    6.  
    7. End Class

    It is saying that my function is undeclared in my private Sub button1.click event procedure.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim number As Integer
    3.         number = Val(Label1.Text)
    4.         addfour(number) 'addfour() is undeclared
    5.         Label1.Text = number
    6.     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:
    1. Module Module1
    2.     Public Function addfour(ByVal pNumber As Integer) As Integer
    3.         Return pNumber + 4
    4.     End Function
    5. End Module
    Last edited by dremelpoo; Oct 15th, 2004 at 01:42 PM.
    I have no signature

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    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

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    6
    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

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    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

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'the following two statements are the same
    2.  
    3. 'with imports
    4. Imports MyCustomDLL
    5. Dim myclass1 As New myclass()
    6.  
    7. 'without imports
    8. Dim myclass1 As New MyCustomDLL.myclass()

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    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:
    1. 'Not doing this - I know I could
    2. Dim dh as New DataHelper  'DataHelper = name of class
    3.  
    4. dh.FillListbox(x, x, x)  'FillListbox is a method in the class DataHelper
    5.  
    6. 'Instead...
    7. Imports ProjectName.DataHelper
    8.  
    9. 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

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Show the code declaration of the method. I has to be declared as Shared, it's not Imports that is doing it.

  16. #16
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Yes, the method is shared, but it doesn't work (for me, at least) w/o using the Imports.
    Tengo mas preguntas que contestas

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you don't import the namespace then you have to list the full namespace to reference it.
    VB Code:
    1. 'instead of
    2. Imports ProjectName.DataHelper
    3.  
    4. FillListbox(x, x, x)
    5.  
    6. 'use this
    7. 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
  •  



Click Here to Expand Forum to Full Width