2

I'm currently trying to create a NEWID() function in my DataContext in LINQ using the solution here, however, when adding the partial class to my DataContext.vb or a separate DataContextPartial.vb, I get the error System.Data.Function is not available in this context because it is 'Friend'.

I've come across this when accessing data types before and that was in easy fix of setting it to Public, but I'm not sure where the properties for function could be or how to change them.

The code I have is converted to VB.NET from the C# in the linked answer above:

Partial Public Class CMSModelDataContext
    <[Function](Name:="NEWID", IsComposable:=True)> _
    Public Function Random() As Guid
        Throw New NotImplementedException()
    End Function
End Class

Thanks for any help in advance.

Community
  • 1
  • 1
LiamGu
  • 5,317
  • 12
  • 49
  • 68

2 Answers2

1

I can't remember offhand whether VB applies the "Attribute" suffix automatically. Try this instead:

<FunctionAttribute(Name:="NEWID", IsComposable:=True)>

... and make sure you have an import for System.Data.Linq.Mapping.

EDIT: It looks like VB does apply the Attribute suffix, so I suspect you were missing an import or a reference. However, specifying FunctionAttribute explicitly will at least help you to verify this by removing the "false positive" of System.Data.Function.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I believe you should

Import System.Data.Linq.Mapping

because FunctionAttribute resides there.

You didn't import the namespace, and compiler went to look for the class in the wrong direction. Trying its best and seeing that you have imported System.Data, compiler assumed you want to use System.Data.Function which is an internal (Friend) class in System.Data.dll assembly, hence the error.

One can wonder what exactly is the purpose of this error message. If the class isn't accessible anyway, why even bothering to tell about it? I think the reason is you could've referenced your own assembly forgetting to make some of types Public. It makes sense that compiler warns you that it sees the class but you just can't use it. It also makes sense applying same rules to all references, including framework libraries, although obviously you can't modify anything in there.

I would argue that FunctionAttribute is not a particularly good choice of name because it's begging for wrong namespace imports and related confusion.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511