Using modules for storing code

Lets say I have one button on a form, and write the code for what happens when I push the button in a modul, instead of in the button. And in the button I only refer to the modul.

Because when I make a program the public class form gets pritty full of code.

Is that possible




Answer this question

Using modules for storing code

  • nikos_22

    Yeah, that worked perfectly.

    But now I have encountered a new proble, in my Form1.vb I have

    Dim cdgs As ULong

    And when I make a function thats using that variable, I get an error ofcourse.
    How can I make my module read and write to exactly that variable

    Form1.cdgs = 1 ' that didnt work



  • Fei-tian

    You are beginning to discover why putting all your procedures in a module is not such a good idea. In order to make it work you will have to either move your variables to the module, in which case if you want to access them from your forms they will have to be public. Or you make them public in the forms and then access them with Form1.variable as you have tried.

    Either way you are losing all the benefits of encapsulation and on balance you are better off keeping your code with the data in the class where it belongs.


  • Jack Spade

    That's not what dave meant. You need to declare it as a friend or public as such:

    Public Function Test()

    MessageBox.show("This Works")

    End Function

    the call out the function in your form:

    (this is simply an example)

    sub Button1_click()

    Test()

    End Sub


  • yahu_Hugh

    You won't be able to access that function from a form because you have declared it as Private. Declare it as Friend or Public to access it from your forms.
  • Douglas Penna

    Hi,

    Try putting>>

    Public cdgs As Integer

    in your module code.

    or just write>>

    cdgs=1

     

    Regards,

    S_DS

     

    P.S. You could do this with a CLASS too or a STRUCTURE but that would probably be too long winded.

     

     



  • joeydj

    I dont quite know howto do this, but in my module1.vb I made this function.
    But problems appear when I want to "Call" the function, how do I do that

    Private Function testing() As Boolean

    MsgBox(" Calling a function works! ")

    End Function



  • pmail

    Dave299 wrote:
    You won't be able to access that function from a form because you have declared it as Private. Declare it as Friend or Public to access it from your forms.

    I get an error "Lib" expected

    declare sub testing as public



  • Using modules for storing code