Acquiring a property value
-
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)
{
...
} -
Truthfully? Not even close. Sorry, but you've got some more work to do.
A guide to posting questions on CodeProject[^]
Dave KreskowiakLike I said, I'm only about halfway through my book… I went back over the book, I didn't find anything in there talking about using properties on different data types… There were a few examples, only on string and int… Maybe, just maybe… It talks about using properties on different data types further in the book. Also, I just got done scouring MSDN and couldn't find anything that talks about using properties on different data types… I also click through the related topics and subjects, still couldn't find anything… Could you point me to a place online or if you have time… Explain it to me here. It would definitely be appreciated for your help. I would like to learn C sharp the right way, I want to be able to write the code myself… I don't want to be a script kiddie!
-
Like I said, I'm only about halfway through my book… I went back over the book, I didn't find anything in there talking about using properties on different data types… There were a few examples, only on string and int… Maybe, just maybe… It talks about using properties on different data types further in the book. Also, I just got done scouring MSDN and couldn't find anything that talks about using properties on different data types… I also click through the related topics and subjects, still couldn't find anything… Could you point me to a place online or if you have time… Explain it to me here. It would definitely be appreciated for your help. I would like to learn C sharp the right way, I want to be able to write the code myself… I don't want to be a script kiddie!
Properties are just little bits of code that gets/sets a value or reference. You access them the exact same way no matter what the class exposing them is. Why use properties at all?? Property "setters" can be written to validate the value being passed in before it's used by the object. They can also be used to kick off other pieces of code, usually depending on the value being passed in. Every class exposes it's own set of properties and methods. What each one does is explained in the documentation for that class. For example, the String class, docs are here[^], only exposes two properties itself, Chars and Length. I don't really know what your having a problem with.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Properties are just little bits of code that gets/sets a value or reference. You access them the exact same way no matter what the class exposing them is. Why use properties at all?? Property "setters" can be written to validate the value being passed in before it's used by the object. They can also be used to kick off other pieces of code, usually depending on the value being passed in. Every class exposes it's own set of properties and methods. What each one does is explained in the documentation for that class. For example, the String class, docs are here[^], only exposes two properties itself, Chars and Length. I don't really know what your having a problem with.
A guide to posting questions on CodeProject[^]
Dave KreskowiakI get all of that, "Black Box" type of writing code… Hiding implementation. The only examples I've ever seen or talked about were on strings and ints… It never mentions other types like byte, long, boolean etc. etc. My question is I guess: can you use properties on all of those types?
-
I get all of that, "Black Box" type of writing code… Hiding implementation. The only examples I've ever seen or talked about were on strings and ints… It never mentions other types like byte, long, boolean etc. etc. My question is I guess: can you use properties on all of those types?
Can you use the properties exposed by those types (don't use "on"), yes of course! Using a property exposed by any type at all is exactly the same no matter what type it is.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak