tags:

views:

272

answers:

5

Not sure if I worded this correctly ... but I have the following code:

    public Guid ItemId
    {
        get;
    }

    public TransactionItem()
    {
        this.ItemId = Guid.Empty;
    }

Naturally I am getting a read-only issue ... which I do understand. Is there anyway to set this property value without having to do something like the below:

    Guid _itemId = Guid.Empty;
    public Guid ItemId
    {
        get
        {
            return _itemId;
        }
        set
        {
            _itemId = value;
        }
    }

or

    public Guid ItemId
    {
        get;
        internal set;
    }

Thanks in advance!

+2  A: 

You can use readonly keyword

public readonly Guid ItemId;

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}
Jakub Šturc
@Jakub, Making your fields non-private is something you shouldn't do unless you *absolutely* need to: http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx
LukeH
+1  A: 

I'm afraid your question isn't very clear - but if there isn't a property setter defined, then you certainly can't call it.

Are you actually after an automatically implemented read-only property, allowing setting just within the constructor? If so, I'm afraid that's not available, much as I'd like it.

Just to expand on what I mean, I'd like to be able to do:

// Not valid in C# - yet!
public class Foo
{
    // Autogenerated field would be readonly in IL.
    public string Name { get; readonly set; }

    public Foo (string name)
    {
        this.Name = name;
    }

    public void Bar()
    {
        // This would be invalid
        this.Name = "No!";
    }
}

Basically it would be "make a property like a readonly field."

Jon Skeet
I didn't think I worded it correctly ... but what you are saying is correct. Just trying to save some typing ... and was curious on top of it.
mattruma
No problem. I've edit the answer to show what I'd like to be able to do - I believe it's what you want too.
Jon Skeet
A: 

Use a private setter, or set the backing field ?
(But, then you must make sure that the name of your backing field can be determined based on the property-name.
For instance by making sure that your backing field alwas has the same name as the property-name , but is prefixed with an underscore; like NHibernate does it using its access-strategies).

Frederik Gheysels
+7  A: 

I would go for this:

public Guid ItemId
{
    get;
    private set;
}

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}

Of course, it would be open for setting within this class, but since you are writing it I hope you have the sense to not break your own intentions...

In my opinion, things like readonly properties, are mostly important when seen from the outside. From the inside, it doesn't really matter what it is, cause there, you are the King =)

Svish
+1 a lot of people don't realise props can have different scopes
annakata
This looks like the way I am going to go! Thank you! Was more or less curious to see if it could be done without having a "set".
mattruma
opening the door of a house without a door is pretty difficult... :p
Svish
+1  A: 

If you just need the ItemId property to be read-only for external users of your class, then Svish's answer is the way to go.

If you need ItemId to be read-only within the class itself then you'll need to do something like this:

private readonly Guid _ItemId
public Guid ItemId
{
    get { return _ItemId; }
}

public TransactionItem()
{
    _ItemId = Guid.Empty;
}
LukeH