|
-
Jul 9th, 2009, 09:36 AM
#1
Thread Starter
Addicted Member
[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.
-
Jul 9th, 2009, 09:40 AM
#2
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
-
Jul 9th, 2009, 09:42 AM
#3
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.
-
Jul 9th, 2009, 09:44 AM
#4
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 -
-
Jul 9th, 2009, 09:47 AM
#5
Re: Passing or calling a Variable to another sub
 Originally Posted by mickey_pt
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.
-
Jul 9th, 2009, 10:07 AM
#6
Thread Starter
Addicted Member
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)
-
Jul 9th, 2009, 11:34 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|