sample code of Out parameter in c#

in Out parameter no need to assign the value before passing.

but in Ref Parameter you must have to assign the value before passing .

see the gender variable in this code

See the sample code for OUT parameter

 

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)

{
string Gender;
string printValue;

//some class properties assignment
clsAll objEmployeedetails = new clsAll();
objEmployeedetails.Name = “tarun”;
objEmployeedetails.age = 25;
objEmployeedetails.Address = “This is the address”;

//calling function
printproperties(objEmployeedetails,out Gender);

printValue=string.Format(“Gender Value after calling the function is {0}”,Gender.ToUpper());
Response.Write(printValue);
}

private void printproperties(clsAll getEmployeeDetails,out string gender)
{
//Assigning the value in Gender Variable here
gender = “female”;

}
}

 

 

So output will be 

Gender Value after calling the function is FEMALE

Advertisements

Create Your Own Exception Class [C#,Asp.net]

In some bigger application we used to create our own exception class.

i have written one example have a look on it

public class ClsTarunException : ApplicationException
{

//These are Class Properties
private DateTime _TimeOccured;
private String _Reason;
private String _Message;

public DateTime TimeOccured
{
get { return _TimeOccured; }
set { _TimeOccured = value; }
}

public String Reason
{
get { return _Reason; }
set { _Reason = value; }
}

public override string Message
{
get { return _Message; }
// set { _Message = value; }
}

//Here is the constructor
public ClsTarunException()
{
//No Parameter constructor
}
public ClsTarunException(string message, string cause, DateTime time)
{
//Constructor with parameter
_Message = message;
_Reason = cause;
_TimeOccured = time;

}

}

 

 

 

And this the Page where i am using this exception class

public partial class TarunException : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//This function will throw the exception
throwMyError();
}
catch (ClsTarunException ex)
{
Response.Write(string.Format(“This is the message—-{0}”,ex.Message));
Response.Write(string.Format(“<br>This is the Time when exception occured —{0}”, ex.TimeOccured));
Response.Write(string.Format(“<br>This is the reason why the exception occured —-{0}”, ex.Reason));
}
}

private void throwMyError()
{
ClsTarunException Excep = new ClsTarunException(“My tank Is full”, “i was eating like a big Elephant”, DateTime.Now);
throw Excep;
}
}

 

 

 

See here is the Output on page load

This is the message—-My tank Is full
This is the Time when exception occured —12/19/2012 7:23:34 PM
This is the reason why the exception occured —-i was eating like a big Elephant