| title | Flow Control & Contract Structure | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| actions |
|
||||||||
| material |
|
As we have seen in Lesson 1, Chapter 9, a contract on the Neo network functions as a list of methods that can be called by other programs or clients. A common practice is to use the main method as the entry point, and use the first parameter for users to specify which method they want to call. Below is an example of the main method for a DNS smart contract.
public static object Main (string method, params object[] args) {
switch (method) {
case "query":
return Query ((string) args[0]);
case "register":
return Register ((string) args[0], (byte[]) args[1]);
case "delete":
return Delete ((string) args[0]);
default:
return false;
}
}Aside from the first parameter, which takes a string variable, the main methods takes an array of objects as parameters, which get cast into specified data types at method calls.
Note that byte[] is the most commonly used data type for verifying account and encryption. In the case of Register (), the args[1] parameter is the address of an account.
The main method uses a switch-case statement to cycle through method calls, and returns a boolean value of false if no matching statement is found.
- Create a public, static,
GenerateAlien ()method. Its return type isBigInteger. Its first parameter is a string calledalienName. Second parameter isbyte[]called owner. - Move the body of
Main ()to the new GenerateAlien () method. - Add
return 1;at the end of the method. This is a placeholder. - Use
switchstatement to check the value ofmethodvariable.- In the case that it is
generateAlien, return a call toGenerateAlienmethod. - In the default case, return false.
- In the method call, cast the first argument as
string, cast the second argument asbyte[]
- In the case that it is