Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Classes, LINQ, Interfaces and VB.NET

Okay, I have ran in to a problem with VB.NET. So all those defenders of VB.NET please can you help me out? Here is my issue: I am using LINQ to Entities, it also works with LINQ to SQL, I build my edmx file I then create a Partial Publc Class of the same name as one of the entities All fine up to now, so here comes the problem.

I need the Partial class to implement an Interface Implements Interfaces.IAsset But VB.NET want to place "Implements Interfaces.IAsset.ID" at the end of the property, which of course is in the generated code section of the Partial class, which I can not do as the edmx file is auto generated and will change in the future.

Here is the line of code that needs sorting:

Public Property ID1() As Integer Implements Interfaces.IAsset.ID

In C# it is fine and works wonders. So why can't VB.NET implement an interface that is in the code generated class?

like image 791
Coppermill Avatar asked Jan 23 '09 14:01

Coppermill


2 Answers

A little bit of indirection in the partial class should do the trick...

Public Property AssetId() As Int32 Implements IAsset.AssetId
    Get
        Return Id1
    End Get
    Set(ByVal value As Int32)
        Id1 = value
    End Set
End Property

The property that implements the interface calls the actual property generated by the dbml. The class will essentially have two id properties, which is not very elegant but when accessed through the interface you won't know the difference.

like image 123
TGnat Avatar answered Sep 30 '22 03:09

TGnat


There is no way round this. It's a flaw in how VB forces you to handle the implementation of interfaces.

You could create a separate library for your LINQ to Entities that was in C#. I'm not sure if you could just put partial classes in the separate project and keep the basics in a VB one. I've never tried that.

like image 21
Garry Shutler Avatar answered Sep 30 '22 04:09

Garry Shutler