1

I have a struct that I created specific for a usercontrol. My thought was that I would have a public property Guid Dictionary<string, Guid> Attachments and then convert that to my private List<Attachment> attachments on the setter. I'm having trouble doing that, preferably with linq but I'm open to alternatives. Thank you...

private List<Attachment> attachments;
public struct Attachment
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}
public Dictionary<string, Guid> Attachments
{
    get { return attachments.ToDictionary(a => a.Name, a => a.Id); }
    set { attachments = new List<Attachment> // not sure what to do here }
}
bflemi3
  • 6,698
  • 20
  • 88
  • 155
  • 2
    Why are you exposing your `List` this way? Both your `get` and `set` will be very expensive for anything larger than a few elements, and it doesn't seem like you're gaining anything.. – Yuck Jan 26 '12 at 14:16
  • @Yuck I'm still learning, what would you suggest? My usercontrol needs the attachment id and name and possible more properties in the future. I suppose I could just go with a dictionary and not use a struct at all, but I'd like to hear your suggestions. Thanks Yuck – bflemi3 Jan 26 '12 at 14:18

2 Answers2

5

Assuming this is a valid design (I haven't really thought about it) I suspect you want:

attachments = value.Select(pair => new Attachment { Id = pair.Value,
                                                    Name = pair.Key })
                   .ToList();

I would strongly discourage you from using a mutable struct though. Using a struct isn't too bad in itself, but I'd change it to:

public struct Attachment
{
    private readonly Guid id;
    private readonly String name;

    public Guid Id { get { return id; } }
    public string Name { get { return name; } }

    public Attachment(Guid id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

... at which point the conversion is just:

attachments = value.Select(pair => new Attachment(pair.Value, pair.Key))
                   .ToList();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @JonSkeet I changed the key value pair to Guid, string. You're right that makes more sense. Thanks Jon – bflemi3 Jan 26 '12 at 14:22
  • @JonSkeet Would you mind explaining why this structure is better than the basic struct that I was using. Thank you – bflemi3 Jan 26 '12 at 14:25
  • 1
    @bflemi3: See http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil for a start. – Jon Skeet Jan 26 '12 at 14:30
  • @JonSkeet why not stick with auto properties and just make the setters private? – phoog Jan 26 '12 at 14:39
  • @phoog: I prefer to use genuinely read-only fields - it makes it clearer that it's really meant to be immutable, rather than "maybe mutable, but only from the inside". (I'd *love* C# to provide auto-properties with readonly fields, which could only be set from the constructor.) – Jon Skeet Jan 26 '12 at 14:45
  • @bflemi: See also [Choosing between Classes and Structures](http://msdn.microsoft.com/en-us/library/ms229017.aspx) on the MSDN. – Brian Jan 26 '12 at 15:56
3

I think you want:

attachments = value.Select(kvp => new Attachemnt { Id = kvp.Value, Name = kvp.Key })
                   .ToList();
Ani
  • 111,048
  • 26
  • 262
  • 307