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 Offline
    B Offline
    Blubbo
    wrote on last edited by
    #1

    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?

    N B D P OriginalGriffO 7 Replies 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?

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

      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

      My Online Journey

      L B 2 Replies 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
        Brisingr Aerowing
        wrote on last edited by
        #3

        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 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?

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

          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

          N B 3 Replies 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

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

            So, where does my answer fit in? I'm halfway through a book I picked up at 'brainmeasures dot com'...

            My Online Journey

            D 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?

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

              Do you need to cast it?

              B 1 Reply Last reply
              0
              • N N8tiv

                So, where does my answer fit in? I'm halfway through a book I picked up at 'brainmeasures dot com'...

                My Online Journey

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

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

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

                N 1 Reply Last reply
                0
                • N N8tiv

                  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

                  My Online Journey

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

                  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: true

                  Completely wrong, I'm afraid. OP has coded the property correctly, but has not instantiated the object properly.

                  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?

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • N N8tiv

                      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

                      My Online Journey

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

                      uhh that's not it. Keep on learning! There's so much to learn!

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