SIGN UP MEMBER LOGIN:    
ARTICLE

Creating a Class and Call it from Main Program

Posted by Mahesh Chand Articles | C# Language September 30, 2000
This article explains how to create your class, add some functions and call it from Main function.
Reader Level:
Download Files:
 

This article tells you how to create your own class with some function in it and how to call these functions from main.

1. Creating your class : This is how my class mcCalculator look like. Its has five functions besides constructor and destructor called Add, Subtract, Devide ( spell mistake ) , Multiply, and DisplayOutVal. As you can see from their names, these functions adds, subtracts, multiplies, divides correspondingly and stores result in a private variable iOutVal which is called by DisplayOutVal to display the result.

public class mcCalculator
{
private int iOutVal;
//Default constructor
public mcCalculator()
{
iOutVal = 0;
}
public mcCalculator(int iVal1 )
{
iOutVal = iVal1 ;
}
//destructor
~mcCalculator()
{
iOutVal = 0;
}
//methods
public void displayiOutVal()
{
Console.WriteLine("iOutVal = {0}", iOutVal);
}
// this function adds two values and saves in iOutVal
public void add(int iVal1, int iVal2 )
{
iOutVal = iVal1 + iVal2 ;
}
// this function subtracts one from other and saves in iOutVal
public void subtract(int iVal1, int iVal2 )
{
iOutVal = iVal1 - iVal2 ;
}
// this function multiplyies two values and saves in iOutVal
public void Multiply(int iVal1, int iVal2 )
{
iOutVal = iVal1 * iVal2 ;
}
// this function devides one value from other and saves in iOutVal
public void Devide(int iVal1, int iVal2 )
{
iOutVal = iVal1 / iVal2 ;
}
}

2. Calling your class from Main : Now call mcCalculator from main. First create instance of mcCalculator and then call its member functions.

// Main Program
class mcStart
{
public static void Main()
{
mcCalculator mcCal =
new mcCalculator(50);
mcCal.add(12, 23 );
mcCal.displayiOutVal();
mcCal.subtract( 24, 4 );
mcCal.displayiOutVal();
mcCal.Multiply( 12, 3 );
mcCal.displayiOutVal();
mcCal.Devide( 8, 2 );
mcCal.displayiOutVal();
}
}

3. Don't forget to call using System; as first line of your file.

Quick Method: Download attached file second.cs and run from command line csc second.cs.

Login to add your contents and source code to this article
share this article :
post comment
 

how to write the code in class file and i will call into aspx environm

Posted by Gopal krishna Dora Nov 21, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor