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
 All Forums
 Origin Forum for Programming
 LabTalk Forum
 scope of variables and IF statements

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
984527 Posted - 09/14/2012 : 6:11:07 PM
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.
3   L A T E S T    R E P L I E S    (Newest First)
Penn Posted - 09/23/2012 : 9:55:47 PM
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
984527 Posted - 09/21/2012 : 6:14:22 PM
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
};
Penn Posted - 09/17/2012 : 10:38:30 PM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000