-2
//class MessageModel
public class MessageModel
{
    public string Name{get;set;}
    public BindingList<AttachDocument> _attachDocument {get;set;};
}
// class AttachDocument that it's object is a MessageModel property
public class AttachDocument
{
    public string AttachName { get; set; }
    public bool IsCheck { get; set; }
}

MessageModel source = new MessageModel { AttachDocument = new BindingList<AttachDocument>()};
MessageModel value = new MessageModel { AttachDocument = new BindingList<AttachDocument>()};
// Error "AttachName" is not defined
source.AttachDocument.AttachName = value.AttachDocument.AttachName; // What I want to access

So how can I access access object's property in inside other object's property and assign a value.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
Benni To
  • 47
  • 6

3 Answers3

2

There is no property for AttachDocument in your class, only a member variable. That needs to be wrapped in a get/set first.

Then the AttachDocument property is a list of AttachDocument instances, not a single one so the problem you have is that AttachName isn’t a property on the list.

This is how you can use indexers to get an element by index

Scott Perham
  • 2,410
  • 1
  • 10
  • 20
2

I assume you want this:

MessageModel source = new MessageModel { AttachDocument = new BindingList<AttachDocument>()};
source._attachDocument[myIndex].AttachName = "Name";

However this assumes you already created an instance of AttachDocument and assigned added it into the _attachDocument-list of your source:

MessageModel source = new MessageModel { _attachDocument = new BindingList<AttachDocument>()};
source._attachDocument.Add(myDocument);

As an aside _attachDocument is a bad name for a public property. You should definitly follow the naming-conventions. So change _attachDocument to AttachDocument. A property can have the same name as the class of its type. However in your case the property is a list of documents, so the best name would be AttachDocuments with "s" at the end. This makes it clear that the property is some kind of collection which needs an index to be accessed.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

There is no property with the name AttachDocument. It is a class that is unrelated to the field you are having defined. There are a few issues you need to address:

  • First, you need to make the field public, to make it accessible outside of your class;
  • Second, you need to make it a property to follow conventions, and help binding it in WPF for example;
  • Third, the list should be addressed by index, but only after you have added an item to it.

Since you want to iterate over the list, you need something like a foreach:

public class MessageModel
{
    public string Name{get;set;}
    public BindingList<AttachDocument> AttachDocuments {get;set;}
}

foreach (AttachDocument s in source.AttachDocuments)
{
    AttachDocument t = new AttachDocument();
    t.AttachName = s.AttachName;

    value.AttachDocuments.Add(t);
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325