32: Static Methods And Data. Always Available.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

Declaring methods inside classes is great but what do you do when you want a method you can call anytime? If you’re using C++, then just write a method. But if you still want your method in a class or if you’re using C# where all methods need to be in classes, then you’ll need to make your method static. And you can declare static data too. What does that do? This episode describes a class called string with a static method called isNullOrEmpty which takes a single parameter of type string and returns true if the passed string is null or if it’s empty. If the passed string is valid and has characters, then this method returns false. If you have some code outside of the string class and declare a string variable like this: C++ code: string * name = someMethodThatMightReturnAString(); if (string::isNullOrEmpty(name)) { // Do something else because we don’t have a string. } C# code: String name = SomeMethodThatMightReturnAString(); if (String.IsNullOrEmpty(name)) { // Do something else because we don’t have a string. } Notice how there are slight differences between the two examples due to pointers vs. references and due to normal naming differences between C++ and C#. C++ also uses two colons between the class name and the static method while C# uses a dot. Listen to the full episode or you can read the full transcript below for more about static methods. Transcript When you declare a class method to be static, it stops being associated with a specific instance of the class and behaves more like a standalone method. It’s still a member of the class though and has access to protected and private members. To call a static class method from some code that’s not already part of the class, just scope the method to the class by putting the class name before the method name. A method that’s not static is called an instance method because you need to create an instance of your class first before you can call the method. You cannot call an instance method by just scoping the method to the class name. Instance methods must be called through the instance variable you created. Let’s take an example of a string class. Strings can have text stored in them or can sometimes be empty. In C#, the string class is a reference type which means your string variable can also be null. A null string is different than an empty string. Null means you have no string at all. Empty means you have a string but it just has no text. If you want to find out how long your string is, or in other words, how many characters are in your string, you can call the instance method Length. The reason Length is an instance method is because it doesn’t make any sense at all to ask how long are all strings. You can only ask how long is a particular string. The string “cat” is three characters long. An empty string is zero characters. But what if your string is null? If you try to call the Length method on a null string, you’re going to get a NullReferenceException. That’s a bad thing. And your program will probably crash. Let’s say you don’t really care if a particular string is null or empty. You just want to make sure that there is some text inside the string. Well, you can’t just call the Length method or any instance method because if you happen to have a null string, then it’ll crash. So you could instead test for null first and if your string is not null, then check the length. That’s a lot of extra code especially when there’s a better way. What you need is a method, and let’s call this method, IsNullOrEmpty, that effectively does what I just described. The method will require a single parameter which is the string you want to test and it will first make sure the string is not null before then calling Length. Then you need to ask yourself, where should such a useful method exist? It can’t be an instance method in the string class b