Results 1 to 2 of 2

Thread: Shared function in an interface

  1. #1
    olsonpm is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2010
    Posts
    46
    Reputation
    84

    Shared function in an interface

    My base problem is that I would like to have an interface define a shared function, although through vb (the CLR from what i've read) does not allow for it. After reading, I don't think there is a single solution to this problem, so let me give you some context.

    I am trying to implement MVC with a program that interprets logs, and spits it out in a format that will allow the users to possibly debug, or at least know more about what happened in case something goes wrong. My view handles the gui, which will soon change to a web interface. My controller handles the input, and then my model handles the raw data. I then have seperate classes handle the business logic, since they might change by themselves later on - this is where I would like to enforce the shared function. One of the functions I need from the business logic is to interpret any given line of a log. I could name this class 'LogConverter'. Since it has no state, I would like it to have the method 'public shared function ConvertLog(byval log as string) as string'. Later, I was assuming someone might want to implement the converter differently, so I figured this method would be defined and implemented through ILogConverter - but obviously I can't do this.

    according to this read Shared functions with interface, a singleton pattern is the way to go. However, the singleton in this case seems as much a hack as simply leaving it to the instance itself - which was going to be my solution. Is there any clean way of accomplishing my goal?

    thank you,
    Phil

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,041
    Reputation
    1753
    static functions in an interface are not logical - an interface ensures that an instance of a particular object can be treated as a certain type, but statics have no instance - there's only one morph of a static, so it cannot be declared as an interface because it cannot be polymorphed

    Why don't you just define a base class (rather than an interface) that different converters can derive from, and provide a default implementation of your LogConverter code. Plugins can override the method if they choose or leave it alone

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •