4 Replies - 1169 Views - Last Post: 13 September 2012 - 10:49 AM Rate Topic: -----

#1 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

How can I extend an existing class?

Posted 12 September 2012 - 10:53 PM

I have been looking for a way to extend an existing class such that I can call new methods in the same manner that I call methods in the existing class. I thought Inherits or Interface might be what I'm looking for, but I don't think they'll do what I want.

Perhaps an example would explain...

Suppose I want to add a method to the String class. Let's call it Twist. I want to be able to do something like this...
  dim Foo as String
  dim Bar as String
  Bar = Foo.Twist(12)


I get the feeling that this is something that folks do a lot, but that I have just not been phrasing my search strings properly.

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: How can I extend an existing class?

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: How can I extend an existing class?

Posted 13 September 2012 - 02:02 AM

You might want to take a look at Extension Methods.
Was This Post Helpful? 1
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How can I extend an existing class?

Posted 13 September 2012 - 06:24 AM

<slappong forehead>!

Thanks a million, lucky3. That's exactly what I was looking for, but never thought to look for "extension methods" insteand of "extend a ...".
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: How can I extend an existing class?

Posted 13 September 2012 - 08:17 AM

I do sometimes things like that. For example today, I was "fighting" with one single line of code with Regular Expression in it. It's almost embatasing, really, but I'll show what I was strugling with:
            Dim linkInCode = Regex.Matches(codeText, "\<a(.*?)\/a\>")
            For Each linkFound In linkInCode 
                Dim changeMe As String = linkFound.value
                Dim linkPrepared As String = Regex.Match(changeMe, "\>(?<value>(.)*?\<)").ToString
                linkPrepared = linkPrepared.Substring(1, linkPrepared.Length - 2)
                codeText = Regex.Replace(codeText, changeMe, linkPrepared).ToString
            Next



I wanted to replace each <a href... /a> into plain text, and results were not good. changeMe was full of /(=, and similar characters, that must be escaped, when you're using them as Regex pattern.

I was digging in Object Browser, searching for Regex Pattern, Pattern Escape, Regex Match, Regex Replace and I don't know what else. I searched with Google, and found nothing...

I almost started to code my own function to clear and prepare regex pattern, and was thinking how stupid would it be, if there is no such method in Regex class, and I couldn't be the first one who needs this.

Then I finally looked Regex class, and all it's methods, and I found it:
codeText = Regex.Replace(codeText, Regex.Escape(changeMe), linkPrepared).ToString did it's job. :^:
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How can I extend an existing class?

Posted 13 September 2012 - 10:49 AM

Hmmm... not sure if this is what you are trying to do, but...

I recently needed to get links to images from a web page, in string form, and did it the following way....

Public Sub getImagesFromPage(ByVal page As HtmlDocument, ByVal basepage As String)
  Dim imgURLs(10) As String
  Dim theElementCollection As HtmlElementCollection = page.GetElementsByTagName("img")
  For i = 0 To 9
    If settings(i).urlsearch <> "" Then
      For Each curElement As HtmlElement In theElementCollection
        Dim imgURL As String = curElement.GetAttribute("src").ToString
        If imgURL.IndexOf(settings(i).urlsearch) > -1 Then
          Select Case settings(i).item
            Case "pbox_oval"
               pbox_Oval.ImageLocation = imgURL
               ToolTip1.SetToolTip(Me.pbox_Oval, "Updated at " & Now)
            Case "pbox_kp"
              pbox_Kp.ImageLocation = imgURL
              ToolTip1.SetToolTip(Me.pbox_Kp, "Updated at " & Now)
'many more cases...
          End Select
        End If
      Next
    End If
  Next
End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1