Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

How to define a property that shows the variable has value or not in c#

I'm trying to create a property like IsRunning for my COM object.

I'm defining a variable named myServer in different COM type class named SpckCOMApp.

If variable value turns to null, my IsRunning property should returns false otherwise it should be return true

SpckCOMApp myServer = null;

Open in new window


I don't know how to apply that? Am i need to create event?

If you have any ideas/solutions it'd be great for me.

Thanks.
Avatar of Eduard Ghergu
Eduard Ghergu
Flag of Romania image

Something like:
public bool IsRunning {
get { return myServer != null; }
}
Avatar of Skale
Skale

ASKER

Yes true :) But i just realize i didn't explain it clearly.

For example i have a label that shows the current status of IsRunning.

But on runtime if it's not working label will still shows IsRunning it doesn't change dynamically.

I mean how to do that?
In this case, you'll need to implement a timer-based checker that will test the value of myServer and update that label
No need to implement a timer, simply implement the INotifyPropertyChanged Interface in your class, setup the property (or properties) that you want to publish the event and subscribe to the PropetyChanged event of you class in your form; e.g. -

Status.cs -
using System;
using System.ComponentModel;

namespace EE_Q29158901
{
	class Status : INotifyPropertyChanged
	{
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
			{
				handler(this, e);
			}
		}


		private bool _isConnected;
		public bool IsConnected
		{
			get { return _isConnected; }
			set
			{
				if (!Equals(value, _isConnected))
				{
					_isConnected = value;
					OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsConnected)));
				}
			}
		}
	}
}

Open in new window

Form1.cs -
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace EE_Q29158901
{
	public partial class Form1 : Form
	{
		Status status = new Status();

		public Form1()
		{
			InitializeComponent();
		}

		private void OnClick(object sender, EventArgs e)
		{
			if (sender is Button)
			{
				var btn = sender as Button;
				if (Equals(btn, button1))
				{
					status.IsConnected = !status.IsConnected;
				}
			}
		}

		private void OnLoad(object sender, EventArgs e)
		{
			status.PropertyChanged += OnPropertyChanged;
			ChangeConnectedStatus();
		}

		protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (sender is Status)
			{
				if (Equals(e.PropertyName, nameof(Status.IsConnected)))
				{
					ChangeConnectedStatus();
				}
			}
		}

		private void ChangeConnectedStatus()
		{
			if (!status.IsConnected)
			{
				textBox1.BackColor = Color.Red;
				textBox1.Text = "Unconnected";
			}
			else
			{
				textBox1.BackColor = Color.Green;
				textBox1.Text = "CONNECTED!!!";
			}
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29158901
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.ForeColor = System.Drawing.Color.White;
			this.textBox1.Location = new System.Drawing.Point(13, 13);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(194, 20);
			this.textBox1.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(13, 39);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(194, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = " Change Status";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.OnClick);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(219, 74);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.OnLoad);
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
	}
}

Open in new window

Produces the following output -User generated imageUser generated imageIn this situation, clicking the button changes the property which publishes the event.  In other words, anytime the IsConnected property changes, the PropertyChanged event is published.  Anything that is subscribed to the event (or listening) will take the appropriate actions as decided in the logic.

-saige-
Avatar of Skale

ASKER

How can i apply for static classes because i'm storing my global variables in that static class.
If you are going to use a static class, then you would use a property which would hold a reference to your class; e.g. -
using System;
using System.ComponentModel;

namespace EE_Q29158901
{
	static class Globals
	{
		public static Status Status { get; private set; } = new Status();
	}

	class Status : INotifyPropertyChanged
	{
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
			{
				handler(this, e);
			}
		}


		private bool _isConnected;
		public bool IsConnected
		{
			get { return _isConnected; }
			set
			{
				if (!Equals(value, _isConnected))
				{
					_isConnected = value;
					OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsConnected)));
				}
			}
		}
	}
}

Open in new window

Form1.cs -
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace EE_Q29158901
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void OnClick(object sender, EventArgs e)
		{
			if (sender is Button)
			{
				var btn = sender as Button;
				if (Equals(btn, button1))
				{
					Globals.Status.IsConnected = !Globals.Status.IsConnected;
				}
			}
		}

		private void OnLoad(object sender, EventArgs e)
		{
			Globals.Status.PropertyChanged += OnPropertyChanged;
			ChangeConnectedStatus();
		}

		protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (sender is Status)
			{
				if (Equals(e.PropertyName, nameof(Status.IsConnected)))
				{
					ChangeConnectedStatus();
				}
			}
		}

		private void ChangeConnectedStatus()
		{
			if (!Globals.Status.IsConnected)
			{
				textBox1.BackColor = Color.Red;
				textBox1.Text = "Unconnected";
			}
			else
			{
				textBox1.BackColor = Color.Green;
				textBox1.Text = "CONNECTED!!!";
			}
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29158901
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.ForeColor = System.Drawing.Color.White;
			this.textBox1.Location = new System.Drawing.Point(13, 13);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(194, 20);
			this.textBox1.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(13, 39);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(194, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = " Change Status";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.OnClick);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(219, 74);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.OnLoad);
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
	}
}

Open in new window

Which produces the same output as above.

-saige-
saige, the COM component will not notify you about its status change. How are you going to detect if it is null or not?
Avatar of Skale

ASKER

@Eduard and @it_saige thanks for your help and I'm sorry to make the subject come alive again :) Is there any way to notify COM component status change?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.