Constructors are for setting up your class.  They return the type of the
class and therefore you don't return a type.

If you want to calculate the area, as per your example, then you should set
up a property in your class that will calculate it when the user goes to
acquire it.  Don't try to put calculations in the constructor.  In your
case, the area will only be calculated when the class instance is created.
If the user changes the length and width (which they can because you
declared them as public) they will not get the correct area.  Plus, your
area is not available because it is not a class variable.

I'd suggest that you find a basic tutorial on object oriented programming to
get the basic concepts on this type of programming before you continue any
further.

...Glenn

On Thu, Sep 11, 2008 at 3:57 AM, VIKAS GARG <[EMAIL PROTECTED]>wrote:

> Do we need to define the return type of the constructor. Or does it carry
> a default return type
> Why do program gives error if we use void with the constructor name
>
>  using System;
>
> namespace ConsoleApplication18
> {
>    public class Program
>     {
>         public int length, width;
>     public Program(int l, int w)
>     {
>         length= l;
>         width= w;
>         int area= l*w;
>         Console.WriteLine(area);
>     }
>         //return (area);
>         //Console.WriteLine("The length and width of the surface is {0} and
> {0}", length, width);
>        //Console.WriteLine(area);
>     }
>     class Sample
>     {
>         static void Main()
>         {
>             Program p1 = new Program(10, 20);
>             {
>                 Console.WriteLine(p1.length);
>                 Console.WriteLine(p1.width);
>                 //Console.WriteLine(p1.area);
>                 Console.ReadKey();
>             }
>         }
>     }
> }
>
>
>
> In this program if I remove comments from
>          //Console.WriteLine("The length and width of the surface is {0}
> and {0}", length, width);
>        //Console.WriteLine(area);
> These two statements then what is wrong there
> It gives error on removing comments.
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web 
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---

Reply via email to