I know this is an older thread. I also know I'm asking a question instead of leaving an answer, but I don't know any other way to get advice for a similar question without asking a duplicate question.
I have a dynamic solution created with VBScript that's similar to Darren's first solution. My users typically have two or more IBM Personal Communications (PCOMM) sessions open on their Desktop to do their daily tasks. Some of those sessions are logged into business proprietary applications and need to be left alone. They also can't use their "A" session as that's their production session. To handle this, I've developed this logic:
Set Connect_Manager = CreateObject("pcomm.autECLConnMgr")
Set autECLConnList = CreateObject("PCOMM.autECLConnList")
Set ObjEmulator = CreateObject("pcomm.auteclsession")
autECLConnList.Refresh
Num = autECLConnList.Count
PrevNum = Num
If Num = 0 Then
MsgBox "You must have at least two PCOMM sessions open to use this tool." & vbnewline & vbnewline & "Program is now terminating.", vbOKOnly + vbExclamation + vbSystemModal, "No Session Error"
SetEverythingToNothing
WScript.Echo "EXIT"
WScript.Quit
End If
strSession = autECLConnList(Num).Name
Connect_Manager.autECLConnList.Refresh
ObjEmulator.SetConnectionByName (strSession)
Window_Title = ObjEmulator.autECLWinMetrics.WindowTitle
If InStr(Window_Title, "HOSTORS") <> 0 Or strSession = "A" Or InStr(Window_Title, "NDB") <> 0 Then
Set ObjEmulator = Nothing
Loop_Count = 1
Do
ResEmulator = "No"
PrevNum = PrevNum - 1
If PrevNum = 0 Then
MsgBox "You need to have more than one session open." & vbnewline & "Program is now terminating.", vbOKOnly + vbExclamation + vbSystemModal, "One Session Error"
SetEverythingToNothing
WScript.Echo "EXIT"
WScript.Quit
Else
strSession = autECLConnList(PrevNum).Name
If strSession = "A" Then
MsgBox "You cannot use your production (A) session." & vbnewline & "Please open another session." & vbnewline & "Program is now terminating.", vbOKOnly + vbExclamation + vbSystemModal, "Production Session Error"
SetEverythingToNothing
WScript.Echo "EXIT"
WScript.Quit
Exit Do
Else
Set ObjEmulator = CreateObject("pcomm.auteclsession")
Connect_Manager.autECLConnList.Refresh
ObjEmulator.SetConnectionByName (strSession)
Window_Title = ObjEmulator.autECLWinMetrics.WindowTitle
If InStr(Window_Title, "NDB") <> 0 Or InStr(Window_Title, "HOSTORS") <> 0 Then
Set ObjEmulator = Nothing
Else
ResEmulator = "Yes"
End If
End If
End If
Loop_Count = Loop_Count + 1
If Loop_Count = 10 Then
MsgBox "Unable to connect to a session." & vbnewline & "Please make sure you have working sessions." & vbnewline & "Program is now terminating.", vbOKOnly + vbExclamation + vbSystemModal, "Production Session Error"
SetEverythingToNothing
WScript.Echo "EXIT"
WScript.Quit
Exit Do
End If
Loop Until ResEmulator = "Yes"
End If
My department is moving away from VBScript and towards C#.NET. I've created a VERY similar version of the VBScript code.
AutConnList autECLConnList = new AutConnList();
AutConnMgr Connect_Manager = new AutConnMgr();
AutSess ObjEmulator = new AutSess();
autECLConnList.Refresh();
int Num = autECLConnList.Count;
int PrevNum = Num;
if (Num == 0)
{
MessageBox.Show("You must have at least two PCOMM sessions open to use this tool." + Environment.NewLine + Environment.NewLine + "Program is now terminating.", "No Session Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
return;
}
string strSession = autECLConnList[Num].Name;
autECLConnList.Refresh();
ObjEmulator.SetConnectionByName(strSession);
string Window_Title = ObjEmulator.autECLWinMetrics.WindowTitle;
if (Window_Title.IndexOf("HOSTORS") == 0 || strSession == "A" || Window_Title.IndexOf("NDB") == 0)
{
ObjEmulator = null;
int Loop_Count = 1;
string ResEmulator = "No";
do
{
PrevNum -= 1;
if (PrevNum == 0)
{
MessageBox.Show("You need to have more than one session open." + Environment.NewLine + "Program is now terminating.", "One Session Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
return;
}
else
{
strSession = autECLConnList[PrevNum].Name;
if (strSession == "A")
{
MessageBox.Show("You cannot use your production (A) session." + Environment.NewLine + "Please open another session." + Environment.NewLine + "Program is now terminating.", "Production Session Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
return;
}
else
{
AutSess ObjEmulator = new AutSess();
autECLConnList.Refresh();
ObjEmulator.SetConnectionByName(strSession);
Window_Title = ObjEmulator.autECLWinMetrics.WindowTitle;
if (Window_Title.IndexOf("HOSTORS") > 0 || Window_Title.IndexOf("NDB") > 0)
{
ObjEmulator = null;
}
else
{
ResEmulator = "Yes";
}
}
}
Loop_Count++;
if (Loop_Count == 10)
{
MessageBox.Show("Unable to connect to a session." + Environment.NewLine + "Please make sure you have working sessions." + Environment.NewLine + "Program is now terminating.", "Production Session Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
return;
}
} while (ResEmulator == "Yes");
}
My issue is I'm getting the CS0136 - A local or parameter named 'ObjEmulator' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
when I'm trying to redeclare AutSess ObjEmulator = new AutSess();
. I'm looking for a similar way to keep this dynamic like the VBScript code. Since ObjEmulator = null;
doesn't fully kill the object in C#.NET like Set ObjEmulator = Nothing
does in VBScript, does anyone have advice on how this can be done?