The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 scope of variables and IF statements
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

984527

USA
13 Posts

Posted - 09/14/2012 :  6:11:07 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I need to declare a variable inside an IF statement and then use it outside the statement. It's a range variable, so I can't declare the variable first (where it would have session scope) and set its value inside the IF statement. It works if I set global scope, but that's like killing a fly with an anvil. Is there an alternative?

Here's what I'm doing now:
// Declare InputBook someplace here

loop(sIndex, 1, 6) {		
	@global = 1;
	if (sIndex == 6) {
		range InputSheet = [%(InputBook$)](1:5)!;
		string OutputSheetName$ = "All";
	};
	else {
		range InputSheet = [%(InputBook$)]$(sIndex)!;
		string OutputSheetName$ = InputSheet.name$;
	};	
	@global = 0;

	// Use InputSheet and OutputSheetName
};


I'm using OriginPro 8.5 SR1 on Windows 7.

Penn

China
644 Posts

Posted - 09/17/2012 :  10:38:30 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

You can use a string variable to hold the range string first, and then define the range outside the IF statement.

loop(sIndex, 1, 6) {		
	//@global = 1;
	string strRange;  // string for range definition
	if (sIndex == 6) {
		//range InputSheet = [%(InputBook$)](1:5)!;
		strRange$ = [%(InputBook$)](1:5)!;
		string OutputSheetName$ = "All";
	};
	else {
		//range InputSheet = [%(InputBook$)]$(sIndex)!;
		strRange$ = [%(InputBook$)]$(sIndex)!;
		//string OutputSheetName$ = InputSheet.name$;
	};	
	//@global = 0;

	// Use InputSheet and OutputSheetName
	// strRange$ = ;
	range InputSheet = %(strRange$);
	if(sIndex != 6)
		string OutputSheetName$ = InputSheet.name$;
	// InputSheet.=;
};


Penn
Go to Top of Page

984527

USA
13 Posts

Posted - 09/21/2012 :  6:14:22 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you. As it turns out, it also works in a switch. Why do variables declared inside a switch have local scope? Is that intentional?

// Declare InputBook someplace here

loop(sIndex, 1, 6) {		
	switch($(sIndex)) {
	case 1 to 5:
		range InputSheet = [%(InputBook$)]$(sIndex)!;
		string OutputSheetName$ = InputSheet.name$;
		break;
	case 6:
		range InputSheet = [%(InputBook$)](1:5)!;
		string OutputSheetName$ = "All";
		break;
	};

	// Use InputSheet and OutputSheetName
};
Go to Top of Page

Penn

China
644 Posts

Posted - 09/23/2012 :  9:55:47 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

There is no scope in the switch statement, which is different from the if statement. That is why the range is still available outside the switch statement. Actually, if adding the braces for each case, the range is not able to be accessed outside the switch statement, such as

loop(sIndex, 1, 6) {		
	switch($(sIndex)) {
	case 1 to 5:
	{
		range InputSheet = [%(InputBook$)]$(sIndex)!;
		string OutputSheetName$ = InputSheet.name$;
		break;
	}
	case 6:
	{
		range InputSheet = [%(InputBook$)](1:5)!;
		string OutputSheetName$ = "All";
		break;
	}
	};

	// Use InputSheet and OutputSheetName
};


Penn
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000