Instance Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inadsad@hotmail.com

    #1

    Instance Variables

    Good Day Group,
    Recently I started a small test project in C# VS 05 for learning
    purposes. I have a class and a form. I defined a property in a class
    with get/set and create an instance of class1 on form.

    The part I'm confused at is when trying to access Name property
    outside the click event it wouldn't work. Why is that? Should I
    create an instance after the InitializeCompo nent? and move all
    constants & variables to class1. I'd appreciate some feedback.

    Thanks
    Ian

    namespace WindowsApplicat ion1
    {
    public partial class Form1 : Form
    {
    Class1 Testc = new Class1(); // create a new instance
    string sCompany = "";
    const string sDATABASE;
    const string sUSERID;


    Testc.???? //>cann't access property Name here
    public Form1()
    {
    InitializeCompo nent();
    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    Testc.Name = "abc"; //>can access property Name here
    }
    }

    class Class1
    {
    private String name;
    public String Name
    {
    get
    {
    return _name;
    }

    set
    {
    name = value;
    }
    }

    }
    }
  • Peter Duniho

    #2
    Re: Instance Variables

    On Tue, 18 Dec 2007 16:51:17 -0800, <inadsad@hotmai l.comwrote:
    [...]
    public partial class Form1 : Form
    {
    Class1 Testc = new Class1(); // create a new instance
    string sCompany = "";
    const string sDATABASE;
    const string sUSERID;
    >
    >
    Testc.???? //>cann't access property Name here
    ...
    }
    And what exactly did you expect to be able to do there?

    Any code statements in your class has to go inside an actual code block.
    That would be inside method declarations, whether for the class itself,
    getters and setters and indexers, anonymous methods assigned to class
    members, etc. You can't just stick some code anywhere you like. Outside
    of code blocks, all you can have are declarations and optionally some
    initializer for the thing being declared (if applicable.

    You can still initialize the instance variable at the declaration. But if
    you want to do something with the property, you have to do that somewhere
    that you're allowed to write non-declarative code (the constructor would
    be a natural place to do that).

    Pete

    Comment

    • bryan

      #3
      Re: Instance Variables

      In other words :

      namespace WindowsApplicat ion1
      {
      public partial class Form1 : Form
      {
      Class1 Testc = new Class1(); // create a new instance
      string sCompany = "";
      const string sDATABASE;
      const string sUSERID;


      //Testc.???? //>cann't access property Name here
      public Form1()
      {
      Testc.Name = "unknown";// you CAN set it here...
      InitializeCompo nent();
      }

      private void button1_Click(o bject sender, EventArgs e)
      {
      Testc.Name = "abc"; //>can access property Name here
      }
      }

      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.t3j4zyx i8jd0ej@petes-computer.local. ..
      On Tue, 18 Dec 2007 16:51:17 -0800, <inadsad@hotmai l.comwrote:
      [...]
      public partial class Form1 : Form
      {
      Class1 Testc = new Class1(); // create a new instance
      string sCompany = "";
      const string sDATABASE;
      const string sUSERID;
      >
      >
      Testc.???? //>cann't access property Name here
      ...
      }
      And what exactly did you expect to be able to do there?

      Any code statements in your class has to go inside an actual code block.
      That would be inside method declarations, whether for the class itself,
      getters and setters and indexers, anonymous methods assigned to class
      members, etc. You can't just stick some code anywhere you like. Outside
      of code blocks, all you can have are declarations and optionally some
      initializer for the thing being declared (if applicable.

      You can still initialize the instance variable at the declaration. But if
      you want to do something with the property, you have to do that somewhere
      that you're allowed to write non-declarative code (the constructor would
      be a natural place to do that).

      Pete


      Comment

      Working...