Variable isn't decalred error --but it is!

J

Jerry West

I have a 'variable not declared' error occurring when in fact the var is
declared --its just nested in a If Then statement. Like so:

If bPortrait Then

'then its in portrait mode

' Make a bitmap for the result.
Dim bm_dest As New Bitmap(CInt(bm_source.Height *
scale_factor), CInt(bm_source.Width * scale_factor))

Else

'then it's in landscape mode

' Make a bitmap for the result.
Dim bm_dest As New Bitmap(CInt(bm_source.Width *
scale_factor), CInt(bm_source.Height * scale_factor))

End If

Why do I get this error when either way the If Then clause falls the var is
declared and will execute? I understand that I can set the If Then clause
for the height & width values and then declare the var outside of that. But
why doesn't the above --which is functionally equivalent-- work?

JW
 
J

Jerry West

You mean when do I encounter the error? If so, the code window underlines
every instance of bm_dest; flagging it as an error. As well, if I try to run
the code I am not allowed to. Is that what you mean?

JW
 
G

Guest

JW,

What I meant was are you trying to use the variable outside the If - Then -
Else statement?

If so, then that is your problem. The scope of the variable is only within
the If - Then - Else statement. You need to dimension bm_dest at the top of
the procedure:

Dim bm_dest As BitMap

and then set it to a new instance inside the If statement.

Kerry Moorman
 
J

Jerry West

Ah, got it. I didn't realize it's scope was relegated to only the If Then
statement. Thanks!

JW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top