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

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
people
quotationmark

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).

people

See more on this question at Stackoverflow