Clone VB.Net Namespace / Structure (Involving Regex) - Complex

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2964 Views - Last Post: 01 November 2012 - 09:17 PM Rate Topic: -----

#1 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 11:03 AM

In short, I want to Override extensions of system types. I know that can't be done. I'm working with Regex, and this is a basic example of my goal:
Dim rgx as Match = Regex.Match("abcd", "a(.*)d")



What Happens is trivial. The Regex (Type/Structure) has function (Match) that builds a Match and returns it to rgx, which is declared of type Match. I wouldn't want to change that. What I DO want to happen is the equivalent of this:

CheckInput(strInput)
CheckRegex(strRegex)
Dim sw as New StopWatch
sw.Start
Dim tmpMatch as Match = Regex.Match(strInput, strRegex)
sw.Stop
GlobalMessage.Add(sw.elapsedmilliseconds)
GlobalMessage.Add(strCheckInput)
GlobalMessage.Add(strCheckRegex)
Return tmpMatch



That is just example code. But the point is, I want to override what happens when Match is called (and ALL regex functions). The best way I can imagine to do this is to create a wrapper/clone of the System.Text.RegularExpression Namespace under my own personal Namespace. That way, a user of this would Import MyRegexNamespace instead of System.Text.RegularExpressions. Everything would still work exactly the same, though, because when they call the "Regex.Match" in my namespace, my namespace really just does a bunch of stuff, and forwards the call to the System.Text.RegularExpression Namespace...

I tried and failed at this once and would like to know if there is some method of doing this already out there.

Is This A Good Question/Topic? 0
  • +

Replies To: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 11:34 AM

You could make a Public Class with Shared methods that use the regex from it's namespace, and only return the values you want. Either way you still have to import the namespace in your project somewhere - if that is what your where trying to avoid.
Was This Post Helpful? 0
  • +
  • -

#3 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:01 PM

I'd like to do:
'Imports System.Text.RegularExpressions
Imports MyRegexNamespace

Module TestClass
  Property rgx as Regex = New Regex("a(.*)d")
  Public Sub Main() As Match
    Dim strTest as String = rgx.Match("abcd").Value
  End Sub
End Module



And everything works the same whether either Import is commented out or the other.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:14 PM

Like this would make your code work.

Imports System.Text.RegularExpressions
Namespace MyRegexNamespace
  Public Class Regex
    Public Function Match(input As String) As String
      'return results from real regex methods
    End Function
  End Class
...more stuff
End Namespace

Was This Post Helpful? 1
  • +
  • -

#5 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:21 PM

Thanks. That's pretty much what I tried the first time. I created a clone of every property and method I could find for the RegularExpressions Namespace. The place where it failed was here:
'Imports System.Text.RegularExpressions
Imports MyRegexNamespace

Module TestClass
  Property rgx as Regex = New Regex("a(.*)d") '<--- Failure
  Public Sub Main() As Match
    Dim strTest as String = rgx.Match("abcd").Value
  End Sub
End Module



I create a Sub New and all I get is rgx = an instance of my Regex class. Sure I could make Sub New do every single thing Sub New does in the original RegularExpressions Namespace. Hell, I could probably rewrite literally the entire RegularExpressions namespace with enough time. But I was hoping I wouldn't have to do that.

Does anybody have any other idea of how I could override particular functions and otherwise pass-through everything else?

I realize I could make Regex Extensions that are named "xMatch" instead of "Match" or something. And that will be my final decision. But I'd rather to have users just be able to change their namespace or some reference instead of every line of code in their classes. Y'know?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:27 PM

I think your missing the point of the subs and functions in your namespace. You can do anything you want inside the sub - use the real regex or make them slightly different. It's your choice on what takes place once inside the methods. Inside the namespace classes and methods you initialize the New regex object pass it the parameters and therefore not needed to worry object the object creation from the calling code.
Was This Post Helpful? 0
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:29 PM

I think you'd want something like this:
Imports System.Text.RegularExpressions
Namespace MyRegexNamespace
    Public Class Regex
        Inherits System.Text.RegularExpressions.Regex
        Public Shadows Function Match(input As String) As String
            'return results from real regex methods  <-- forgot this from Hawk's example (and freaking strikethrough doesn't work in code ;)/>
        End Function
    End Class
End Namespace


This post has been edited by lucky3: 31 October 2012 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:40 PM

Right. And for most methods I just did a pass-through. So my Match function was like so:
Public Shared Function Match(ByVal input As String, ByVal regex as String) As System.Text.RegularExpressions.Match
    Return System.Text.RegularExpressions.Regex.Match(input, regex)
End Function



That's fine, and technically works (But fails because of this issue I'm having with my base New Regex class), So if I do this:
Dim rgx as new Regex("asdf")



The Microsoft Regex class I suppose has a Sub New() that takes the parameters and does a bunch of crazy stuff to build the properties of that Regex class then assigns that instance to rgx.

