Need to inherit from System.String (I think), but it's sealed

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

Ok, what I'd like to do is create what I'll call an AdvancedString, which is
just a string with a couple of extensions. Let's say for example, I'd like
have an IsValid boolean property which will work in conjuction with setting
the Required boolean property and return false if the string is empty.

I'd like to be able to use this new type as a property in a class that could
be bound to in a WinForm, like I could with a regular string.

But...
- System.String is sealed
- Ok, let's try composition. But there doesn't seem to be a concept of a
default property other than an indexer, or a way to explicitly write my own
= operator.

So how do I make a class I could use like a string in a property - something
like:

class MyClass
{
private AdvancedString _lastName = new AdvanceString(true); // specifies
empty value is invalid, for example

public AdvancedString LastName
{
get { return _lastName; }
set { _lastName = value; }
}

----
MyClass dude = new MyClass();
dude.LastName = "Smith";
if (dude.LastName.IsValid)
{
// do something
}
 
Ok, I've mostly answered my own question.

I added conversion operators to and from string, and to override ToString
and now I can use AdvancedString like a string.

Moving on to testing the binding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top