Agent / Designer question

Have been writing an agent to update the font display adjustment in the notes.ini and came across this code in designer, which kind of nearly does the job;

Dim num As Integer

On Error GoTo OutOfRange

EnterNum:

num% = CInt(InputBox(“Enter 1, 2, or 3”))

On num% GoTo label1, label2, label3

’ The user did not enter 1, 2, or 3, but a run-time error

’ did not occur (the user entered a number in the

’ range 0-255).

MessageBox “You did not enter a correct value! Try again!”

GoTo EnterNum

label1:

MessageBox “You entered 1.”

Exit Sub

As I’m learning script, I just want to understand which part of it recognises the input as being 1, 2 ,3 or out of range? Is it the % sign or is labeln doing it?

Thanks.

Subject: Agent / Designer question

Kind of nearly does the job? Maybe, but it’s also awfully ugly, ain’t it? There are better ways to write more-or-less the same thing that are both more maintainable and less annoying to the user.

By more maintainable, I mean using explicit declarations and readable types (instead of type suffixes) and eliminating the gotos and labels. With the code as it is, you need a separate label for every possible returned number value.

By less annoying, I mean that the user should have a chance to bail out, and there’s no way for the user to change his/her mind about the font size adjustment using that code. Clicking “Cancel” beats the heck out of typing, say, “0” if the user does not want to change font sizes – the Prompt method of the NotesUIWorkspace serves better than Inputbox for most uses. The equivalent code might be:

Option Public

Option Declare

Dim session As NotesSession

Dim ws As NotesUIWorkspace

Dim adjustNumber As Integer

Dim promptReturn As Variant

Dim promptMessage As String

Dim currentSetting As String

Dim badValueReturned As Boolean

Const BAD_VALUE_MESSAGE = “The font size adjustment must be a number between 0 and 25”

Const TWO_NEW_LINES = {

} 'Two literal new lines

Const PROMPT_TITLE = “Adjust Font Size - Applies To All”

Const PROMPT_INTRO = “Please enter the amount of font enlargement you desire. Use a number between 0 and 25.”

Const PROMPT_VALUES = “0 (zero) uses the default size. 25 is VERY large.”

Const PROMPT_CURRENT = "Your current setting is: "

Sub Initialize

Set session = New NotesSession

Set ws = New NotesUIWorkspace

adjustNumber = 0

badValueReturned = False

currentSetting = session.GetEnvironmentString("Display_font_adjustment", True)

If currentSetting = "" Then

	currentSetting = "0"

End If

On Error Goto ErrorHandler

Do

	If badValueReturned Then

		promptMessage = BAD_VALUE_MESSAGE + TWO_NEW_LINES + PROMPT_INTRO + TWO_NEW_LINES + PROMPT_VALUES + TWO_NEW_LINES + PROMPT_CURRENT + currentSetting

	Else

		promptMessage = PROMPT_INTRO + TWO_NEW_LINES + PROMPT_VALUES + TWO_NEW_LINES + PROMPT_CURRENT + currentSetting

	End If

	'Reset bad value flag for next attempt

	badValueReturned = False

	promptReturn = ws.Prompt(PROMPT_OKCANCELEDIT, PROMPT_TITLE, promptMessage, "")

	If Isempty(promptReturn) Then

		'User clicked Cancel -- Bail out

		Exit Sub

	Else

		adjustNumber = Cint(promptReturn) 'This may error

		Select Case adjustNumber

		Case 0

			If Not badValueReturned Then

				Call session.SetEnvironmentVar("Display_font_adjustment", "", True) 'removes ini value if present

			End If

		Case 1 To 25

			Call session.SetEnvironmentVar("Display_font_adjustment", adjustNumber, True)

		Case Else

			badValueReturned = True

		End Select

	End If

Loop While badValueReturned

ExitGracefully:

Exit Sub

ErrorHandler:

adjustNumber = 99   'An illegal value

Resume Next

End Sub

Sure, it’s a LOT longer, but it’s also a lot easier to follow and maintain AND it’s more user-friendly IMHO.

Subject: RE: Agent / Designer question

Stan - thanks also for taking the time to respond. I have written a different version of the code, the original question was more out of curiosity to understand how that worked. Comparing yours to mine, it may be obvious that I come from a CoBOL background :slight_smile:

Sub Click(Source As Button)

Dim s As New NotesSession

Dim fsize As Variant

start:

fsize = Inputbox("Enter size increase from 1 - 5 ", "Notes Administrator")

If fsize <>"" Then

	If Not Isnumeric(fsize) Then

		Messagebox "Invalid Input, please try again!", MB_OK, "Notes Administrator"

		Goto start

	Else

		If fsize > "5" Then

			Messagebox "Invalid Input, please try again!", MB_OK, "Notes Administrator"

			Goto start

		Else

			

			Call s.SetEnvironmentVar("Display_font_adjustment", fsize,True)

			Messagebox "Font size will be changed next time you restart Notes", MB_OK, "Notes Administrator"

		End If			

	End If

Else

	Exit Sub

End If

End Sub

Subject: Agent / Designer question

Hi James,

The % defines the variable implicit as integer.

The Cint function converts the input to an integer. If the user enters text or a number not in the integer range (-32768 to 32767) a runtime error is raised.

The code skips to the label OutOfRange (because of the On Error statement).

Now, after the line num% = CInt(InputBox(“Enter 1, 2, or 3”)) you can have a number from -32768 to 32767.

If the user enters a negative number or a number >255 a runtime error is raised on the line On num% Goto… since the value of num% in the on … goto stament must be in the range 0-255.

Again, the code skips to the label OutOfRange.

Finally, the On num% GoTo function means:

if num% is 1, goto the 1st defined label after GoTo (label1)

if num% is 2, goto the 2nd defined label (label2)

if num% is 4, goto the 4th defined label.

Since the 4th label is not defined, there is no label to go to so the code will continue on the next line and you’ll get the messagebox “You did not enter a correct value! Try again!”

Hope this helps.

Regards.

Subject: RE: Agent / Designer question

thanks rene, it makes sense as much as my coding head will alow!