[C#] Tutorial: Understanding Classes

Posts 1–10 of 10 · Page 1 of 1
[C#] Tutorial: Understanding Classes
Start a console app project and add this code inside the namespace



- Creating the class called Output
Code:
class Output
- Declaring two strings the will hold our text
Code:
        string myString;
        string nextString
- Creating a method called Output and declaring two new strings that will hold the text that will be outputted.
Code:
 public Output(string inputString, string input2String)
        {
            myString = inputString;
            nextString = input2String;
        }
- Creating the method that will allow us to print (we will call this later)
Code:
        public void print()
        {
            Console.WriteLine("{0} {1}", myString, nextString);
        }

So far you should look like this:
Code:
class Output
    {
        string myString;
        string nextString;

        
        public Output(string inputString, string input2String)
        {
            myString = inputString;
            nextString = input2String;
        }


        public void print()
        {
            Console.WriteLine("{0} {1}", myString, nextString);
        }


    }

Next add this:
Code:
class program
    {

        public static void Main() 
    {
        
        Output Out = new Output("First one", "Next one");

        
        Out.print(); 
        Console.Read();
    }
    }

Code:
Output Out = new Output("First one", "Next one");
This snippet is declaring the class Output as a new name, out. It is setting it equal to Output with two strings.

Code:
        Out.print(); 
        Console.Read();
Now we call our print function which will display "First one Next one" on the screen. console.Read(); just holds it on the screen.

Your finally code should look like this:
Code:
class Output
    {
        string myString;
        string nextString;

        // Constructor
        public Output(string inputString, string input2String)
        {
            myString = inputString;
            nextString = input2String;
        }


        public void print()
        {
            Console.WriteLine("{0} {1}", myString, nextString);
        }


    }


    class program
    {

        public static void Main() 
    {
        
        Output Out = new Output("First one", "Next one");

        
        Out.print(); 
        Console.Read();
    }
    }
If I was of any help press thanks, if you have any questions please post them.
Not very well explained really. No mention of what a constructor is or does (you just said make a method called Output..lol), sorta just copy and paste this and voila you have a class. More detail is needed because this is a fundamental in programming, it's necessary to explain it properly so people don't get the wrong impression at the very start of their programming career. My Java lecturer was explaining classes the other day and he did a good job (despite the fact that I nearly fell asleep with boredom) explaining all about instance variables, access modifiers, constructors, methods...etc. You need to actually go into some detail for this to count as a tutorial.
Quote Originally Posted by Jason View Post
Not very well explained really. No mention of what a constructor is or does (you just said make a method called Output..lol), sorta just copy and paste this and voila you have a class. More detail is needed because this is a fundamental in programming, it's necessary to explain it properly so people don't get the wrong impression at the very start of their programming career. My Java lecturer was explaining classes the other day and he did a good job (despite the fact that I nearly fell asleep with boredom) explaining all about instance variables, access modifiers, constructors, methods...etc. You need to actually go into some detail for this to count as a tutorial.
TBH, i didn't read all your post, but I think it's pretty much easy to understand what he said.....Really really easy.

ps:
make it:
Code:
public static void Main(string[] args)
{
      //BlaBla
}
Just a recomandation
Quote Originally Posted by user590177 View Post


TBH, i didn't read all your post, but I think it's pretty much easy to understand what he said.....Really really easy.

ps:
make it:
Code:
public static void Main(string[] args)
{
      //BlaBla
}
Just a recomandation
You even know what you're talking about? .

If you actually bothered to read my post, I said classes were a <let me highlight the import parts for your selective reading>

FUNDAMENTAL ASPECT of learning to program, i.e you LEARN THEM AT THE BEGINNING WHEN YOU DON'T KNOW AN INT FROM A STRING so special attention must be given to ENSURE THE NEW PROGRAMMER UNDERSTANDS WHAT A CONSTRUCTOR IS, WHAT A METHOD IS, WHAT ACCESS MODIFIERS DO, HOW TO CONSTRUCT A NEW INSTANCE OF THE CLASS AND CALL IT'S METHODS.

Shit man, I didn't have any trouble understanding it either, but...oh wait... I already know how to program. Of course it's easy to understand if you know how to program, fuck sake.
Quote Originally Posted by Jason View Post


You even know what you're talking about? .

If you actually bothered to read my post, I said classes were a <let me highlight the import parts for your selective reading>

FUNDAMENTAL ASPECT of learning to program, i.e you LEARN THEM AT THE BEGINNING WHEN YOU DON'T KNOW AN INT FROM A STRING so special attention must be given to ENSURE THE NEW PROGRAMMER UNDERSTANDS WHAT A CONSTRUCTOR IS, WHAT A METHOD IS, WHAT ACCESS MODIFIERS DO, HOW TO CONSTRUCT A NEW INSTANCE OF THE CLASS AND CALL IT'S METHODS.

Shit man, I didn't have any trouble understanding it either, but...oh wait... I already know how to program. Of course it's easy to understand if you know how to program, fuck sake.
damn sorry if I made you mad. but I didn't say that you can't program..Ar nvm.Sorry again...
Quote Originally Posted by user590177 View Post


damn sorry if I made you mad. but I didn't say that you can't program..Ar nvm.Sorry again...
I never said you did say that, I said it IS easy to understand...if you already know how to program. To beginners that actually need the tutorial, they won't get some of the concepts.
Quote Originally Posted by Jason View Post


I never said you did say that, I said it IS easy to understand...if you already know how to program. To beginners that actually need the tutorial, they won't get some of the concepts.
well now I understood. Well, yes you're right, he should explain what he said...Thanks, but the method that you said your previous post was like: WAW! .
Quote Originally Posted by user590177 View Post


well now I understood. Well, yes you're right, he should explain what he said...Thanks, but the method that you said your previous post was like: WAW! .
[highlight=c#]
public Output(string inputString, string input2String)
{
myString = inputString;
nextString = input2String;
}
[/highlight]

How is a person with little prior programming experience going to know that this is a constructor?
O_O im still learning c++ sooo this needs to be explained a little more
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us