Nasenbaer
Nasenbaer

Reputation: 4900

Extension in WP7

Imports System.Runtime.CompilerServices

Public Module ColorExtension
    <Extension()> _
    Public Function ToColor(ByVal argb As UInteger) As Global.System.Windows.Media.Color
        Return Global.System.Windows.Media.Color.FromArgb(CByte((argb And -16777216) >> &H18), CByte((argb And &HFF0000) >> &H10), CByte((argb And &HFF00) >> 8), CByte(argb And &HFF))
    End Function
End Module



Public Class Test
    Private Sub TestExt()
        Dim Col As System.Windows.Media.Color
        Col = System.Windows.Media.Color.ToColor(100)'<-- Error
        Col.ToColor(100)'<-- Error
    End Sub
End Class

When I use this code, I got this exception Error 1 'ToColor' is not a member of 'System.Windows.Media.Color'. C:...\ColorExtension.vb

Please any advice how to develop Extension functions like this Color extension one.

Upvotes: 0

Views: 47

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

  1. Extension methods can be fired on object of class, not the class itself.
  2. Your extension method is set on UInteger class, not on Color. Type you extends is the type of first method parameter.
  3. You cannot add new method to a class that could be fired like you tried to do it.

You can use your extension method in two ways:

  1. Standard method invocation: ColorExtension.ToColor(100)
  2. Using the extension method syntax: 100.ToColor()

Upvotes: 1

Related Questions