wizzardmr42
wizzardmr42

Reputation: 1644

Declaring a property as the same type as a specific other property

I'm using EF code first with a fairly large number of entity types and I am building a number of reports with filter models that refer to the ID (primary key) fields in the different entity types. Depending on the table, the ID field is either a Byte, Short, Integer or Long (depending on how many records I expect in that table and where it is referenced from).

Eg. I may have a "Channel" table which has an ID field of type Short, whereas "Order" has an ID field of type "Integer".

When I create a model for a view to filter on eg. Channel, I would do a ChannelID Property, which would either be a Short or a Short? (as in Nullable(Of Short)). However, this is a pain as I have to look up the correct ID type for the relevant entity type each time I add a filter and in the long run, it is fragile as I may change the ID type on a particular entity and don't want to have to hunt down every reference to it.

Ideally, I would like to have some way of referring to it eg.

Public Property ChannelID As TypeOf([Channel].[ID]) 

but I can't see any way to do that.

If I was using C, I would probably #define ChannelID short and just refer to the type as ChannelID throughout, but I can't see any way of using the VB.NET compiler to achieve something like that (which isn't quite as good as the above solution either.

I have thought of the idea of implementing it as an interface for each type, but that doesn't work if there are multiple properties filtering on the same entity in a single model (which is possible), and it is messy as it means an interface for every entity type.

I realise that I could just set them all as Long and there isn't really a major disadvantage to doing this, but I am picky about matching types.

Does anyone have any clever ideas for dealing with this?

Upvotes: 0

Views: 42

Answers (1)

Dave Doknjas
Dave Doknjas

Reputation: 6542

Type aliasing will work across files - set this on the 'References' tab of the project properties - e.g., test = System.Int32. Then in any file in the project you can use the type alias 'test'. e.g.,

Dim myInt As test

Upvotes: 1

Related Questions