Results 1 to 5 of 5

Thread: VB Module structure question

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    VB Module structure question

    I am new to Vb.net and through a bunch of trolling the forums and trying out code I have come to the conclusion that using modules is quite a bit different in .net.

    I am in the process of developing my first large vb.net project, learning as I go. Up to this point I have been writing all my code in the Main Form1.vb file. I decided to clean up my code a bit by pulling all the functions I wrote into a module. That seems fine except for the instances when I try to access a control on the main form. When I try to do that it errors. I spent the better part of the afternoon surfing forums and found this to be a topic of confusion for vb6 developers as it is a totally new concept.

    I am not sure if I am correct or not, but from my understading if I wish to access a control on form1 from a function in module1 I first must create an instance of the form1 object within module that houses my functions.

    Once I created an instance of the original form it no longer errors out, but no changes are reflected on the original form. This leads me to believe that I must now show the new form object I created through something along the lines of (frm1.show or frm1.showdialog) . Unfortunatly this does create a new form on the screen with the reflected changes, but the original is still there. I am sure I can figure out a way to destroy the original, I just can't believe there is not an easier way to access form controls from within a module without createing a new form every time.

    I would love it if someone would help clear the concept of controlling form elements from a module or another form, here on these boards.

    Thanks for your replies,
    Toucan

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    My advice to you is to get a VB book. You'll never learn from the net alone. I have tried and its impossible.

    Get a book and grasp Object Oriented programming concepts before you start writing any code. Vb6 was not object oriented in ANY way and should be forgotten about if you want to get started in VB.net.

    OOP is hard going to begin with, but its worth doing the groundwork for.

    "Tips and Tricks in VB.net" is a good book to start with.

    Steer clear of any of the Sams "Teach yourself XYZ in 21 days/hours" they invariably suck.

    Books are better because they give you time to digest the info put forward, instead of the internet that puts you on the spot to understand something immediately. Trust me on this.
    I don't live here any more.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    I admit I dont have the strongest grasp of OOP I am moving from ASP, which suffered from the same problem. I do in fact have two books from wrox press. (Begginning VB.net) and (Advanced VB.net) and though to do have information on OOP they dont address modules at all. I will check out the book you recommended.

    It just seems searching through 3 different boards accessing form controls from modules and other forms seems to be a big issue for a lot of people. I am guessing they are suffering from the lack of OOP understanding as I am.

    For those of you that are suffering from the same confusion as I; I have figured out a solution (maybe not the best) to my problem and that is if you are trying to access form controls from a module one way to do it is pass the form into the function/sub that resides in the module.

    Code in Form
    Code:
    Myfunction(me) 'me passes an instance of the form
    module
    Code:
    Public Function(frm as Form1)
    frm.textcontrol.text = "test"
    .....
    Hope this helps and I would appreciate it if anyone could remark on the validity of this solution.

    Thanks,
    -Toucan

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Quick add on question:

    The code I posed above works fine. I am curious on the performance issues between the following:

    Code in Form

    Code:
    Myfunction(me) 'me passes an instance of the form

    module
    Code:
    Public Function(ByVal frm As Form1)
    frm.textcontrol.text = "test"
    .....
    The above code will pass a refrence of the calling form, but it will only let you control the textcontrol from a single type of form (Form1). Using this solution is it possible to make the funciton work for any form you pass to the function assuming it has controls by the same name?

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    VB Code:
    1. 'form code
    2. x(me)
    3.  
    4. 'module code
    5. sub x(f as form)
    6. .
    7. .
    8. .
    9. end sub

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