Saturday, December 04, 2004

StringControl

StringControl, something I wrote last night to handle parsing of text between tags. I'm not too sure whether there's another easier way to do it, but here's what I came up with.

public class StringControl : Control {
  private string m_text;

  public StringControl() {
  }

  public StringControl(string text) {
    m_text = text;
  }

  public string Text {
    get { return m_text; }
    set { m_text = value; }
  }

  protected override void AddParsedSubObject(object obj) {
    if ( obj is LiteralControl ) {
      m_text = ((LiteralControl)obj).Text;
    }
  }
}

Why did I create a simple control like this? It's basically to handle this situation easily.

<sgdn:ModuleItem href="testhref" forumref="testforumref">
  <heading>Testing Heading</heading>
  <text>Testing Text</text>
</sgdn:ModuleItem>

Notice if I wanted to do that way, I needed to create a control to parse the text between <heading> and <text>. The code is something like this.

[ NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty) ]
public HeadingControl Heading {
  get { return new HeadingControl(m_heading); }
  set { m_heading = value.Text; }
}

[ NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty) ]
public TextControl Text {
  get { return new TextControl(m_text); }
  set { m_text = value.Text; }
}

So basically I consolidated the logic for both HeadingControl and TextControl into 1 class called StringControl which will parse the inner text of the tags.

Fantastic don't you think? But I'd prefer if there's a better way to do.

No comments: