• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to refer to a variable in an if statement outside of the if statement?  RSS feed

 
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created an if-then condition which controls the flow of my program. In this case, if the user passes a certain date, then the program takes one flow. If the user passes another date, then the program takes a separate flow. I am using JXLS and I have created a Hashmap which takes advantage of whichever dataset is passed to it.

One small problem though. The variable "dataset" which is what I am using in my XLS spreadsheet, will either come from either the if(true) part of my if-statement, or the if(false) part of my if-statement.

But, as you can see in the colorized window, the dataset variable found in the if statement is grayed out and only the one outside of it is recognized by my Hashmap. How do I get around this? (please see attached imgur image)




http://imgur.com/wsTADfZ  ; -  You can view the IDE screen here as I am seeing it   Notice the second grayed-out "dataset."
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to move dataset to the outer scope.
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You'll need to move dataset to the outer scope.


Can you please elaborate on this? I'm a very junior developer so I'm not entirely sure what you mean... even though I have the basic idea that scope refers to whatever is within the curly braces.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:
Can you please elaborate on this? I'm a very junior developer so I'm not entirely sure what you mean... even though I have the basic idea that scope refers to whatever is within the curly braces.


Well, simply, local variables declared in a curly brace can only be used in that curly brace (after the declaration). So, if you want to have variables that is used outside of a specific curly brace, one option is to move the declaration to a curly brace (outer) that cover all the code that needs it.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!