Methods in C#

Methods in C#

Postby joppiesaus » 30 Jun 2013, 14:42

Hey! I am again bored so here's another tutorial, I hope this will help you.
For more help check [url="http://mcdzienny.cba.pl/viewtopic.php?f=30&t=2196"]this[/url] out.

So, I made a quick class called Book.
In the constructor you can see that the name and author are defined.
Code: Select all
using System;
using System.Linq;
using System.Text;

namespace Book
{
    public class Book
    {
        public string name { get; set; }
        public string author { get; set; }
        public int amountOfWords { get; set; }

        public Book(string name, string author)
        {
            this.name = name;
            this.author = author;
        }
    }
}


Now, we add a method, because amountOfWords is not defined!
We do something random, just to show you arguments:
Code: Select all
        public int getAmountOfWords(string nameOfBook)
        {
            Random random = new Random(nameOfBook.Length);
        }


We define a new random. Inside the constructor is a argument, called seed. It's an int, it will make the random more random. :P
random.Next(int min, int max) is a method from the Random class. min is the minimal outcome, max is the maximum outcome.

Now we must return a value, it is a int. It will return the minimal number of 1000 words, and the maximum is 10000. Somewhere between that. :)
Code: Select all
        public int getAmountOfWords(string nameOfBook)
        {
            Random random = new Random(nameOfBook.Length);

            return random.Next(1000, 10000);
        }


Next up we are going to make a method called sayItNice. Just to tell somebody the name of the book, the author and the amount of words the book has.
Code: Select all
        public string sayItNice(string name, string author, int amountOfWords)
        {

        }


Now we make the return value:
Code: Select all
        public string sayItNice(string name, string author, int amountOfWords)
        {
            return "The book is called " + name + ", and the author of this awsome book is " + author + ", it has about " + amountOfWords.ToString() + " words!";
        }

The int needs a ToString() method because it is not a string, but a int. You can't just set a int in a string.

So what you could do(If you have a console application) is print it in the class called Program.
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Book
{
    class Program
    {
        static void Main(string[] args)
        {
            // define a new book!
            Book book = new Book("Mr. Bean is lost", "Peter");

            // Set the value amountOfWords of the book using the method we created!
            book.amountOfWords = book.getAmountOfWords(book.name);
           
            // Print it using the method we created!
            Console.WriteLine(book.sayItNice(book.name, book.author, book.amountOfWords));

            // And a simple command that makes the program not instant-quit
            Console.ReadKey();
        }
    }
}

Output:
Knipsel.PNG
Output of the program we wrote.
Knipsel.PNG (4.42 KiB) Viewed 594 times


Now, why don't we just make it simpler?!
We change the method book.sayItNice to a void! That will save us some code! :D
Code: Select all
        public void sayItNice(string name, string author, int amountOfWords)
        {
            Console.WriteLine("The book is called " + name + ", and the author of this awsome book is " + author + ", it has about " + amountOfWords.ToString() + " words!");
            Console.ReadKey();
        }


The Program class will be the code now:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Book
{
    class Program
    {
        static void Main(string[] args)
        {
            // define a new book!
            Book book = new Book("Mr. Bean is lost", "Peter");

            // Set the value amountOfWords of the book using the method we created!
            book.amountOfWords = book.getAmountOfWords(book.name);

            //print it with our new void!
            book.sayItNice(book.name, book.author, book.amountOfWords);
        }
    }
}

The output will be the same, even the amount of words because it is based on a seed.

I hope you learned something from it. Good luck with it. <ok>
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron