Solved

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

Posted on 2011-03-18
5
583 Views
Last Modified: 2012-05-11
Hi,

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.

Thanks,

Sam
0
Comment
Question by:OTM
5 Comments
 
LVL 33

Expert Comment

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

http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

-saige-
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35167515
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:

    http://www.c-sharpcorner.com/UploadFile/mahesh/out_and_ref11112005002102AM/out_and_ref.aspx
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 50 total points
ID: 35167794
Would it not be easier to just use a getter instead of going the session route?
0
 
LVL 4

Accepted Solution

by:
abhinayp earned 450 total points
ID: 35168222
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();
emp.EmployeeGet(id);
lblFirstName.Text = emp.FirstName;
lblLastName.Text = emp.LastName;

Open in new window

0
 

Author Closing Comment

by:OTM
ID: 35169443
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.

Thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question