-1

I want to inherit from MailAddress class to make a COM visible class, but MailAddress does not have a parameterless constructor, and COM doesn't have a mechanism to pass arguments to a constructor.

https://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress%28v=vs.110%29.aspx

so, i have to create a class like this, thinking about in just create it and later modify its properties:

public class Recipient : MailAddress
{
    public Recipient()
        : base("")//this is the contructor that takes less parameters, but can also add the other contructor parameters here
    {

    }
}

but then i realize that i can't modify its properties, because they are all read-only

anyone knows why they are readonly? and the class Attachment is made alike.

jrivam
  • 1,081
  • 7
  • 12
  • Immutability is a beautiful thing. (although I know this doesn't help you...) – spender Apr 30 '15 at 16:56
  • Where are you going to use this? Are you passing this to a method that requires a MailAddress? – Der Kommissar Apr 30 '15 at 16:58
  • 2
    You could expose a `MailAddressBuilder`, with an additional method `Build()` that returns a `MailAddress` (yes, this is the [Builder](http://en.wikipedia.org/wiki/Builder_pattern) pattern) – xanatos Apr 30 '15 at 17:04
  • Yes, builder pattern is the solution here. – Ben Voigt Apr 30 '15 at 17:05
  • @EBrown I want to use it in VB6 when i build the class with parameters public class Recipient : MailAddress { public Recipient(string address) : base(address) { } } VB6 shows me the message "invalid use of New keyword" in this sentence Dim oRecipient As New SendMailSmtp.Recipient And VB6 shows me the message "Expected: end of statement" when i try this Dim oRecipient As New SendMailSmtp.Recipient("address@domain.com") – jrivam Apr 30 '15 at 17:09
  • can I ask why the downvote? – jrivam Apr 30 '15 at 17:25

1 Answers1

2

The MailAddress class uses the immutable pattern -- so once the instance is created, it can't be modified (there are some benefits to this pattern -- since among other things it can help a lot with threading, since there are no lock contention issues).

The collection it gets placed into on the Message type (the to, from, cc collections) can be modified though. So you can always remove an existing MailAddress instance from one of those, and then create a new MailAddress class that replaces it with modified values.

Hope this helps,

BSG
  • 2,084
  • 14
  • 19
  • 2
    I think you missed the point of the question, which concerns *access from COM, which cannot pass parameters to a constructor* – Ben Voigt Apr 30 '15 at 17:05