In MY Namespace, my Sub New does what?... Anything shy of re-writing the entire namespace will just make rgx an instance of my fail class.

Remember my goal here is to take a fully written class using Regex and just replace the System.Text.RegularExpressions Imports with my own namespace and still have everything work the same (But giving me power over additional functionality)

Edit to my response to Lucky
In my fail response, I said I didn't think you could inherit System.Text.RegularExpressions.Regex. But I just re-tried and you could. What I tried to inherit was System.Text.RegularExpressions. That obviously doesn't work. The reason I didn't try System.Text.RegularExpressions.Regex is because that doesn't solve my issue of Sub New Unfortunately.

This post has been edited by Suamere: 31 October 2012 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:49 PM

You can still define as many overloaded Sub New() for your namespace class as they did.

Imports System.Text.RegularExp<b></b>ressions
Namespace MyRegexNamespace
  Public Class Regex
    Sub New()
    End Sub

    Sub New(pattern As String)
      'set variables here
    End Sub

    Sub New(pattern As String, input As String)
      'set variables here
    End Sub

    Public Function Match(input As String) As String
      'return results from real regex methods
    End Function

  End Class
...more stuff
End Namespace

This post has been edited by _HAWK_: 31 October 2012 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#10 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:54 PM

You can overload constructors any way you want:
Imports System.Text.RegularExpressions
Namespace MyRegexNamespace
    Public Class Regex
        Inherits System.Text.RegularExpressions.Regex
        Public Sub New()

        End Sub

        Public Sub New(input As String)

        End Sub

        Public Sub New(input As String, regex As String)

        End Sub

        Public Shadows Function Match(input As String) As String
            'return results from real regex methods  <-- forgot this from Hawk's example (and freaking strikethrough doesn't work in code ;)/>
        End Function
    End Class
End Namespace


Was This Post Helpful? 0
  • +
  • -

#11 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 12:59 PM

View Post_HAWK_, on 31 October 2012 - 01:49 PM, said:

You can still define as many overloaded Sub New() for your namespace class as they did.

Imports System.Text.RegularExpressions
Namespace MyRegexNamespace
  Public Class Regex
    Sub New()
    End Sub

    Sub New(pattern As String)
      'set variables here
    End Sub

    Sub New(pattern As String, input As String)
      'set variables here
    End Sub

    Public Function Match(input As String) As String
      'return results from real regex methods
    End Function

  End Class
...more stuff
End Namespace


Yeah... that gets pretty complex if you imagine what I'd have to do within those constructors. A poo-load (technical term) happens within those in the RegularExpressions namespace, It appears by this logic that it wouldn't be worth it because I'd have to literally re-write the entire namespace.
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 01:03 PM

Echo Echo Echo

ROFL...........

I thought would may be only looking at a few common ones that you were wanting to use within your entire project to simplify and make some reuse code - not re-write the entire thing then that make what your doing impracticable.

This post has been edited by _HAWK_: 31 October 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 01:09 PM

Lol, sorta. This statement makes "logical" sense, but doesn't work:

I want to create my own namespace that uses the RegularExpressions namespace, but overwrites the Match and Matches methods of the RegularExpressions.Regex class so I can do what I want with them.

However, that statement doesn't work because you can't inherit the RegularExpressions namespace. You CAN inherit the Regex class and actually the Match and Matches methods ARE Overridable (Yay?). But then I have a personal Regex class with new RegularExpressions namespace and I get into an ugly loop that never works.

So no, I don't need to re-write the whole thing, I just need to override a couple methods. But in order to do so, it appears I'd have to re-write the whole thing. Do ... Loop. So ya, this won't work.
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2250
  • View blog
  • Posts: 9,432
  • Joined: 29-May 08

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 31 October 2012 - 01:33 PM

Why do you want to do this, in the first place? What problem are trying solve?
Was This Post Helpful? 0
  • +
  • -

#15 Suamere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-August 09

Re: Clone VB.Net Namespace / Structure (Involving Regex) - Complex

Posted 01 November 2012 - 07:57 PM

Adam, thanks for your interest in this thread.

I'm pretty good with VB and took a bit of time earlier this year to master Regex. The company I'm contracted to has tasked me with evaluating and re-writing all Regex statements in all of their hundreds of libraries to work faster. Of course, just the ones that really need it.

I happen to have built a class a few years back (and well-tested and improved since) that evaluates Regex statements by changing the call Regex.Match(...) to RGX.Match(...). It takes the input, the regex, and the options it is passed and evaluates them for weaknesses. And if they receive a low score, I go re-write it. This is easy for 10-100 lines of code for a whole project, but for hundreds of projects each with hundreds of lines of code, Ew...

So I'd like to override the VB Regex Class functions so I can implement the tools I built.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2