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. .NET (Core and Framework)
  4. Problem while accessing values set in another class function.

Problem while accessing values set in another class function.

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcomtutorial
5 Posts 3 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.
  • N Offline
    N Offline
    Nilesh Hamane
    wrote on last edited by
    #1

    Hi Friends, In my project, i am using two classes suppose A and B. Class A contains two functions suppose SetInputF1 and SetInputF2 with same code and same return type just differ in function name (both functions are used to assign input values of class A to static global variables), and class B contains one function suppose f3. SetInputF1 is called by many functions. Now if i call SetInputF1 in f3 then the values of static global variables are not setting properly and if i call SetInputF2 in f3 then values are setting properly. I am using SetInputF2 function only due to this problem. But due to this my code length is increasing. How to overcome this problem :confused:, please help me if you can...

    http://nnhamane.googlepages.com/

    N D 2 Replies Last reply
    0
    • N Nilesh Hamane

      Hi Friends, In my project, i am using two classes suppose A and B. Class A contains two functions suppose SetInputF1 and SetInputF2 with same code and same return type just differ in function name (both functions are used to assign input values of class A to static global variables), and class B contains one function suppose f3. SetInputF1 is called by many functions. Now if i call SetInputF1 in f3 then the values of static global variables are not setting properly and if i call SetInputF2 in f3 then values are setting properly. I am using SetInputF2 function only due to this problem. But due to this my code length is increasing. How to overcome this problem :confused:, please help me if you can...

      http://nnhamane.googlepages.com/

      N Offline
      N Offline
      Nilesh Hamane
      wrote on last edited by
      #2

      One more thing, SetInputF2 function is not called by any function. If i call SetInputF2 function in any other function then same problem occuring as with SetInputF1 function.

      http://nnhamane.googlepages.com/

      N 1 Reply Last reply
      0
      • N Nilesh Hamane

        One more thing, SetInputF2 function is not called by any function. If i call SetInputF2 function in any other function then same problem occuring as with SetInputF1 function.

        http://nnhamane.googlepages.com/

        N Offline
        N Offline
        Narendra Reddy Vajrala
        wrote on last edited by
        #3

        i think due to the cause of other functions your global variable values are changing. better to clear those variables before calling your function.

        1 Reply Last reply
        0
        • N Nilesh Hamane

          Hi Friends, In my project, i am using two classes suppose A and B. Class A contains two functions suppose SetInputF1 and SetInputF2 with same code and same return type just differ in function name (both functions are used to assign input values of class A to static global variables), and class B contains one function suppose f3. SetInputF1 is called by many functions. Now if i call SetInputF1 in f3 then the values of static global variables are not setting properly and if i call SetInputF2 in f3 then values are setting properly. I am using SetInputF2 function only due to this problem. But due to this my code length is increasing. How to overcome this problem :confused:, please help me if you can...

          http://nnhamane.googlepages.com/

          D Offline
          D Offline
          dojohansen
          wrote on last edited by
          #4

          I don't think you have a good understanding of the problem. Chances are one of the methods doesn't actually assign the static field under some circumstance, and you need to understand what those circumstances are in order to see how to proceed from here. Also, I cannot help but wonder why you're using static fields. If they really belong to the class rather than an instance of the class then fine, but then you should also use static methods to work with the fields. Not that this would affect whether or not the fields get set; it just doesn't make any sense to require instance methods to be used to set class state - at least in most cases it wouldn't make sense. The short answer to most questions of the sort "my code says to add together two and two and store the result in the variable 'n', but after the code has run the value of n is five. Why?" is "attach the debugger". Step through your code and step into the "SetF.." methods and inspect local, instance, and class variables, and you'll probably soon find out exactly why it doesn't work as expected, while at the same time learning invaluable debugging skills you can use to solve hundreds of other problems. If that's not possible, post the minimum code required to reproduce the problem. If the problem is what you say it is, you should be able to reproduce it with simple classes a bit like this:

          public class A
          {
          static int n;
          static public void SetN(int value) { A.n = value; }
          static public void GetN() { return A.n; }
          }

          I presume you have a way to GET the value that is being set as well, although I do not understand why you'd use this approach over static properties.

          N 1 Reply Last reply
          0
          • D dojohansen

            I don't think you have a good understanding of the problem. Chances are one of the methods doesn't actually assign the static field under some circumstance, and you need to understand what those circumstances are in order to see how to proceed from here. Also, I cannot help but wonder why you're using static fields. If they really belong to the class rather than an instance of the class then fine, but then you should also use static methods to work with the fields. Not that this would affect whether or not the fields get set; it just doesn't make any sense to require instance methods to be used to set class state - at least in most cases it wouldn't make sense. The short answer to most questions of the sort "my code says to add together two and two and store the result in the variable 'n', but after the code has run the value of n is five. Why?" is "attach the debugger". Step through your code and step into the "SetF.." methods and inspect local, instance, and class variables, and you'll probably soon find out exactly why it doesn't work as expected, while at the same time learning invaluable debugging skills you can use to solve hundreds of other problems. If that's not possible, post the minimum code required to reproduce the problem. If the problem is what you say it is, you should be able to reproduce it with simple classes a bit like this:

            public class A
            {
            static int n;
            static public void SetN(int value) { A.n = value; }
            static public void GetN() { return A.n; }
            }

            I presume you have a way to GET the value that is being set as well, although I do not understand why you'd use this approach over static properties.

            N Offline
            N Offline
            Nilesh Hamane
            wrote on last edited by
            #5

            Thanks dojohansen for your reply. Actually static variables i have mentioned above are not actually variables..they work like static variables but they are objects of user defined classes(in dll) which are created for mapping the inputs, and the objects works as keys and can be used anywhere in the project. If i assign any input value to object then its value can be used throughout the program. So we use it...but sometimes value becomes undefined(error) as mentioned above. I thought that it was due to half code in .cpp and half in .h, so integrated all in .h file then also not working...while trying a lot i got the solution. I need to write same code with different function name and that function should not be called by any other function, then values are setting properly but code is increasing...dojohansen i have posted my query after lot of debugging and discussing with colleagues, hope you better understand my problem now. Thanks and Regards - Nilesh.

            http://nnhamane.googlepages.com/

            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