Acquiring a property value
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
I'm just starting to learn this stuff, I'm about halfway through this e-book course… It's just now starting to cover "properties"… Maybe there is something new in the .Net that allows properties on a Boolean? As far as I know, Boolean is only true or false. I forgot what the default value is a Boolean… I'm more guessing than giving you an answer because I'm still learning C# myself. Get rid of the: get; set And replace that with: true Then you can make a call to your Boolean "isConnected"… Hopefully someone will correct me if I'm wrong. Good luck
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
C# is case sensitive. So isConnected != IsConnected
brisingr_aerowing@Gryphon-PC $ rake in_the_dough Raking in the dough brisingr_aerowing@Gryphon-PC $ make lots_of_money Making lots_of_money
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
You defined
deviceControl
asControl
, which, if oyu look at the documentation forControl
, doesn't have aIsConnected
property. You have to use the code you listed second, under "should I use this instead". I suggest picking up a beginners book on C# and working through it. This is a very basic concept you're going to have to learn completely before building on it.A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You defined
deviceControl
asControl
, which, if oyu look at the documentation forControl
, doesn't have aIsConnected
property. You have to use the code you listed second, under "should I use this instead". I suggest picking up a beginners book on C# and working through it. This is a very basic concept you're going to have to learn completely before building on it.A guide to posting questions on CodeProject[^]
Dave KreskowiakSo, where does my answer fit in? I'm halfway through a book I picked up at 'brainmeasures dot com'...
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
Do you need to cast it?
-
So, where does my answer fit in? I'm halfway through a book I picked up at 'brainmeasures dot com'...
Truthfully? Not even close. Sorry, but you've got some more work to do.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I'm just starting to learn this stuff, I'm about halfway through this e-book course… It's just now starting to cover "properties"… Maybe there is something new in the .Net that allows properties on a Boolean? As far as I know, Boolean is only true or false. I forgot what the default value is a Boolean… I'm more guessing than giving you an answer because I'm still learning C# myself. Get rid of the: get; set And replace that with: true Then you can make a call to your Boolean "isConnected"… Hopefully someone will correct me if I'm wrong. Good luck
WidmarkRob wrote:
I'm more guessing than giving you an answer because I'm still learning C# myself.
True.
WidmarkRob wrote:
Get rid of the: get; set
And replace that with: trueCompletely wrong, I'm afraid. OP has coded the property correctly, but has not instantiated the object properly.
Use the best guess
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
Just to add to the other responses: Your DeviceMonitor is a UserControl, which means that it is derived from Control (it has to be, otherwise it can't be displayed - even Form is derived from Control via a couple of other classes). That means it inherits (and you can access) all the properties of a Control in your class. But it doesn't work the other way...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);devMon contains a reference to your DeviceMonitor instance, so you can access all the properties and methods of the DeviceMonitor class via devMon:
if (devMon.isConnected)
{
...
}But deviceControl is a Control reference: it can contain a Control instance, or an instance of any class which is derived from Control, but it can only access the properties and methods of the Control class because that is the only class type it can guarantee that deviceControl contains. It is perfectly legal and correct to say:
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);
deviceControl = new TextBox();So the compiler will not let you access a derived class property or method via the base class variable. It's a bit like cars: a Car has four wheels and an engine, but a Ford Fiesta also has Cruise Control, which a Ford Model T doesn't. If you declare a variable as a Car, then you can't access myCar.CruiseControl because the Car may be a Model T. BTW: You don't need to use the deviceControl in your code - you can use a derived class anywhere you can use the base class. So this will work as well:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
I'm just starting to learn this stuff, I'm about halfway through this e-book course… It's just now starting to cover "properties"… Maybe there is something new in the .Net that allows properties on a Boolean? As far as I know, Boolean is only true or false. I forgot what the default value is a Boolean… I'm more guessing than giving you an answer because I'm still learning C# myself. Get rid of the: get; set And replace that with: true Then you can make a call to your Boolean "isConnected"… Hopefully someone will correct me if I'm wrong. Good luck
-
C# is case sensitive. So isConnected != IsConnected
brisingr_aerowing@Gryphon-PC $ rake in_the_dough Raking in the dough brisingr_aerowing@Gryphon-PC $ make lots_of_money Making lots_of_money
-
You defined
deviceControl
asControl
, which, if oyu look at the documentation forControl
, doesn't have aIsConnected
property. You have to use the code you listed second, under "should I use this instead". I suggest picking up a beginners book on C# and working through it. This is a very basic concept you're going to have to learn completely before building on it.A guide to posting questions on CodeProject[^]
Dave KreskowiakI have been programming in C# for a long time. My engineer has programmed this way on the 1st option. I'm trying to figure out and need to revise the code to work with USB device. I've came up with the solution with loss/recovery of the USB device with WndProc process.
-
Do you need to cast it?
-
You defined
deviceControl
asControl
, which, if oyu look at the documentation forControl
, doesn't have aIsConnected
property. You have to use the code you listed second, under "should I use this instead". I suggest picking up a beginners book on C# and working through it. This is a very basic concept you're going to have to learn completely before building on it.A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I have been programming in C# for a long time. My engineer has programmed this way on the 1st option. I'm trying to figure out and need to revise the code to work with USB device. I've came up with the solution with loss/recovery of the USB device with WndProc process.
I have been programming in C# for a long time. Then why did you ask this question? If what you said is true, this should have been obvious to you.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You have to cast it to the type that contains the property your trying to use, not the one that doesn't.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
I appear to have finally got what I wanted... after googling, I ran into this site... http://stackoverflow.com/questions/987982/how-can-i-get-the-value-of-a-string-property-via-reflection[^] and kept option 1... I modified the code in Main form and it got the value I was looking for. In this example, it would go as:
bool bResult = (bool)devMon.GetType().GetProperty("isConnected").GetValue(devMon, null);
-
I appear to have finally got what I wanted... after googling, I ran into this site... http://stackoverflow.com/questions/987982/how-can-i-get-the-value-of-a-string-property-via-reflection[^] and kept option 1... I modified the code in Main form and it got the value I was looking for. In this example, it would go as:
bool bResult = (bool)devMon.GetType().GetProperty("isConnected").GetValue(devMon, null);
Quite a performance hit if you do that a lot.
-
I appear to have finally got what I wanted... after googling, I ran into this site... http://stackoverflow.com/questions/987982/how-can-i-get-the-value-of-a-string-property-via-reflection[^] and kept option 1... I modified the code in Main form and it got the value I was looking for. In this example, it would go as:
bool bResult = (bool)devMon.GetType().GetProperty("isConnected").GetValue(devMon, null);
-
When a custom control (DeviceMonitor) was generated along with the property as follow:
public bool isConnected {get; set;}
then in the main form to add the control to the form...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);Somewhere in the code, I would need to acquire the "isConnected". I tried
if (deviceControl.IsConnected)
{
...
}but the .net doesn't see the property value of "isConnected". How do I get this property value this way? Should I use this instead in the main form:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);This way, I can get it as:
if (devMon.IsConnected)
{
...
}Any response?
Heres one way:
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);...
if((deviceControl as DeviceMonitor).isConnected)
{
...
}a better way:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);...
if (devMon.isConnected)
{
...
}