You must Sign In to post a response.
  • Category: .NET

    how to access all the methods & variables of a class globally?

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    i want to access all the variables n methods of a class (say publicinfo)in other classes. what should be the return type & access modifiers of (publicinfo) that class.

    my problem in specific: i have a class CPersonalInfo & it has variables like name,sex,address,etc. i want to update these values in respective forms(i.e variable name accepted from the user in form1, variable sex is accepted in form2) i want to update all these values in the class(CPersonalInfo) when we traverse through the forms. so that i should b able to display the values of name & sex in form4.
    how to write code for this

    plz send ur answer in C# code.
  • #179
    Hi Shruthi,
    You can pass variables form one page to another using following methods.

    1. Using Session variables
    2. Using View state variables
    3. Request.Querystring
    4. Request.Form
    5. Using hidden variables

    <u>Samples</u>

    <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vctskcodepassingdatafromonewebformspagetoanothervisualc.asp" target="_new">
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vctskcodepassingdatafromonewebformspagetoanothervisualc.asp </a>
    <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vctskcodepassingdatafromonewebformspagetoanothervisualc.asp" target="_new">
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vctskcodepassingdatafromonewebformspagetoanothervisualc.asp</a>

    <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpassingservercontrolvaluesbetweenpages.asp" target="_New">
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpassingservercontrolvaluesbetweenpages.asp</a>

    <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k2/html/odc_passparam.asp" target="_new">
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k2/html/odc_passparam.asp</a>

    Thanks and Regards,
    J.Kasiviswanath

  • #180
    Hi,

    U can create the publicinfo class with private instance variables with public get and set methods or public properties for the private fields

    In your first form create instance of the publicinfo class,
    assign values to private fields through its properties
    create instance of next form by invoking constructor that accepts publicinfo as parameter
    load the second form
    In second form access the values set in first form thro this obj passed
    Pass this obj to third form and so on

    Code snippet

    person class
    <code>
    public class person
    {
    private string name;
    private int age;
    public person()
    {
    }
    public string sname
    {
    get
    {
    return name;
    }
    set
    {
    name=value;
    }
    }

    public int sage
    {
    get
    {
    return age;
    }
    set
    {
    age=value;
    }
    }

    }
    </code>

    Form1.cs button event code
    <code>
    private void btn_Click(object sender, System.EventArgs e)
    {
    person obj=new person();

    obj.sname=txtname.Text;
    Form2 f2=new Form2(obj);
    this.Hide();
    f2.Show();


    }
    </code>
    Form2.cs constructor and button event code

    <code>
    public class Form2 : System.Windows.Forms.Form
    {
    private person p1;
    - - -;
    //constructor that accepts person obj as parameter
    public Form2(person p)
    {

    InitializeComponent();
    p1=p;
    MessageBox.Show(p1.sname);

    }
    //btn event code
    private void btn_Click(object sender, System.EventArgs e)
    {


    p1.sage=int.Parse(txtage.Text);

    MessageBox.Show(p1.sname);
    MessageBox.Show(p1.sage.ToString());


    }
    }//end class
    </code>

    HTH

    Mohan

  • #184
    Hi shruthi,

    U can have one general class like what u r having now..
    CPersonnalInfo.cs...
    Create what are the Properties you want...

    <code>
    public class CPersonnalInfo
    {
    private string Name;
    private string Address;
    private int Age;
    private string sex;
    private int id;

    public int ID {
    get{
    return id;
    }
    set{
    id=value;
    }

    }
    public string Name
    {
    get
    {
    return name;
    }
    set
    {
    name=value;
    }
    }

    public string Address {
    get {
    return Address;
    }
    set {
    Address=value;
    }
    }

    public string Sex {
    get {
    return Sex;
    }
    set {
    Sex=value;
    }
    }

    public int Age{
    get {
    return Age;
    }
    set {
    Age=value;
    }
    }

    }

    </code>

    In your forms u can create an object for the class and initialise the ID value for the person
    and then assign values for the class properties, then based on the id value u can update the values in the class for the id value
    <code>
    int ID;
    PersonalInfo obj =new PersonalInfo(ID) ;
    obj.Name=txtName.Text;
    obj.Update();
    </code>

    this is same for all the forms.... and there u can update by calling the update routine by passing the idvalue...

    Any question please feel free to ask me....

    <b>Siva</b>

  • #185
    Hi
    shruthi

    If u want to access Method of the class without creating a object, then u have to use static function

    Ex :
    public class CTest
    {
    public static string TestMethod()
    {
    return "Test";

    }
    }

    u can call the function CTest. TestMethod();


    Regards
    Raghs

  • #187
    thanks for the pain taken.

    i could save the the values of different variables in different form

    once again thanx a lot :Kasiviswanath, Mohan, siva

  • #197896
    private accessspecigier used to to access the data globally.
    So you make which class you want that class mention private.
    Ex:private class my
    {
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.