Inheriting Regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have built a class called DTLineScrub that inherits Regex.
All works well with the code below until I try to do a replace on a string.
It then tells me that the object was not instantiated.

Anyone have an Idea what's going on?

Much Appreciated!

Rob

string myTEST = "ABE'\"TH|IS";

DTLineScrub dtls = new DTLineScrub();
dtls.CustomRegularExpression = @"[\cA-cZ]";

MessageBox.Show( dtls.ToString() );

myTEST = dtls.Replace(myTEST," ");

MessageBox.Show( myTEST );
 
Rob said:
I have built a class called DTLineScrub that inherits Regex.
All works well with the code below until I try to do a replace on a string.
It then tells me that the object was not instantiated.

Anyone have an Idea what's going on?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks Jon for looking at this for me. I have created a console app that will
show the issue in total. If you have any questions, please feel free to
contact me directly at (e-mail address removed).

You can just slap this into an empty page and it will compile and run to the
error.

Cheers!

Rob

--------------------------------- CODE
-----------------------------------------------------

/* This code results in the following error:
* An unhandled exception of type 'System.NullReferenceException' occurred
in system.dll
* Additional information: Object reference not set to an instance of an
object.*/

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace SampleCode
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
//creation of the regularexpression object
static regx lineScrub = new regx();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Create the line to be scrubbed
string line = "This pipe| should be taken out and so should this quote.\"";

//Action of the object - error occurs on this line
line = lineScrub.Replace(line, " ");

//Never gets here.
Console.WriteLine( line );
}
}

//regularexpression object created that inherits the
System.Text.RegularExpressions.Regex object.
class regx : Regex
{
public regx()
{
this.pattern = "\\\"\\|";
}
}
}

-------------------------------- End Code
 
Right;

First - your regex is wrong; it currently says "match quote followed by
pipe".

Second - the protected .pattern *tells* you (intellisense) that it is
intended for use in CompileToAssembly mode - which you aren't using, so it
doesn't apply; you need to use the ctor usage; I changed this to:

class regx : Regex {
public regx() : base(@"[\""\|]") {}
}

Third - I'd challenge the need to subclass here; it would be far less
complex to just declare

Regex lineScrub = new Regex(@"[\""\|]");

(Perhaps stored somewhere handy with compile-to-assembly if it is going to
be re-used much)

Marc
 
Note - going back to your original issue... if I really wanted this type of
API, I would wrap a regex instead... see below... but to be honest, Regex et
al are so common that (without siginificant extra functionity) I'd need
convincing to use it myself...

public class MyWrapper {
private Regex regex;
private string exp;
public string Pattern {
get {return exp;}
set {if(exp!=value) {
// do this first so if it barfs we haven't changed exp
// (and maybe compile too...)
regex = new Regex(value);
exp = value;
}}
public blah Replace(blah) {}
public blah IsMatch(blah) {}
// blah
}
 
Thanks Marc,

The purpose of the class is to be able to add properties that would automate
selections of common regular expressions without the developer needing write
the expressions directly. The idea was to inherit the Regex object into the
class and add the properties to change the pattern.

I see that the only way to do this, is to set the class to a new Regex for
each new addition to the expression. Knowing now that the expression must be
passed in at the time the Regex Object is instantiated tells me that I need
to go the rout you noted in this last reply.

Thanks.

Rob
 
I must admit though, the Regex class was /very/ misleading here...

In the MSDN docs, it claims to be immutable (although it doesn't carry the
immutable attribute) - and as you found, it exposes these protected fields
[aside: why fields? eugh], which makes it look as though you are allowed to
manipulate it in this way (although MSDN does report these fields as not
intended for user consumption). A very misleading implementation, so your
attempted route was perfectly reasonable in the "it looks like it should
work" sense.

I think you just got bit by Redmond ;-p

Best of luck with your wrapper,

Marc
 

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