Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am developing Windows Form application :


i have declaire one public variable in my Form...

but i want to access that variable's value in class..

Thanx in advance.
Posted
Updated 18-Sep-14 20:04pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Sep-14 1:19am    
Well, then stop development of Windows Form application. Stop it all and start reading some language and .NET manual, some general programming topics. Start with elementary problems, perhaps, writing some simple console-only applications as exercises. Seriously, everything else will be just a waste of time, including reading any answers you might get — it cannot help you.
—SA
George Jonsson 19-Sep-14 1:34am    
Sad but true
Sergey Alexandrovich Kryukov 19-Sep-14 3:16am    
:-( :-( :-( (This is the link; please see :-)
But actually this is not so sad. Take a break and learn, isn't that a good thing?
—SA
George Jonsson 19-Sep-14 3:21am    
Hehe
BillWoodruff 19-Sep-14 1:25am    
How about showing us the code (not too much) you are working with ? Where is the definition of the Class in which you want to access the value ? Where are instances of that Class created in your project ?

A public variable, say myvar, is accessible if you can access an instance of the form, say Form1, this way:
C#
Form1.myvar

That said, it is not a very good idea exposing member variables such a way. As other suggested, you should study a good book on Object Oriented Programming (and possibly on C#)
 
Share this answer
 
Comments
BillWoodruff 19-Sep-14 3:35am    
Hi, I'm not down-voting your answer, but I do think this is a confusing answer, particularly for a newcomer who is obviously not "at home" in .NET, and may be relatively new to the whole idea of semantic scope in OOP.
CPallini 19-Sep-14 6:49am    
Thank you for your comment. Don't be afraid to downvote my answer if you think it is not helpful.
using System;
using System.Windows.Forms;

class form1:Form{
public  string variablename=""; // here you are declaring a variable 
//

here your code

//
}
First Create a object of that form class and using that object  you can call a variable

Form1 form=new Form1();
string str=form.variablename;     // here we can call that variable  using object of that class and store it into another variable
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900