So a VB interface can't have shared functions. Is there an alternative to creating dummy objects?

Jay

To avoid getting into the weeds on my particular program, let me just create a simplified case.

I have a generic class that should work on a variety of objects. Each of those objects must implement a certain interface.

What I WANT to say is something like:

Public Interface GenThing
    Shared Function thing_name() As String ' This doesn't work! Can't be shared!
    Sub FillOne(row As DataRow)
End Interface

public class Thing1
  implements GenThing
    public shared function thing_name() as string implements GenThing.thing_name
        return "thing number one"
    end function

    public sub FillOne(row as DataRow) implements GenThing.MakeOne
        ... bunch of work ...
    end sub
end class

public class ThingUtil(of T as {GenThing,New})
    public function GetList(id as integer) as List(of T)
      dim name=T.thing_name() ' This doesn't work!
      dim ds as DataSet=GetData(name,id) ' bunch of work here that's the whole point of the class but not relevant to the question
      dim my_list = new List(of T)
      for each row as DataRow in ds.tables(0).rows
          dim my_t = new T()
          my_t.FillOne(row)
          my_list.add(my_t)
      next
      return my_list
    end function
end class

Do you get my problem? I need every class that implements the interface to have a function that returns a "name" that is used to get the data that is needed to create an instance of the object. But I need to know this name BEFORE I create the instance, because I need it to be able to create the instance. But VB doesn't allow an interface to have a shared function, so what I want to write doesn't work.

So what I've done is this:

I make thing_name not shared.

Then instead of simply "dim name=T.thing_name()", I write

dim dummy = new T()
dim name = dummy.thing_name()

Okay, it works, but it seems really ugly. I create an instance of the object, with all the overhead that that involves, just to get a piece of constant text.

Is there a better way? Or am I making a big deal out of nothing?

Update

I see that two people voted to close this question on the grounds that it is the same as "Why can't we have shared functions in an interface?"

I am not asking why I can't have a shared. I am saying, GIVEN that I can't, how do I solve this particular problem?

Jon Skeet

There's no really simple way of fixing this, no.

Depending on what thing_name does, however, you might approach things in a different way. If each implementation just returns a constant value, then it's effectively metadata about the class - and could be described in an attribute instead, which can be fetched at execution time. (See Type.GetCustomAttributes.) Unfortunately you can't then enforce all types implementing the interface to be decorated with the attribute - but you could write a unit test to check this pretty easily.

If thing_name needs to really do work at execution time, that's tougher. You could potentially look for a well-known shared method name instead and execute that via reflection (and again have unit tests to check that it's implemented properly).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can we have unused functions but we can't have unused variables?

how can Map.Entry interface call getValue()/getKey() methods without creating objects?

So we can use virtual functions with objects allocated on stack?

Why can't an interface have fields?

GCC compatibility of shared libraries with STL objects in their interface

Creating internal functions (can't be called from console) in R

Procedure with assumed-shape dummy argument must have an explicit interface

Creating an anonymous object that implements an interface with overloaded functions

So I can't have NOT NULL in column definition of temporal tables?

Intellisense can't detect functions in vb.net created dll

Creating a pipeline function that is asynchronous so functions can be awaited

One line arrow functions without braces - can't have a semicolon?

Odoo, can't have two different siblings objects in the same view

Why can PL/pgSQL functions have side effect, while SQL functions can't?

Creating lambda functions in python that can return objects

Creating a map of functions that return an interface

Function-local static objects in static inline functions aren't shared

Javascript: Are variables in prototype functions shared across objects?

Creating interface objects in java

CGPoint variable initialized in @interface of .m file can't have value

Creating a hyperlink in the VB can't seem the quotes right

Error when creating object implements interface: The class doesn't have a constructor

why can't we have concrete implementation of base interface in aggregation?

Can't have a regular property and computed property keys in the same interface

Can't we have just variables to Objects in c++?

What can I do so functions don't stack?

Can someone help me in creating a dummy query for following use case

Can I have a collection over trait objects where their associated types implement a shared trait?

Can't have two network interface enabled at the same time?