passing variables from a class in app_code without using 'return' - c#


How can i pass a variable such as a string or a no. from the class file to a webform control or the webforms codebehind file?

Can i use a session variable?

i cant use:

return variablehere;

as its used for another purpose already.. I hope this makes sense.


Who is Participating?
abhinaypConnect With a Mentor Commented:
Properties are best for u..

Here is a sample
//Ur classfile in AppCode

Public Class EmpClass
    private string _strFirstname;
    private string _strLastname;
   public string Firstname {
	get { return _strFirstname; }
	set { _strFirstname = value; }
   public string Lastname {
	get { return _strLastname; }
	set { _strLastname = value; }
public void EmployeeGet(int empID)
   _strLastname=GetEmployeLastName(empID);//ur code to get first name
   _strFirstname = GetEmployeeFirstName(empID); //ur code to get last name

// in ur webforms codebehind
EmpClass emp = new EmpClass();
lblFirstName.Text = emp.FirstName;
lblLastName.Text = emp.LastName;

Open in new window

You could pass the variable as a method parameter by reference and set the variable inside the function.

Carl TawnSystems and Integration DeveloperCommented:
You could use a Session variable, but it is a bit of a messy solution. Your other options are either a "ref" variable, or an "out" variable:
ALaRivaConnect With a Mentor Commented:
Would it not be easier to just use a getter instead of going the session route?
OTMAuthor Commented:
A "getter" was exactly what i needed, i just needed a good example to learn from and now i've implemented this into my project.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.