#1
  1. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    185

    [VB.NET] Object ambiguity with classes in a namespace


    I'm writing a program that works with a 3rd-party .NET library. The COM allows for an interface between .NET (or COM) programs and a proprietary format used by an accounting program.

    Sadly, the documentation, despite being over 200 pages, is sorely lacking when it comes to explaining a lot of the basics, and much of the code is counter-intuitive. (For instance: to search for a specific entry by a term, you first have to include the term into a "key buffer" by using one of five functions based on the term's type, then make another call to a different function on a different class object with the parameter with what index (read: column) to use.) So I am trying to write my own middle-man class that will make the program not only easier to code, but also more reader-friendly should someone else have to take over my code in the future.

    The namespace itself contains about 50 classes, most of which are related to one exact file in the accounting software; while these all share one or two basic functions, they all have different properties to align with each column in the files. I'm trying to make my middleware extensible for the future (in case the library itself or the accounting software changes), and in any case I really don't feel like writing 50 classes, one for each file.

    I'm trying to pass an ambiguous object type containing one of these file classes to my function, which then assigns the type to a local variable. The actual properties of the class in question don't matter, as the variable will be manipulated at the top level (or is that bottom?). I have Option Explicit On, and while turning that off might at least fix the nagging, I fear it won't take care of the errors that they could cause at runtime.

    The Class Code:
    [code=.NET]Public Class MiddleWare

    Private oConn As New ConnLibrary.LibraryAccessClass
    Private oFile As New Object

    Public Sub New(ByVal filename As String, ByVal fileType As Type)
    Status = btConn.btOpen(mCompanyPath & "\" & filename, pcReadWrite)
    If Status <> bts.OK Then
    Throw FileAccessError(Status)
    End If

    fileObj = Activator.CreateInstance(fileType)

    fileObj.Initialize(btConn.ThreadIndex)
    End Sub
    End Class[/code]The calling code:[code=.NET]Dim fileAccess As New MiddleWare("filename.dat",GetType(ConnLibrary.FileInQuestion))[/code].Initialize is one of the few methods that are in all relevant classes. But, since fileObj does not have a defined type at compile time, Option Explicit throws a fit for .Initialize. Is there a way around this without turning off Option Explicit? I find it useful for debugging, and don't really trust that those who take over for me once I'm gone will understand enough to be safe without it.
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.
  2. #2
  3. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    185
    I just gave up and turned off Option Explicit. I feel like less of a man.
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Since you know the name and signature of the method, you can continue using reflection to call it. Something like
    Code:
    fileType.GetMethod("Initialize", New Type() { GetType(whatever the ThreadIndex is) }).Invoke(fileObj, New Object() { btConn.ThreadIndex })
  6. #4
  7. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    185
    I'll give that a shot, thanks!
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.

IMN logo majestic logo threadwatch logo seochat tools logo