Results 1 to 7 of 7

Thread: [RESOLVED] Passing or calling a Variable to another sub

  1. #1

    Thread Starter
    Addicted Member raptormanad's Avatar
    Join Date
    Oct 2008
    Posts
    224

    Resolved [RESOLVED] Passing or calling a Variable to another sub

    Is there a way to call or pass a variable to another sub? I'm trying to reterive a variable (or even a field name) in somethign like this:

    Code:
    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
    
            args.Graphics.DrawString(RS("field1"), New Font("arial", 8), Brushes.Black, 5, 5)
    
        End Sub
    or

    Code:
    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
    
            args.Graphics.DrawString(variable1, New Font("arial", 8), Brushes.Black, 5, 5)
    
        End Sub
    Where variable1 is from another sub. I keep getting a null exception.

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Passing or calling a Variable to another sub

    Just declare it as friend and you can access it anywhere...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing or calling a Variable to another sub

    A method has access to its own local variables and to its type's member variables. It does not have access to local variables from other methods because, by definition, a local variable exists only within the method it's declared in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Passing or calling a Variable to another sub

    Declare variable1 with a scope that is accessible to both methods. In this case, you declare variable1 with class scope, that is move it just outside of the sub. For example, instead of having this
    Code:
    private Sub Foo()
       Dim var1 as Integer = 100
      '......
    end sub
    private sub Foo2()
       'You cannot access var1 here because it's not in the same scope
    [end sub]
    You change to this
    Code:
    Private var1 As Integer
    private Sub Foo()
       var1 = 100
      '......
    end sub
    private sub Foo2()
       'You now can access var1 because it's in the same scope
       Dim var2 as integer = var1 * 100
    [end sub]
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing or calling a Variable to another sub

    Quote Originally Posted by mickey_pt View Post
    Just declare it as friend and you can access it anywhere...
    That doesn't really follow. Declaring something Friend means that it is accessible anywhere within the same project but not outside. If you're talking about accessing another member within the same type then Friend is no more accessible than Private. Apart from that, local variables have no access modifiers so they can't be declared Friend anyway. Local variables are accessible in the block they're declared in and not outside, so access modifiers would be meaningless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member raptormanad's Avatar
    Join Date
    Oct 2008
    Posts
    224

    Re: Passing or calling a Variable to another sub

    Now we get:

    Conversion from type 'InternalField' to type 'String' is not valid.

    When we go to this code:

    Code:
    Dim fname2 = fname
            args.Graphics.DrawString(fname2, New Font("arial", 8), Brushes.Black, 5, 5)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Passing or calling a Variable to another sub

    This question has now been dealt with in the other thread. This one should be marked Resolved, and ended.
    My usual boring signature: Nothing

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