Help please with extending properties

  • Thread starter Thread starter NewGuy
  • Start date Start date
N

NewGuy

I have this parameter:
public string Title
{
get{return m_sTitle;}
set{m_sTitle = value;}
}


I want to do this:
Object foo = new Object;
foo.Title.IsRequired();
 
NewGuy said:
I have this parameter:
public string Title
{
get{return m_sTitle;}
set{m_sTitle = value;}
}


I want to do this:
Object foo = new Object;
foo.Title.IsRequired();

You can't do that for two reasons:

1) The Object type doesn't have a Title property
2) The String type doesn't have an IsRequired method

You could create your own type which encapsulates a string and whether
or not it's required to get round 2) if you wanted, but we'd really
need to know more information to know whether or not it was really
appropriate.
 
Hi NewGuy,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to extend a property with
required checking. If there is any misunderstanding, please feel free to
let me know.

If you need to extend the property, you need to make m_sTitle another type.
Currently, it is a string type, which does not support IsRequired method.
You have to define your own method in a user defined class. It can be a
subclass inherited from String class. Or just contains string member. Here
is an example:


class title
{
public title()
{
}
public string m_title;
public bool IsRequired()
{
return true;
}
}

class FOO
{
public FOO()
{

}
private title m_sTitle = new title();
public title Title
{
get{return m_sTitle;}
set{m_sTitle = value;}
}

}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Kevin Yu said:
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to extend a property with
required checking. If there is any misunderstanding, please feel free to
let me know.

If you need to extend the property, you need to make m_sTitle another type.
Currently, it is a string type, which does not support IsRequired method.
You have to define your own method in a user defined class. It can be a
subclass inherited from String class.

No, String is sealed - you can't subclass it. Composition is required
instead.
 
Thanks Jon, for pointing me out. Didn't check it before posting.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Here a sample of the code I would like to write.

public class User
{
private string m_sFoo;
public string Foo
{
get{return m_sFoo;}
set{m_sFoo = value;}
}
}


private class Editor
{
User user = new User();

if (user.Foo.IsRequired())
{ user.Foo = "My Value"; }
else
{ 'do something else' }
}

I have tried adding a new class as Kevin pointed out, the problem is that I
now have a new object and I can't get or set it like a normal string.
 
Back
Top