[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.
Re: Passing or calling a Variable to another sub
Just declare it as friend and you can access it anywhere...
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.
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]
Re: Passing or calling a Variable to another sub
Quote:
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.
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)
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.