Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Acquiring a property value

Acquiring a property value

Scheduled Pinned Locked Moved C#
questioncsharp
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Brisingr Aerowing

    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

    B Offline
    B Offline
    Blubbo
    wrote on last edited by
    #11

    I don't understand your question

    1 Reply Last reply
    0
    • D Dave Kreskowiak

      You defined deviceControl as Control, which, if oyu look at the documentation for Control, doesn't have a IsConnected 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

      B Offline
      B Offline
      Blubbo
      wrote on last edited by
      #12

      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.

      D 1 Reply Last reply
      0
      • P PIEBALDconsult

        Do you need to cast it?

        B Offline
        B Offline
        Blubbo
        wrote on last edited by
        #13

        Tried to cast the control. Still nothing.

        D 1 Reply Last reply
        0
        • D Dave Kreskowiak

          You defined deviceControl as Control, which, if oyu look at the documentation for Control, doesn't have a IsConnected 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

          B Offline
          B Offline
          Blubbo
          wrote on last edited by
          #14

          I would have to agree with the 2nd option.

          1 Reply Last reply
          0
          • B Blubbo

            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.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #15

            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

            1 Reply Last reply
            0
            • B Blubbo

              Tried to cast the control. Still nothing.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #16

              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

              1 Reply Last reply
              0
              • B Blubbo

                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?

                B Offline
                B Offline
                Blubbo
                wrote on last edited by
                #17

                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);

                P L 2 Replies Last reply
                0
                • B Blubbo

                  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);

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #18

                  Quite a performance hit if you do that a lot.

                  1 Reply Last reply
                  0
                  • B Blubbo

                    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);

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #19

                    And what happens if that object does not have a property named isConnected? You have received some good advice here from people who are trying to help you, so why implement a bad answer from somewhere else?

                    Use the best guess

                    1 Reply Last reply
                    0
                    • B Blubbo

                      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 Offline
                      C Offline
                      coolcat227
                      wrote on last edited by
                      #20

                      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)
                      {
                      ...
                      }

                      1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Truthfully? Not even close. Sorry, but you've got some more work to do.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        N Offline
                        N Offline
                        N8tiv
                        wrote on last edited by
                        #21

                        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!

                        My Online Journey

                        D 1 Reply Last reply
                        0
                        • N N8tiv

                          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!

                          My Online Journey

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #22

                          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

                          N 1 Reply Last reply
                          0
                          • D 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 Kreskowiak

                            N Offline
                            N Offline
                            N8tiv
                            wrote on last edited by
                            #23

                            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?

                            My Online Journey

                            D 1 Reply Last reply
                            0
                            • N N8tiv

                              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?

                              My Online Journey

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #24

                              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

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups