Properties / Getters n setters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Properties / Getters n setters

  1. #1
    Join Date
    Jun 2006
    Posts
    130

    Properties / Getters n setters

    How do I use properties in C# (Im used to getters and setters in Java). I just want an attribute that Im able to set/get. Is there any ways to autmatically make them thread safe?

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Properties / Getters n setters

    You use them the same way as in Java, with the exception that the syntax is like working with fields rather than methods - you use = and no () to access them.

    Code:
    public class Foo {
     private stirng name;
    
     // C# properties style
     public string Name {
      get {return this.name;}
      set {this.name = value;}
     }
    
     // Java style (yes, you can use it too, but any C# programmer will be confused
     public string getName() {
      return this.name;
     }
    
     public void setString(string name) {
      this.name = name;
     }
    
      // ... and now how to use them
      public void FooFoo() {
       // Instead Java's style
       this.setName("Hugo");
       string _name = this.getName();
      
      // you would use
      this.Name  = "Hugo";
      string _name2 = this.Name;
      }
    }
    Thread safe code can be achieved using lock keyword
    Code:
    public class Foo {
     private stirng name;
    
     public string Name {
      get 
      {
        lock (this) 
       {
          return this.name;
        }
      }
      set 
      {
        lock (this) 
       {
         this.name - value;
        }
      }
    }
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Mar 2004
    Location
    3311'18.10"N 9645'20.28"W
    Posts
    1,808

    Re: Properties / Getters n setters

    attributes are something different in .net. java attributes or .net properties have get and set blocks w/ out parameters inside the property block like so:

    Code:
    private string foo;
    public string Foo {
        get { 
            return foo;
        }
        set {
            foo = value;
        }
    }
    where the body of your method goes inside the get / set block. the value that would be passed in to your set method is named "value".

    you need a get or set but not both.
    Code:
    private string foo;
    public string Foo {
        get { 
            return foo;
        }
    }
    in 2.0 you can declare access modifier
    Code:
    private string foo;
    public string Foo {
        get { 
            return foo;
        }
        protected set {
            foo = value;
        }
    }
    to make them thread safe (atleast in writing to the data):

    Code:
    object fooLock = new object();
    private string foo;
    public string Foo {
        get { 
            return foo;
        }
        set {
            lock(fooLock) {
                foo = value;
            }
        }
    }

  4. #4
    Join Date
    Jun 2006
    Posts
    130

    Re: Properties / Getters n setters

    Thanks. Is there any way of locking on an integer or something like that? Of course I can encapsulate my variables in some sort of class, but it would be nice to be able to lock regular variables as well.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Properties / Getters n setters

    No, lock works only on reference objects. You need extra object (instance of Object itself is enought) on which the lock can work.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center