Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello People

I am trying to understand how the classes works in c#.
I have made following code below, my issue is how do I print "hello" from the outputclass.

see code below:




C#
using System;
//NAMESPACE
namespace Lesson
{

    class Program
    {
        public static void Main()
        {

            //Alert my Printstring

        }
    }

    class Outputclass
    {
        string myString;


        public void PrintString()
        {
            Console.WriteLine("Hello!");
        }

        public Outputclass(string inputString)
        {
            myString = inputString;
        }
    }
}




Could someone help me?

Thank you in advance
Posted
Updated 15-Nov-15 23:37pm
v2

C#
using System;
//NAMESPACE
namespace Lesson
{

class Program
{
public static void Main()
{

 Outputclass o=new Outclass();
 o.PrintString();

 Console.Readline(); 
}
}

class Outputclass
{
string myString;


public void PrintString()
{
Console.WriteLine("Hello!");
}

public Outputclass(string inputString)
{
myString = inputString;
}
}
}
 
Share this answer
 
you need to

1) instantiate/create an object of type Outputclass - in your 'main'
2) call the PrintString() method of the object you instantiated at (1)

(it seems a bit pointless to be creating the object of type Outputclass with a string, you dont use the string, but lets get past the first hurdle)

so, how do you do point (1) .. hint, lookup the 'new' keyword
 
Share this answer
 
Create an instance of Outputclass and then call the print method.
Outputclass opc = new Outputclass();
opc.PrintString();


You would do this in the main method.
 
Share this answer
 
v2
you need to create an object of class Outputclass in main()



C#
class Program
{
    public static void Main()
    {

       Outputclass objOutputclass = new Outputclass(); // this will allocate memory 
 
objOutputclass.Outputclass("this text will not going to print");
    }
}

//then you need to call PrintString() in Outputclass

class Outputclass
    {
        string myString;
 

        public void PrintString()
        {
            Console.WriteLine("Hello!");
        }
 
        public Outputclass(string inputString)
        {
            myString = inputString;
            PrintString();
        }
    }
 
Share this answer
 
Hi everyone I have tested your solutions,

I understand that I have to call the class.


I using this code below:
C#
using System;
//NAMESPACE
namespace Lesson
{

    class Program
    {
        public static void Main()
        {

            //Alert my Printstring


            Outputclass opc = new Outputclass();
            opc.PrintString();


        }
    }

    class Outputclass
    {
        string myString;


        public void PrintString()
        {
            Console.WriteLine("Hello!");
        }

        public Outputclass(string inputString)
        {
            myString = inputString;
        }
    }
}




but I get following error:

Error 1 'Lesson.Outputclass' does not contain a constructor that takes 0


Could someone help me?

Thank you in advance
 
Share this answer
 
Comments
sanjay243657 16-Nov-15 7:03am    
Hi,
You can pass value in Constructor
class Program
{
static void Main(string[] args)
{
Outputclass opc = new Outputclass("Hello !");
opc.PrintString();
Console.ReadLine();
}
}
class Outputclass
{
string myString;

public void PrintString()
{
Console.WriteLine("Hello!");
}

public Outputclass(string inputString)
{
myString = inputString;
Console.WriteLine(myString);
}
}
Your code will never compile because 'Outputclass does not contain a constructor that takes zero arguments.

You need to get a good book on .NET and study the basic aspects of using 'Classes. ".NET Zero" by Charles Petzold is free to download: [^].

You can use a working example of a read-eval-print-loop, like the one shown here to study how a Console Application works: set break-points in the code and single-step (F11 in Visual Studio) through the code:
C#
class Program
{
    public static void Main()
    {
        // set a break-point here, then single-step through code
        string inputMessage = "Enter <any characters=""> to continue: Enter <return> to Exit";
        string newEntry = "You entered: ";

        Outputclass o = new Outputclass("get a good .NET book");
        o.PrintString();

        Console.WriteLine(inputMessage);

        string line;

        while ((line = Console.ReadLine()) != string.Empty)
        {
            // set a break-point here
            o.myString = string.Format("{0}{1}", newEntry, line);
            o.PrintString();
            Console.WriteLine(inputMessage);
        }

        Console.WriteLine("exiting Application");
        
        Environment.Exit(0);
    }
}

class Outputclass
{
    public string myString { set; get; }

    public void PrintString()
    {
        Console.WriteLine(myString);
    }

    public Outputclass(string inputString)
    {
        myString = inputString;
    }
}
 
Share this answer
 
v2
Thank you very much Bill Woodruff
 
Share this answer
 
Comments
CHill60 17-Nov-15 12:53pm    
Please stop posting comments as solutions - if you have something to say to a member then use the "Have a Question or Comment?" link next to their post - they will automatically be notified
I got an updated code:

how do I alert the "myCompany AB"



<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace MyProgram<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            CompanyInformation companyInfo = new CompanyInformation();<br />
            Company Compinf = new Company();<br />
            Console.WriteLine(Compinf.ToString());    // Alert goes here<br />
        }<br />
    }<br />
    public class Company<br />
    {<br />
        private string m_name = string.Empty;<br />
        public string GetName()<br />
        {<br />
            return m_name;<br />
        }<br />
        public void SetName(string name)<br />
        {<br />
            m_name = name;<br />
        }<br />
    }<br />
    public class CompanyInformation<br />
    {<br />
        public static void main()<br />
        {<br />
            Company companyInf = new Company();<br />
            companyInf.SetName("MyCompany AB");<br />
            Console.ReadLine();<br />
        }<br />
    }<br />
}<br />
<br />


I dont get the result why?

Thank you in advance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900