Newbie C# Question! Please help

Hi,

I just completed section 2 and I have no trouble understanding what Ben teaches. Though, I’ve had this question ever since he brought it up and I’m just curious…

I understand that you have to define a string in a function’s parameters. Such as :
ExampleFunction(string input)

If you are apart of his course then you’d understand that almost every function in Terminal Hacker has to define the string as “input” in its parameters

Why can you not define “input” as a string at the very top of the script once so you can use it throughout the whole script? I’ve tried this and the script just doesn’t run properly. Thank you and please word your answer(s) as simply as you can because I am a very slow person :blush:

Hi Jonathan,

You don’t have to. The use of the parameters will be determined by what the method needs to do.

The name input is not mandatory, it can be called whatever you want, just as when you declare a variable. However, in Terminal Hacker the text values that you pass around from method to method are typically based on the player’s input, so the name kinda makes sense.

A member variable, e.g. one declare within the class but outside of any methods will have a different scope to that of the parameters you define in your methods. Anything set in the member variable would be accessible to all methods, and, as you’ve stated, you’d only have to declare it the once. However, you would need to then also change all of the methods to look at that specific member variable as opposed to using the parameters that are currently being passed in.

Taking this a step further, the way that the code for the WM2000 currently works (not sure if you’ve had a look) is to use reflection to find all of the methods within the entire solution that have a specific name (OnUserInput from memory) and then invoke them, passing the player’s input as a parameter. This is how this method within your own Hacker.cs script gets executed each time there is a change in input received from the player.

I suppose you could use the OnUserInput method as-is, but then set the member variable from this method, which your other methods would look at. One of the risks I would suggest however is being 100% certain that the value that is now in the member variable (at any given time) was the value you were expecting and that it hasn’t been updated/changed since you started working with it. Where-as at the moment each subsequent method call is handed the value to use explicitly.

Hope this helps :slight_smile:

Rob, you are a BEAST. Thank you so much and cheers!

1 Like

You’re very welcome Jonathan :slight_smile:

If your query is resolved please use the “Solved” functionality to indicate the reply which resolved your query.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms