Design a site like this with WordPress.com
Get started

Day 71: Static

As I mentioned yesterday, something else ‘clicks’ with every iteration of a different C# tutorial.  That’s my programming learning style: I need to read various definitions and type out different examples for it to sink in (or maybe I’m just a slow learner…)

Although it hasn’t quite ‘clicked’ yet, the fuzzy picture surrounding the static keyword is beginning to sharpen:

  • There are static classes and members
  • A static member is a member of the class, not a member of an instance of the class
  • Static does not mean “cannot be changed”
  • A static class is directly accessible by its name, can’t be instantiated, can’t be inherited (sealed)

Current Progress Status

  • Tower Defense Tutorial: 65/73
  • The Art of Game Design by Jesse Schell (pg 387/518)
  • C# The Yellow Book (pg 113/216)

Onward!

Advertisement

Author: Mike@PAUSED

Aspiring indie game developer. Devlog: www.paused.ca

6 thoughts on “Day 71: Static”

  1. Think about it like this, something static is always available whether you created an object or not. If you do string.IsNullOrEmpty(myString) you are using a static method (https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netframework-4.8). I don’t need a instantiated string class to use the method IsNullOrEmpty.

    In an RPG game you need a lot of container objects, e.g., chests, pouches, backpacks, etc. They all share a certain set of properties so you create a parent Container class and then derive a Chest, Backpack, and other objects. The Container class could have a method similar to the string class that checks if a given Container (or a derived class) holds any objects. This way you have a quick and easy way to see if a container holds anything. You can use this in a mouse over tip to inform the player if a chest is empty.

    I created a Fiddle that you can run, https://dotnetfiddle.net/urlnvC.

    using System;
    using System.Collections.Generic;

    public class Program
    {
    public static void Main()
    {
    Chest myEquipment = new Chest();

    Console.WriteLine(string.Format(“Newly created Chest, does it contain anything? {0}”, Container.IsEmpty(myEquipment)));

    myEquipment.Equipment.Add(“item 1”);
    Console.WriteLine(string.Format(“I added an item, what about now? {0}”, Container.IsEmpty(myEquipment)));
    }
    }

    public class Container
    {
    public Container()
    {
    Equipment = new List();
    }

    public List Equipment
    { get; set; }

    public static bool IsEmpty(Container checkForEquipment)
    {
    return checkForEquipment.Equipment.Count > 0;
    }
    }

    public class Chest : Container
    {
    //
    }

    Output:
    Newly created Chest, does it contain anything? False
    I added an item, what about now? True

    Liked by 1 person

    1. Having a Fiddle with your tutorial is an awesome way to learn because I can instantly play around with the code.

      When I removed “Static” from Main, it still ran the same. Is the static necessary in Main for this example, or is it there for when you build upon this class further?

      (Of course when I removed the static from the bool itself it wouldn’t compile)

      Liked by 1 person

      1. It’s the standard convention when working with C# that the main entry point be static. In Unity, you won’t see this. But if you create a console app (e.g., a new Fiddle), you will see that static Main method is the default. The compiler needs something to call for the Program class, and since Main is static the compiler doesn’t need to instantiate the Program class first.

        I guess that Fiddle adds the static back in before the Main() if you delete it, as according to MS doc the Main method must be static.

        MS Doc
        https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/

        Liked by 1 person

      2. I found a bug, the Container.IsEmpty is returning the opposite of what is expected. If you change “…Count > 0” to “…Count == 0” then it returns the correct bool.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: