Skip to content

Methods to Use

At this point the programs we create will include sequences of method calls, with variables that we can use to work with data within our program.

To build programs we will need to have some methods that we can call. The great this is that library creators will describe the methods that they have created so that we can find and use them. In order to use this documentation we need to know how these methods will be described.

Method Declarations

Methods are usually communicated using part of their declaration. We will look at building these ourselves in Part 2, but for now we need to know some basics so that we can start calling methods.

Method declaration visualisation

The above image helps show the components of a method declaration. The key parts are:

  • The name of the method. Remember, to call the method you use its name.
  • The variables and types of any arguments you will need to pass when you call the method. These will be listed as variables after the method name in parentheses.
  • You can also see the type of data that is returned (where void means none).
  • You also need to check that the method is public and static. We can call these directly if you have using static ClassName; at the top of your code file. For example, WriteLine is in the SplashKitSDK.SplashKit code, so you can call it if you have using static SplashKitSDK.SplashKit at the top of the code.

The following code snippets show example method declarations.

public static void WriteLine (string value);

This is available to call directly, as it is public and static. We can use this in our code using:

using static SplashKitSDK.SplashKit;
WriteLine("Hello World");

The following shows the declaration of the Open Window method from the SplashKit documentation. It is similar to the code above, but also includes the class name so that you know where the method exists. We can call it directly as it is public and static.

public static Window OpenWindow(string caption, int width, int height);

This is in the SplashkitSDK.SplashKit class, so you can call the OpenWindow method using the following code. This will pass “This is the caption” to the caption data, 800 to the width, and 600 to the height. This code will return a Winddow value, which we can ignore or store in a Window variable.

using static SplashkitSDK.SplashKit;
OpenWindow("This is the caption", 800, 600);

For the final example let’s look at Fill Circle and Color Red. These have the following declaration on the SplashKit website.

public static void FillCircle(Color clr, double x, double y, double radius);
public static Color ColorRed();

We can see from its declaration that we can call these (they are public and static). Their names are FillCircle and ColorRed. Will FillCircle we need to pass it a Color, and three double values. ColorRed does not need to be passed anything, and will return a Color value. The following is an example of calling this method. Here we use the result of ColorRed() as the value passed into the color for FillCircle.

using static SplashkitSDK.SplashKit;
OpenWindow("This is the caption", 800, 600);
ClearScreen(ColorWhite());
FillCircle(ColorRed(), 400, 300, 100);
RefreshScreen();

In the next pages we will list the different methods that you will use to complete the activities in this chapter both for terminal and graphical programs.

Other Websites

You can call these methods on the class in which they are defined, and we will work up to this over the next few parts. To simplify things, we want to avoid doing this for now and instead call these directly as we have seen here.

When you read other websites, you will often see them calling methods like this: Console.WriteLine();. This is the equivalent of calling WriteLine(); when you have using static System.Console; at the top of your code. When you find code like this, you will need to convert it so that you can call the method directly. We are not using the System.Console version of WriteLine as it uses language features we have not covered yet.