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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. All methods called when class instantiated?

All methods called when class instantiated?

Scheduled Pinned Locked Moved C#
csharphelpquestion
16 Posts 7 Posters 1 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.
  • N Not Active

    Maybe you could share the code with us also. Might be helpful to see any problems.


    only two letters away from being an asset

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #4

    Mark Nischalke wrote:

    Maybe you could share the code with us

    Oh dear :~ :-D

    led mike

    N 1 Reply Last reply
    0
    • S stuebbie

      Hi, I have created a C# class which, when i instantiate an object, unexpectedly runs all the protected methods I have created. This is odd as the methods are only called from within specific property gets, and when I step through the code at runtime it does not step through the methods. It is all the more frustrating as i have a counter within one of the methods, and this unexpected method call completely messes up my end results. Does anyone have any idea what could cause this? I fear that I'm doing a really stupid thing, but I showed my code to the other developer in my office (albeit a VB developer) and he couldn't see anything wrong with the code. Any help is appreciated, code can be provided if necessary. Thanks, Stuart

      M Offline
      M Offline
      Mark Churchill
      wrote on last edited by
      #5

      Does this happen in Release mode? I suspect the debugger is pulling all the protected properties to stick in the watch window ;)

      Mark Churchill Director Dunn & Churchill Free Download:
      Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

      C S 2 Replies Last reply
      0
      • L led mike

        stuebbie wrote:

        I showed my code to the other developer in my office (albeit a VB developer) and he couldn't see anything wrong with the code.

        Now that is funny! :laugh: 1) so you have a class foo and when you new an instance of foo all the protected methods of the object are executed? 2) Furthermore if you put a break point in one of those methods it will not break there? 3) But you are sure it is executing, how?

        led mike

        S Offline
        S Offline
        stuebbie
        wrote on last edited by
        #6

        Hi, Thanks for the quick reply :) I didn't expect my colleague to come up with any answers, but it was worth a try! 1) so you have a class foo and when you new an instance of foo all the protected methods of the object are executed? This is correct. Further to this (apologies for not mentioning), every time that I provide data to the instantiated object, via its exposed property gets, the protected methods are run again, and the private int counter is incremented again. 2) Furthermore if you put a break point in one of those methods it will not break there? This is correct. The only time that the break point's for the protected methods are executed is when I call them from public property gets. 3) But you are sure it is executing, how? Using Visual Studio 2005, I have set up a watch on the private int that i set up within the class for counting purposes. I set it up in the following way: private int m_foo = 0; When I instantiate the class foo, m_foo is set to 1. When I set foo.SomeProperty = "bar", m_foo is then set to 2. And so on, when I assign values to properties, the m_foo variable is incremented. I also set up some Debug.WriteLine commands within the protected methods, and I can view the output from these Debug objects in the output window. It's ever so frustrating as it is probably something very small that I have missed out. Thanks again, Stuart

        L 1 Reply Last reply
        0
        • L led mike

          Mark Nischalke wrote:

          Maybe you could share the code with us

          Oh dear :~ :-D

          led mike

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #7

          He shared it the VB in the office afterall. Might as well have qualified people look at it. ;P I was hoping it was a small snippet, not the usual CodeProject source code repository posting.


          only two letters away from being an asset

          S 2 Replies Last reply
          0
          • N Not Active

            He shared it the VB in the office afterall. Might as well have qualified people look at it. ;P I was hoping it was a small snippet, not the usual CodeProject source code repository posting.


            only two letters away from being an asset

            S Offline
            S Offline
            stuebbie
            wrote on last edited by
            #8

            I'll cut it down to a respectable size and post it shortly :)

            1 Reply Last reply
            0
            • N Not Active

              He shared it the VB in the office afterall. Might as well have qualified people look at it. ;P I was hoping it was a small snippet, not the usual CodeProject source code repository posting.


              only two letters away from being an asset

              S Offline
              S Offline
              stuebbie
              wrote on last edited by
              #9

              Hi, Code snippet is below. I've tried to include only sections that are of interest. When I instantiate this class, the addParameter method is run without invoking the public property AddParameter: public class foo { private string[,] m_strParams = new string[c_RowArrayMaxValue,c_ColArrayMaxValue]; private int m_ParamCount = 0; public Boolean AddParameter { get { return addParameter(m_ParamName, m_ParamValue, m_ParamDataType, ref m_ParamCount); } } protected Boolean addParameter(string strParamName, string strParamValue, string strParamDataType, ref int intParamCount) { try { m_strParams[Convert.ToInt32(intParamCount), 0] = strParamName; m_strParams[Convert.ToInt32(intParamCount), 1] = strParamValue; m_strParams[Convert.ToInt32(intParamCount), 2] = strParamDataType; intParamCount += 1; return true; } catch (Exception e) { Debug.WriteLine("Foo.cs - addParameter - Exception found: " + e.InnerException); return false; } } } Thanks, Stuart

              G J 2 Replies Last reply
              0
              • M Mark Churchill

                Does this happen in Release mode? I suspect the debugger is pulling all the protected properties to stick in the watch window ;)

                Mark Churchill Director Dunn & Churchill Free Download:
                Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                C Offline
                C Offline
                codemunch
                wrote on last edited by
                #10

                A former co-worker got stung with that one :) LOL...I had a good laugh. He was getting quite pissed off and swearing at these properties because they were getting called before running code that accessed them. I looked down at his watch window & had a big fat laugh. Anyway, To the poster: If you've added these properties to the watch window OR if you even mouse over them while debugging, the code inside will be executed by the debugger WITHOUT stopping at your breakpoints inside the get/set.

                S 1 Reply Last reply
                0
                • M Mark Churchill

                  Does this happen in Release mode? I suspect the debugger is pulling all the protected properties to stick in the watch window ;)

                  Mark Churchill Director Dunn & Churchill Free Download:
                  Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                  S Offline
                  S Offline
                  stuebbie
                  wrote on last edited by
                  #11

                  When I run in Release mode I get the same outcome.

                  C 1 Reply Last reply
                  0
                  • S stuebbie

                    Hi, Thanks for the quick reply :) I didn't expect my colleague to come up with any answers, but it was worth a try! 1) so you have a class foo and when you new an instance of foo all the protected methods of the object are executed? This is correct. Further to this (apologies for not mentioning), every time that I provide data to the instantiated object, via its exposed property gets, the protected methods are run again, and the private int counter is incremented again. 2) Furthermore if you put a break point in one of those methods it will not break there? This is correct. The only time that the break point's for the protected methods are executed is when I call them from public property gets. 3) But you are sure it is executing, how? Using Visual Studio 2005, I have set up a watch on the private int that i set up within the class for counting purposes. I set it up in the following way: private int m_foo = 0; When I instantiate the class foo, m_foo is set to 1. When I set foo.SomeProperty = "bar", m_foo is then set to 2. And so on, when I assign values to properties, the m_foo variable is incremented. I also set up some Debug.WriteLine commands within the protected methods, and I can view the output from these Debug objects in the output window. It's ever so frustrating as it is probably something very small that I have missed out. Thanks again, Stuart

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #12

                    See the replies below about having things in the watch window. Get log4net and use that to monitor runtime behavior and see what happens. Also as Guffa points out there seems to be a design problem in regards to how you are implementing properties which might also somehow be effecting your project. In relation to that you might want to read this[^].


                    Last modified: 26mins after originally posted --

                    led mike

                    1 Reply Last reply
                    0
                    • S stuebbie

                      When I run in Release mode I get the same outcome.

                      C Offline
                      C Offline
                      codemunch
                      wrote on last edited by
                      #13

                      Are you running in release mode through the IDE? i.e. debugging the release mode? Is the "AddParameter" in your watch window? If so, remove it. In fact, remove everything from your watch window and DON'T use the mouseover in the debugger to see the public property value either. It will cause the code in the property to run and breakpoints will be ignored.

                      1 Reply Last reply
                      0
                      • C codemunch

                        A former co-worker got stung with that one :) LOL...I had a good laugh. He was getting quite pissed off and swearing at these properties because they were getting called before running code that accessed them. I looked down at his watch window & had a big fat laugh. Anyway, To the poster: If you've added these properties to the watch window OR if you even mouse over them while debugging, the code inside will be executed by the debugger WITHOUT stopping at your breakpoints inside the get/set.

                        S Offline
                        S Offline
                        stuebbie
                        wrote on last edited by
                        #14

                        Hi, Thanks for this post; I removed the watch items related to the instantiated object, and this has resolved the issue with all methods being called. Thanks, Stuart

                        1 Reply Last reply
                        0
                        • S stuebbie

                          Hi, Code snippet is below. I've tried to include only sections that are of interest. When I instantiate this class, the addParameter method is run without invoking the public property AddParameter: public class foo { private string[,] m_strParams = new string[c_RowArrayMaxValue,c_ColArrayMaxValue]; private int m_ParamCount = 0; public Boolean AddParameter { get { return addParameter(m_ParamName, m_ParamValue, m_ParamDataType, ref m_ParamCount); } } protected Boolean addParameter(string strParamName, string strParamValue, string strParamDataType, ref int intParamCount) { try { m_strParams[Convert.ToInt32(intParamCount), 0] = strParamName; m_strParams[Convert.ToInt32(intParamCount), 1] = strParamValue; m_strParams[Convert.ToInt32(intParamCount), 2] = strParamDataType; intParamCount += 1; return true; } catch (Exception e) { Debug.WriteLine("Foo.cs - addParameter - Exception found: " + e.InnerException); return false; } } } Thanks, Stuart

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #15

                          As metioned elsewhere in the thread, it's very possible that the debugger is evaluating the properties. Why are you using a property to move data in the class? That contradicts what a property is intended for. Just make it a method, and the code almost makes sense.

                          Despite everything, the person most likely to be fooling you next is yourself.

                          1 Reply Last reply
                          0
                          • S stuebbie

                            Hi, Code snippet is below. I've tried to include only sections that are of interest. When I instantiate this class, the addParameter method is run without invoking the public property AddParameter: public class foo { private string[,] m_strParams = new string[c_RowArrayMaxValue,c_ColArrayMaxValue]; private int m_ParamCount = 0; public Boolean AddParameter { get { return addParameter(m_ParamName, m_ParamValue, m_ParamDataType, ref m_ParamCount); } } protected Boolean addParameter(string strParamName, string strParamValue, string strParamDataType, ref int intParamCount) { try { m_strParams[Convert.ToInt32(intParamCount), 0] = strParamName; m_strParams[Convert.ToInt32(intParamCount), 1] = strParamValue; m_strParams[Convert.ToInt32(intParamCount), 2] = strParamDataType; intParamCount += 1; return true; } catch (Exception e) { Debug.WriteLine("Foo.cs - addParameter - Exception found: " + e.InnerException); return false; } } } Thanks, Stuart

                            J Offline
                            J Offline
                            J4amieC
                            wrote on last edited by
                            #16

                            There is nothing in the code you have posted that would do that, although its obvious that you've missed out some of your original code as there are no m_ParamValue,m_ParamDataType etc variables defined. However, the fact that a get accessor invokes the method you think is being called, makes me think you have a watch defined somewhere in the IDE which is being evaluated as soon as this object comes into scope.

                            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