Start a console app project and add this code inside the namespace
- Creating the class called 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.