.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Get & Set Property

Posted By: Savi3     Posted Date: August 26, 2008    Points:2   Category :C#
Tell me about get and set property wit example..
Because i need the username for the next form also in C#.net

Responses
Author: Pankaj Mishra             
Posted Date: August 26, 2008     Points: 5   

Hi,
we declare a data field as private and will provide a set of public SET and GET modifier to access the data fields. Its good programming practice, because the data fields are not directly accessible out side the class. We have to use the GET and SET methods to access the data fields this is called properties in .NET.

eg

class SomeClass
{
private string m_UserName;
public string UserName
{
get
{
return m_UserName;
}
set
{
m_UserName = value;
}
}
}



Hope this will help you

Regards
Pankaj

Author: Savi3             
Posted Date: August 26, 2008     Points: 5   

Ya, I try this in a seperate class. But i dont know how to call this for my class. Since it showsAn unhandled exception of type 'System.StackOverflowException' as error..

I give like this

string UserName;
Members ObjMembers = new Members();
UserName = txtlogusername.Text;
ObjMembers.username1 = UserName;



where Members is the class using set & get property

Author: Pankaj Mishra             
Posted Date: August 26, 2008     Points: 5   

Hi,
So what i understood from your question is you want to pass username to a different form using Property if it is so then try like this

Form1 is the form from where you want to pass UserName to Form2 then try like this

In Form2 expose one property UserName by usinng above post code(No need for seperate class)


Now while calling Form2 from Form1 on some event Try like this


Form2 objForm2 = new Form2()
objForm2.UserName = txtlogusername.Text; //UserName Is property in Form2

objForm2.Show(); //to show From2

this.hide(); //to hide Form1


Hope this will help you

Cheers
Pankaj

Author: Savi3             
Posted Date: August 27, 2008     Points: 5   

Ya, I try this in a seperate class. But i dont know how to call this for my class. Since it showsAn unhandled exception of type 'System.StackOverflowException' as error..

I give like this

string UserName;
Members ObjMembers = new Members();
UserName = txtlogusername.Text;
ObjMembers.username1 = UserName;



where Members is the class using set & get property

Author: Pankaj Mishra             
Posted Date: August 27, 2008     Points: 5   

Hi,

Did you tried the above code by exposing property in calling page(Form2)? Or you want to expose property in seperate class?

Cheers
Pankaj

Author: Savi3             
Posted Date: August 27, 2008     Points: 5   

Ya, I try this in a seperate class. But i dont know how to call this for my class. Since it showsAn unhandled exception of type 'System.StackOverflowException' as error..

I give like this

string UserName;
Members ObjMembers = new Members();
UserName = txtlogusername.Text;
ObjMembers.username1 = UserName;



where Members is the class using set & get property

Author: Pankaj Mishra             
Posted Date: August 27, 2008     Points: 5   

Hi,
if you want to expose in seperate class the try like this

Create property

public class Members
{
private static string m_UserName = string.Empty;
public static string UserName
{
get
{
return m_UserName;
}
set
{
m_UserName = value;
}
}
}



Now use this property in login form in some Button Click event like this


Members.UserName = txtlogusername.Text;


Now you can use this anywhere in you application by calling your property say in Form2 you want to retrive UserName

then try like this


string _userName = Members.UserName;


Hope this will help you

Cheers
Pankaj

Author: Savi3             
Posted Date: August 27, 2008     Points: 5   

Sorry For delay..

I use seperate class for all data feilds, So in Members i give

private string username;
public string username1
{
get { return username; }
set { username=value; }
}


In login Form

while i give ok button click

Members Objmembers = new Members();
string UserName;
Objmembers.username1 = txtlogusername.Text;
Welcome w = new Welcome();
w.Show();



I need this user name as a label in the Welcome Form, So i open Welcome Form here

But i want to create a new form in that i want to display username


Am trying for this only
I think u got my question....

Author: Savi3             
Posted Date: August 27, 2008     Points: 5   

Sorry For delay..

I use seperate class for all data feilds, So in Members i give

private string username;
public string username1
{
get { return username; }
set { username=value; }
}


In login Form

while i give ok button click

Members Objmembers = new Members();
string UserName;
Objmembers.username1 = txtlogusername.Text;
Welcome w = new Welcome();
w.Show();



I need this user name as a label in the Welcome Form, So i open Welcome Form here

But i want to create a new form in that i want to display username


Am trying for this only
I think u got my question....

Author: Pankaj Mishra             
Posted Date: August 27, 2008     Points: 5   

Hi,
Now in your Welcome form in Form_Load event

try kike this


lableName.Text = Members.UserName1;


Make sure that your Member Class userName Property is Static as i have given in previous example.Its checked code

Cheers
Pankaj



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend