Saturday, May 31, 2014

Cosmos - Part 2 - Let's make a shell

This advertising slot is open! To have a paragraph here featuring your company (doesn't have to be tech related), go to

Hey blog readers! we have a new Facebook page! Check it out at

Hello once again blog readers. If you haven't already, go check out part one first before you try any of the examples below. It will save you a lot of headaches. But for everyone else, welcome back! So now we can get started editing code. The first thing you want to do is open up your project from last time. Open Visual Studio and go to File->Recent Projects and Solutions-> and the name of your project. If you installed Visual Studio for the first time in the last post, then it should be the only one that pops up. Once you have that open, it should look like this.
Once your windows looks like this, we can continue on. Just note that the line that says namespace TOS  Should look say namespace and then the name of your project.
The first step is to delete the default code and add some more code. Go ahead and delete the code from your windows so it looks like this:
You will have to type in those two lines with the green text. And once again, line 6 should have the name of your project, not TOS (see, this is why you can't just copy and paste peoples code and expect it to work). But once you have that up, we can get started coding. Here is the completed code. Go ahead and type it in and then I will go through it line by line. I have included a picture of the code because practice makes perfect. Typing the code will help you learn it faster. And it lets you see how it should look on your screen (if something is a different color, you probably did something wrong).
If you go ahead and run (click the green arrow), you will see  it run where it will ask you for your name, and then show a prompt (#). Go ahead and type in "echo" without the quotes and press enter. You should see "echo echo echo...". When you see the "#" again, go ahead and type in something random and press enter. You should see "ERROR: unknown command". Now let's go ahead and review the code so you can see what it actually does in the program.

Lines 1-9 do nothing important at the moment. But without them, your code wouldn't run. Lines 10-18 is the BeforeRun() method. This is what Cosmos calls as soon as it loads. BeforeRun() just runs once, so this is where you usually put loading commands and things like that. On line 12, you see Console.WriteLine("Loading...");. Anytime you see Console.WriteLine("Some Text Here");, you are telling the computer to write "Some Text Here" to the screen. The next line (line 13) is what's called a comment. This line is ignored by the compiler and is just for you. It's just a note so you remember what your code does in that spot or what you need to add in that spot. Honestly you could have just skipped that line and it would have not made a difference in your program, but it is a very good habit to comment your code. Moving on to line 16, we see some new code. The first part is "String name". This is called a variable. It stores a value for us. String means that we want to store some text. There are other types of variables including Int (numbers), bool (true/false), and a lot more. But we will get to those in a later segment. The next part is the "=" sign. We are telling the computer that we want to assign the variable "name" the value on the other side of the "=" sign. That value that we want is Console.ReadLine();. Opposite from Console.WriteLine(), Console.ReadLine() lets the user type in a line of text and it will return it when the user presses enter. So in English, we are saying: Ask the user for a line of text. And when I ask for "name", tell me what the user just said. It may seem a little bit complicated, bit it will start to make sense over time.

Moving on to lines 20-31, we see the method Run(). Cosmos will loop this until your program quits (you shutting down VMware Player). So there is nothing new on lines 22 through 23, but 24 is definitely new. This is called an "if" statement. We are saying "if this is true, do everything inside of the brackets below". In this case, we are saying if the variable cmd equals "echo", then run Console.WriteLine("echo echo echo...");. Beneath it we have line 28. This is called an else statement. This only works if you put it directly beneath an "if" or an "else if" statement. This is saying "if the above if statement if false, do this". In this case, if cmd does NOT equal "echo", do Console.WriteLine("ERROR: unknown command");.

Well congratulations! You have created an interactive shell! In the next part, we can start working with some graphics. We can start with a mouse and work our way towards making a crude GUI (don't worry if you don't know what this means). If you ran into any trouble, feel free to leave a comment below and any of the authors will help you out. Remember that you don't need a google account to comment, just select "Anonymous". Just be warned that you will not receive emails when someone leaves an answer to your question. If you see any errors or improvements, feel free to notify us in the comments and we will make the appropriate changes. When you are ready, go to part 3 here -> Have Fun!

Hey blog readers! If you want to follow our meaningless updates, check out our pages here:
Twitter: Follow us @allstuffnerdy
If you have a business you would like us to mention at the beginning of a post, check out our gigs on Fiverr: