Skip to content

Commit 7f89c8e

Browse files
committed
Add description of method declarations to sequence and data
1 parent 8e8d74e commit 7f89c8e

File tree

10 files changed

+260
-25
lines changed

10 files changed

+260
-25
lines changed

book

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/workspaces/the-programmers-field-guide/src/content/docs/book

resources/guide-diagrams.graffle

3.12 KB
Binary file not shown.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Methods to Use
3+
---
4+
5+
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.
6+
7+
To build programs we will need to have some [methods](/book/part-1-instructions/1-sequence-and-data/1-concepts/02-method) that we can [call](/book/part-1-instructions/1-sequence-and-data/1-concepts/03-method-call). The great this is that [library](/book/part-1-instructions/1-sequence-and-data/1-concepts/10-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.
8+
9+
## Method Declarations
10+
11+
Methods are usually communicated using part of their declaration. We will look at building these ourselves in [Part 2](/book/part-2-organised-code/2-organising-code/0-overview), but for now we need to know some basics so that we can start calling methods.
12+
13+
![Method declaration visualisation](./images/method-decl.png)
14+
15+
The above image helps show the components of a method declaration. The key parts are:
16+
17+
- The *name* of the method. Remember, to call the method you use its name.
18+
- 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.
19+
- You can also see the type of data that is returned (where `void` means none).
20+
- 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 `System.Console` code, so you can call it if you have `using static System.Console` at the top of the code.
21+
22+
The following code snippets show example method declarations.
23+
24+
In the details of the [Console code](https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-8.0#system-console-writeline(system-string)) in the .NET documentation, there is the following documentation to show one of the `WriteLine` methods.
25+
26+
```cs
27+
public static void WriteLine (string? value);
28+
```
29+
30+
This is available to call directly, as it is `public` and `static`. We can use this in our code using:
31+
32+
```cs
33+
using static System.Console;
34+
35+
WriteLine("Hello World");
36+
```
37+
38+
The following shows the declaration of the [Open Window](https://splashkit.io/api/windows/#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`.
39+
40+
```cs
41+
public static Window SplashKit.OpenWindow(string caption, int width, int height);
42+
```
43+
44+
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.
45+
46+
```cs
47+
using static SplashkitSDK.SplashKit;
48+
49+
OpenWindow("This is the caption", 800, 600);
50+
```
51+
52+
For the final example let's look at [Fill Circle](https://splashkit.io/api/graphics/#fill-circle-3) and [Color Red](https://splashkit.io/api/color/#color-red). These have the following declaration on the SplashKit website.
53+
54+
```cs
55+
public static void SplashKit.FillCircle(Color clr, double x, double y, double radius);
56+
public static Color SplashKit.ColorRed();
57+
```
58+
59+
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`.
60+
61+
```cs
62+
using static SplashkitSDK.SplashKit;
63+
64+
OpenWindow("This is the caption", 800, 600);
65+
ClearScreen(ColorWhite());
66+
67+
FillCircle(ColorRed(), 400, 300, 100);
68+
RefreshScreen();
69+
```
70+
71+
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.
72+
73+
## Other Websites
74+
75+
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.
76+
77+
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.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: Terminal Methods
3+
sidebar:
4+
label: " - Terminal Methods"
5+
---
6+
7+
Interacting with the [Terminal](/book/part-0-getting-started/2-computer-use/1-concepts/01-terminal) is pretty simple in code. There are basically three methods that you need to care about: `WriteLine`, `Write`, and `ReadLine`. These are all described in the [Console](https://learn.microsoft.com/en-us/dotnet/api/system.console?view=net-8.0#methods) page in the .NET documentation which we will summarise below, along with some methods from the [Convert](https://learn.microsoft.com/en-us/dotnet/api/system.convert?view=net-8.0#methods) code.
8+
9+
## WriteLine and Write
10+
11+
When you want to display something on the terminal, you use either the `WriteLine` or `Write` method. There are lots of different versions of these methods which will allow you to pass many values to be output - so we can think of these as having this declaration, where `...` is replaced with data of the different basic data types (integers, numbers, text):
12+
13+
```cs
14+
public static void Console.WriteLine(...);
15+
public static void Console.Write(...);
16+
```
17+
18+
These methods differ only slightly. The `Write` method outputs the data that you pass it, while the `WriteLine` method outputs the data and adds a new line. This means that with `Write`, the cursor will remain on the same line, and the next output (or input) will appear on the same line as the data written out. Whereas, `WriteLine` will advance the cursor to the start of the next line.
19+
20+
## ReadLine
21+
22+
You can use the Terminal for input as well as output. The `ReadLine` method allows you to read data from the Terminal, capturing the text the user types, placing it in a string, and returning it to you in a string when the user hits the enter or return key.
23+
24+
You can think of this as having the following method declaration:
25+
26+
```cs
27+
public static string Console.ReadLine();
28+
```
29+
30+
:::tip
31+
[ReadLine](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=net-8.0#system-console-readline) in the Console class uses a feature that lets you indicate that it may not return any data. This will generate warnings when we use it. To remove these warnings, for now, you need to set the *nullable* property in your *.csproj* file to *disable*.
32+
:::
33+
34+
## Converting Data
35+
36+
As you can see above, `ReadLine` only returns string data. When you need this to be a number, you need to convert it. The C# library provides a number of conversion methods in its [Convert](https://learn.microsoft.com/en-us/dotnet/api/system.convert?view=net-8.0#methods) code. We can use these with `ReadLine` to convert data the user enters into either an integer (int32) or a double.
37+
38+
```cs
39+
public static int Convert.ToInt32();
40+
public static double Convert.ToDouble();
41+
```
42+
43+
## Example
44+
45+
You can use these as shown below:
46+
47+
```cs
48+
using static System.Console;
49+
using static System.Convert;
50+
51+
string name;
52+
int age;
53+
54+
Write("Enter your name: ");
55+
name = ReadLine();
56+
57+
WriteLine($"Hello {name}!");
58+
59+
Write("How old are you? Enter your age: ");
60+
age = ToInt32(ReadLine());
61+
```
62+
63+
To remove the warning about the data returned from `ReadLine`, I have updated my csproj file to be as shown below.
64+
65+
```xml
66+
<Project Sdk="Microsoft.NET.Sdk">
67+
68+
<PropertyGroup>
69+
<OutputType>Exe</OutputType>
70+
<TargetFramework>net8.0</TargetFramework>
71+
<ImplicitUsings>enable</ImplicitUsings>
72+
<Nullable>disable</Nullable>
73+
</PropertyGroup>
74+
</Project>
75+
```

0 commit comments

Comments
 (0)