CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    59

    [RESOLVED] How to manipulate global vars

    Hello, Im having an issue with my globals. In particular a Bool value.

    I have created a public class,

    public class Globals
    {
    public bool dataToSend;
    }

    in one thread data is loaded into an array and this variable is changed to true.

    in another thread there is a constant loop that checks to see if this variable is set to true then sends it down the serial port. Although, after I change the variable in my first thread the value does not change for the second thread. Why would this be?

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: How to manipulate global vars

    Your public class is not static and so therefore not really 'Global' in the way you want it to be. You will need to make your member variable static too. You would access it using Global.dataToSend.

    Code:
    public static class Globals
    {
       public static bool dataToSend;
    }
    Because you're doing multithreading, you may want to look at locking, especially if you move onto having global objects in a multithreaded environment. I don't think it's really a problem for value types like boolean.
    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

  3. #3
    Join Date
    Mar 2011
    Posts
    59

    Re: How to manipulate global vars

    I found MY issue and will post it in case anyone reads this or someone is actually good at searching the internet.

    Inside the class that contains my first thread that loads the array and changes the var I have

    globals g = new globals();

    when I need to change the bool value i use

    g.dataToSend = true;

    In the other thread the reason I was getting a false return was because I was re declaring globals with another globals g = new globals(); (I was creating a seperate instance where dataToSend was not being changed.

    THE CORRECTION:

    this was actually really simple, in the second thread I removed the instance of globals and created an instance of my first thread heartBeat

    heartBeat hb = new heartBeat();

    then when I need to do the check it is

    if (hb.g.dataToSend)

    the last step for this to work was changing the instance in the heartBeat class

    globals g = new globals(); became public globals g = new globals();



    I hope this helps someone. I can write a better tutorial for it but am kind of pressed for time at the moment.

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: How to manipulate global vars

    I like your solution.

    Using a single instance of a non static class that you share between your objects (as you have done) can be a better way because:

    1. Having global objects means that anyone can use them. This ultimately can lead to higher coupling because any class can talk to it. It can lead to lazy code design because it would be easy to make lots of static classes that all talk to each other without any real structure to your code.
    2. You have much more control over the accessibility to your global class by passing it round as a single instance.

    On the downside, you have to pass references to your global object to every object that needs to access it and so for something that really is global, this can be a bit of a pain.

  5. #5
    Join Date
    Mar 2011
    Posts
    59

    Re: How to manipulate global vars

    Thanks, right now my fixes are usally a bit more complex than they need to be. Ive been out of the programing game for about 6 years and Ive only been working for about 2 months on it now, so I forgot a LOT of the little stuff.

    static is exactly what I needed for now, I really only have 1 public variable class, and actually im going to need ALL of my classes and forms to at least be able to read the data off of the variables, some will manipulate the data.

    Once I really get the main function down to this program its really just adding user defined inputs that will be stored in a global variable till its passed to a queue to be sent to the serial port. Thanks for Static again. I was just about to ask the question and I saw there was already an answer!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured