Hi gooseman-ga,
The problem you are encountering is one of variable scope -
configSheet is a WritableSheet object which currently has a scope the
size of the storeConfigData() method. In other words, you are
currently only able to access this variable within this method.
There are two possible ways around this:
1. Change the signature of your storeConfigData() method so that it
returns a WriteableSheet instead of nothing (represented by "void").
Remember that Java has automatic garbage collection so you don't need
to worry about this object being returned in your main() method unless
you want to use it - in that case you would save it to a variable in
main() and proceed.
// below is the implementation of solution #1
public class dataOutput
{
public static boolean spreadsheetCreated = false;
private static String fileName;
public static WritableSheet storeConfigData()
{
fileName = "outputRun"+simulationControl.scenarioCounter+".xls";
// If file not opened yet, open
if (spreadsheetCreated == false)
{
try
{
// Create the spread sheet
WritableWorkbook workbook = Workbook.createWorkbook(new
File(fileName));
spreadsheetCreated = true;
// Name the first sheet in the file for the configuration data
WritableSheet configSheet = workbook.createSheet("Sim"
+simulationControl.scenarioCounter+" Config", 0);
/* the rest of your method code here */
return configSheet;
====SNIP====
2. Create a global WritableSheet object and use this to store the
configSheet variable.
// below is the implementation of solution #2
public class dataOutput
{
public static boolean spreadsheetCreated = false;
private static String fileName;
public static WritableSheet globalSheet;
public static void storeConfigData()
{
fileName = "outputRun"+simulationControl.scenarioCounter+".xls";
// If file not opened yet, open
if (spreadsheetCreated == false)
{
try
{
// Create the spread sheet
WritableWorkbook workbook = Workbook.createWorkbook(new
File(fileName));
spreadsheetCreated = true;
// Name the first sheet in the file for the configuration data
WritableSheet configSheet = workbook.createSheet("Sim"
+simulationControl.scenarioCounter+" Config", 0);
globalSheet = configSheet; // storing configSheet in the
global var
====SNIP====
So which of these two approaches is better? Personally I would go with
the first one for the following reasons:
1. You don't know how many times this method is going to be called, so
if it is called multiple times the global variable will only retain
the last WritableSheet. By returning the variable to the calling
procedure each time, you can let the author of that procedure decide
whether or not any given variable is worth storing.
2. Global variables, while legal, are considered bad style. When
coding any class, you have to assume that others will be using it for
a variety of purposes that you are not able to predetermine.
3. The creators of Java have made it easy with garbage collection -
you don't even need to account for returned variables that are not
being stored in the calling procedure.
CONCLUSION: Return a WriteableSheet by using the modified code used in
the first solution.
Hope that clears things up...let me know if you have problems
understanding the above information by posting a clarification.
Cheers!
answerguru-ga |