I was playing with some SAPI code and ran across two different approaches to coding an Object, where the task was to set the speed of the speaking voice. Not having any particular skills with Objects, I just thought the two approaches were interesting and worthy of taking a closer look because it will teach me something about using Objects.
This must be the millionth time I've told myself I want to code more with Objects. The list of reasons, and code I want to explore, just keeps building up!
This is a side-by-side comparison of the two coding approaches. I find it useful in viewing the differences between the two. The actual code listings are further down the post.

This must be the millionth time I've told myself I want to code more with Objects. The list of reasons, and code I want to explore, just keeps building up!

This is a side-by-side comparison of the two coding approaches. I find it useful in viewing the differences between the two. The actual code listings are further down the post.
Code:
'Compilable Example: #Compiler PBWin 10 #Compile Exe #Dim All %Unicode=1 #Include "Win32API.inc" %IDC_Slow = 500 %IDC_Fast = 501 Global hDlg As Dword, vRes, vTxt As Variant, oSp As Dispatch Function PBMain() As Long Dialog Default Font "Tahoma", 12, 1 Dialog New Pixels, 0, "SAPI, Test Code",300,300,200,75, %WS_OverlappedWindow To hDlg Control Add Button, hDlg, %IDC_Slow,"Slow", 10,10,75,25 Control Add Button, hDlg, %IDC_Fast,"Fast", 100,10,75,25 Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long Local vRate As Long Select Case Cb.Msg Case %WM_InitDialog vTxt = "Hello from Gary Beene" osp = NewCom "SAPI.SpVoice" Case %WM_Command Select Case Cb.Ctl Case %IDC_Slow vRate = -3 Object Let osp.Rate() = vRate Object Call osp.Speak(vTxt) To vRes Case %IDC_Fast vRate = +3 Object Let osp.Rate() = vRate Object Call osp.Speak(vTxt) To VRes End Select End Select End Function
Code:
'Compilable Example: #Compile Exe #Dim All %Unicode=1 #Include "Win32API.inc" #Include "Sapi.inc" %IDC_Fast = 501 %IDC_Slow = 502 Global hDlg As Dword, psp As ISpVoice, wText As WString Function PBMain() As Long Dialog Default Font "Tahoma", 12, 1 Dialog New Pixels, 0, "SAPI Test Code",300,300,300,75, %WS_OverlappedWindow To hDlg Control Add Button, hDlg, %IDC_Slow,"Slow", 10,10,75,20 Control Add Button, hDlg, %IDC_Fast,"Fast", 100,10,75,20 Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long Select Case Cb.Msg Case %WM_InitDialog wText = "Hello from Gary Beene" pSp = NewCom "SAPI.SpVoice" Case %WM_Command Select Case Cb.Ctl Case %IDC_Slow psp.SetRate -3 pSp.Speak(ByVal StrPtr(wText), %SPF_Async, ByVal %Null) Case %IDC_Fast psp.SetRate +3 pSp.Speak(ByVal StrPtr(wText), %SPF_Async, ByVal %Null) End Select End Select End Function
Comment