Results 1 to 23 of 23

Thread: I need help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278

    I need help

    I cant call a subroutine that is located in my form from an external module.......like

    Form Code:
    VB Code:
    1. Public Sub Hello(byval strString as string)
    2. Code Here
    3. End Sub

    Module Code:
    VB Code:
    1. form1.Hello("Hello")

    what is wrong..............is there another syntax? because in the form i can do

    VB Code:
    1. Module1.(some subroutine)
    ??????

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    form1 is a class not a module, you cant just access one of its subrutines unless it's declared as SHARED.
    You should do something like this:

    dim frm as form1 ' create a new form1
    frm.hello("hello world")
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    that would work but here is the problem........

    i call a subroutine in the module from the form but that module calls a sub in the form and it has to be the same instance of the form is that possible?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    would

    dim frm as form1

    would frm be the existing instance of form1? or would it create a new one?

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by vector9
    would

    dim frm as form1

    would frm be the existing instance of form1? or would it create a new one?
    it creates a new one
    You can pass the form as an argument to the function that you are calling, but I wouldnt recommend it You could also use delegates, but the easiest way is to declare your sub as shared (I guess you could just put the code in a module rather than accessing it from the form, but anyways if you wanna do it this way, this should work: )



    Public Shared Sub Hello(byval strString as string)
    Code Here
    End Sub
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    i cant refer to objects on the form in the shared sub???? this sub has to control one of the objects like change its text property

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Put a variable in the module that will hold a reference to the form you want to call a sub on. Then set that module variable either with the forms load event or from somewhere else before your module will call that forms sub.

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by hellswraith
    Put a variable in the module that will hold a reference to the form you want to call a sub on. Then set that module variable either with the forms load event or from somewhere else before your module will call that forms sub.
    or use a delegate
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    ok i am more of a novice programmer and have no clue what some of this stuff your saying is.......could you be more elaborate and include some code samples?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    when i use a shared sub i cant control any objects in the form class. what can i do cuz this sub in the form class (that im calling from an external module) must controll an object in the form class

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    up

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    anyone?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    up

  14. #14
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    I think this is what you try to do:

    look at the attachment
    Attached Files Attached Files
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    sorry but its not..........i need to call the sub in the form from the module without creating a new instance of the form.

  16. #16
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    don't think you cann. VB.Net is fully object oriented. Also a form is an object. If an object doesn't exist how can you call a method of the object that doesn't exist. I think Java supports this (calling methods from the wrapper class) but vb.net not doesn't really support this.
    Last edited by robbedaya; Feb 15th, 2003 at 04:08 AM.
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  17. #17
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You CAN do this, when I get home from work, I will create a project to show you how. It is fairly easy to do.

  18. #18
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I don't have the .Net framework here, but try something like this:

    In your module, put this code:
    VB Code:
    1. Module Module1
    2.     Dim myForm1 as Form1
    3.     Sub MySub()
    4.     if myForm1 <> Nothing
    5.            myForm1.Hello("Hi")
    6.         end if
    7.     End Sub
    8. End Module

    And in your form put this code:
    VB Code:
    1. Private Sub TheSub()
    2.         'Give the module a reference to this form.
    3.         'This will allow your module to call this forms subs and functions.
    4.         Module1.myForm1 = Me
    5.  
    6.         'Now call your sub in the module to do something.
    7.         'It will in turn call sub hello on this form.
    8.         Module1.MySub()
    9.     End Sub
    10.  
    11.     Public Sub Hello(ByVal s As String)
    12.         MessageBox.Show(s)
    13.     End Sub

  19. #19
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    what you are suggestig to do is to check first of an instance of your form exists, right? So an instance has to be loaded. How do you create that instance of your form? by seting it startup-form or by loading it in another form. If you don't load this form somewhere else myForm1 will always be nothing.
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Cecilton, MD USA
    Posts
    278
    ok the form instance loads on start up.......would it work if i used the sub Main in the module as start up and create a new instance of the form from the module???? then would i be able to do what i wanna do? or does hells idea work if the form is loaded on start up?

  21. #21
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    I'd prefer starting up from the sub main. And then creating a new instance of your form. Then it should work.
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  22. #22
    New Member
    Join Date
    May 2002
    Posts
    14

    Thumbs down

    John Brown, looser John Brown, looser John Brown, looser

  23. #23
    New Member
    Join Date
    May 2002
    Posts
    14
    John Brown

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