0

I defined the following extension of the Date? data type

'Nullable Date Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function ToObject(ByVal thisInstance As Date?) As Object
    Return If(thisInstance.HasValue, CType(thisInstance, Object), DBNull.Value)
End Function

Which gave me the terse capability to do this:

Public Property MyDateTime() As Date?

rowTest.Item("MyDate") = Me.MyDate.ToObject

But when I moved my compiler definition to a separate DLL, I the ToObject method was no longer available from my project even though I had referenced the Class project which now contained the extension.

Is this a limitation of the Compiler Extensions? HOw do you get reusability out of them?

Chad
  • 23,658
  • 51
  • 191
  • 321

1 Answers1

0

You'll need to include the .ToObject namespace (the namespace containing your Nullable date extensions) in your target file in the other project.

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • I dont know what namespace that is. The code works if copied directly into eiether project, which suggests to me that the required references are already included in both projects. – Chad Nov 12 '10 at 13:17
  • It's not a matter of just references. You'll need to add a "imports" statement to your target file, importing the namespace containing your extension method. – Judah Gabriel Himango Nov 14 '10 at 17:24
  • Have a look at this: http://msdn.microsoft.com/en-us/library/bb384936.aspx In particular, "Extension methods can be declared only within modules. Typically, the module in which an extension method is defined is not the same module as the one in which it is called. Instead, the module that contains the extension method is imported, if it needs to be, to bring it into scope. After the module that contains Print is in scope, the method can be called as if it were an ordinary instance method that takes no arguments, such as ToUpper:" – Judah Gabriel Himango Nov 17 '10 at 14:19