0

I am making a college project. It s a chat application in a Windows form. I would like to understand the correct way to have a global variable even though this is against the oop philosophy!

My main class calls a method within another class to first get the chat messages to this point. Then I would use a global variable to track the last update message and then retrieve from the database only the newer entries. So this variable will hold then entryid of the latest update

If there is a way better than a global then I am all ears. Otherwise can you advise how best to achieve this goal?

Thanks Ps I am a newbie - be kind!

Piquet
  • 33
  • 3
  • 1
    That dupe candiate is closed but has some usable answers. – H H Jan 17 '16 at 18:50
  • You already are aware that this isn't a good practice. The "better way" is a bit too broad for an answer but start by distinguishing classes and objects. – H H Jan 17 '16 at 18:51
  • 3
    @Piquet, I think your question would be much easier to answer (and more concrete) if you will post some of the code that you mentioned... – Nikolai Samteladze Jan 17 '16 at 18:55
  • 1
    There is no good way. It does not cost anything to do this correctly, at a minimum use a static **property**. With an explicit setter so you can debug the typical problems caused by globals. And find at least 10 reasons to make it *public* instead of *internal*. – Hans Passant Jan 17 '16 at 20:30

2 Answers2

0

I believe you want to use global variable because you want to have global access. I recommend you to use abstraction which will be thread safe, especially for something, that will hold messages between users. Take into consideration singleton. In case if you'll have many users writing messages simultaneously, you can apply singleton mentioned in that article in order to syncrhonze sessions, messages etc.

Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54
0

Here's how I'd do that. In your class which goes and reads the newer entries, make a property that will hold the last entryid. That can store the value for you, in your instance of the class. So when the method (in the same class) that reads the newer entries is called, it will just look at this entryid property, and pass that in to the SQL to read only ones that are newer than that (and then update the entryid with the newer last entryid).

As long as you always use that one instance of the class, your last entryid will be stored. This would be better than a global variable, as it uses the language as it was intended.

Something like this:

public class ReadNewestEntries {
  public int entryId {get;set}
  public List<someDataType> ReadNewEntries() {
     // some code to read the database, using the entryId

     // Get last ID read
     entryId = dataList.Max(x => x.entryId);
  }
}

// in main class:
ReadNewestEntries getNewEntries = new ReadNewestEntries();
// Then just keep this class instance around to use
List<someDataType> = getNewEntries.ReadNewEntries();

Your "global variable" is just the entryId int in the class itself, which will hold and keep for you your last ID read. I didn't test the above code out, but hopefully that helps you, and without using a global variable.

astrosteve
  • 320
  • 3
  • 11