Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging object properties from within a try statement (C#)
    text
    copied!<p>I am attempting to make an image control be displayed after a port check is run.</p> <pre><code>namespace MonitorFlux { public partial class Form1 : Form { PortChecks PortCheckObject = new PortChecks(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } public static void CheckHTTP() { string hostname = "google.com"; int portno = 80; IPAddress ipa = (IPAddress)Dns.GetHostAddresses(hostname)[0]; Form1 formobject = new Form1(); // Create new class object, so can call other methods in the class try { System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); sock.Connect(ipa, portno); if (sock.Connected == true) // Port is in use and connection is successful { MessageBox.Show("Port is Open"); formobject.displayGreen(); } sock.Close(); } catch (System.Net.Sockets.SocketException ex) { if (ex.ErrorCode == 10061) // Port is unused and could not establish connection { formobject.displayRed(); MessageBox.Show("Port is Closed"); } else { MessageBox.Show(ex.Message); } } } public void displayGreen() { pictureBox2.Visible = false; pictureBox1.Visible = true; } private void displayRed() { pictureBox2.Visible = true; pictureBox1.Visible = false; } private void testCheck_Click(object sender, EventArgs e) { CheckHTTP(); // MessageBox.Show(PortCheckObject.httpport); } } } </code></pre> <p>It won't let me call a method from within the try clause e.g. displayRed()</p> <p>So I create an object of Form1 class to run the displayRed() method, although when the method runs it doesn't hide the image controls as expected. I assume this is because I have create another instance of the class. (If I run the method normally - not using an object, the image controls are hidden correctly.)</p> <p>So I guess my questions is how can I get around this issue?</p> <p>Please let me know if I have not explained the situation very well and I'll try my best to elaborate. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload