Reputation: 1039
I've never used structs before. I've created a simple example of what I'm trying to do below. The reason I've chosen struts is because the object will never need to exist outside the context of the class. Thanks.
Classes
public class EmailAddress
{
public string Email { get; set; }
public string Name { get; set; }
}
public class EmailMessage
{
public EmailAddress To { get; set; }
public EmailAddress From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public struct Attachment
{
public string Name { get; set; }
public string Bas64 { get; set; }
}
Attempted Method
protected void MyMethod()
{
var myEmailMessage = new EmailMessage
{
To = { Email = "ToEmailAddress" },
From = { Email = "FromEmailAddress" }
};
var myAttachment = new EmailMessage.Attachment
{
Name = "AttachmentName",
Bas64 = "Base64String"
};
myEmailMessage.Attachment = myAttachment;
}
Upvotes: 1
Views: 57
Reputation: 292425
Your EmailMessage
class doesn't have an Attachment
property, only an Attachment
nested type.
You should rename the Attachment
struct to EmailAttachment
to avoid name conflicts, and create an Attachment
property:
public class EmailMessage
{
public EmailAddress To { get; set; }
public EmailAddress From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public EmailAttachment Attachment { get; set; }
public struct EmailAttachment
{
public string Name { get; set; }
public string Bas64 { get; set; }
}
}
Upvotes: 1
Reputation: 27599
Your problem is that myEmailMessage.Attachment = my Attachment;
is attempting to assign something to a member called Attachment
. The problem is that you don't have a property called Attachment
on your class. You have declared a struct
called that but that is just a declaration and doesn't create a property or anything else like that.
What you'd need is to have an actual property on your class that you could then assign an instance of Attachment
to.
Upvotes: 1
Reputation: 156978
You can't use a struct
declaration as property of your class. You should split the property and the actual struct
definition.
Try this:
public _Attachment Attachment {get;set;} /* Attachment as property */
public struct _Attachment /* The definition of the struct */
{
public string Name { get; set; }
public string Bas64 { get; set; }
}
And use it like this:
var myAttachment = new EmailMessage._Attachment
{
...
}
By the way: there is no real need for a struct
. Using a class
would be fine too.
Upvotes: 4