I am not a programmer...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    I am not a programmer...

    which is why I am asking yet another question.
    Apologies to Alex who probably already answered this.

    I have this code:

    Code:
    public Interface iLinkable 
    end interface
     
    public class fibreport 
        implements iLinkable
     
     Dim portname As string
    end class
     
    public class ethernet 
        implements iLinkable
    Dim portname As string
    end class 
     
    public class fabric
     
    Dim ISLs As iLinkable()
     
    'tests the iLinkable interface
    sub tester 
       Redim ISLs(2)
       ISLs(1) = new fibreport
       ISLs(2) = new ethernet
    end sub
    Now when I try to do something like:
    Code:
    ISLs(1).portname = "tester"
    I get a compiler error to the effect of no such property 'portname' for iLinkable. I am expecting it to resolve ISLs(1) to the fibreport object stored there.

    but this works:
    Code:
    Dim newport As fibreport = ISLs(1)
    fibreport.portname = "tester"
    So why does first not work but second does and how do I get first to work? This has implications for other stuff I am trying to do. Google's got nothin'. Well I'm sure it has something, but I can't find it.
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • Alex Franke
    Veteran Member
    • Feb 2007
    • 2641
    • Chapel Hill, NC
    • Ryobi BT3100

    #2
    You have two options.

    Option 1:
    "ISLs(1)" is a reference to an interface, not a specific object, and the interface doesn't know that the object has a property called portname. So, add portName to the interface definition. If everything that is ILinkable must have a property called PortName, then PortName can be part of the contract. Like this:

    Code:
    Interface ILinkable
        Property PortName() As String 
    End Interface
    
    ' Implement the property required by the interface in the class that implements the interface
    public class ethernet 
        implements iLinkable
    
        Private _Portname As string
    
        Property PortName() As String
            Get
                Return _PortName
            End Get
            Set(ByVal Value As String)
                _PortName = value
            End Set
        End Property
    end class
    Now the compiler will demand that everything that you say implements ILinkable does in fact have the PortName property -- it's part of the contract -- part of what it means to be ILinkable. And now ILinkable knows that everything that implements it has this property, so a reference to an ILinkable object in an array of ILinkables will be able to access it.

    If there's other stuff that an ILinkable object should be able to do, then you should also add it to the interface definition. For example, an IDrivable interface might have properties called Speed and Direction and functions called Steer(), Accelerate() and Brake(). Then if you have a Car object that implements IDrivable, it must implement all those properties and functions, so that if you have an array of IDrivable objects (car, boat, bicycle, etc), you can access any of these properties or call any of these functions on those objects while only knowing that they're IDrivable. (You don't need to know it's a car or a boat -- only that it's IDrivable.)

    Option 2:
    ...is exactly what you did in your second example. You're "casting" the object held in the interface array back to a specific object, and the computer knows that that object has a property called PortName.
    Last edited by Alex Franke; 09-22-2007, 08:07 PM.
    online at http://www.theFrankes.com
    while ( !( succeed = try() ) ) ;
    "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

    Comment

    • Alex Franke
      Veteran Member
      • Feb 2007
      • 2641
      • Chapel Hill, NC
      • Ryobi BT3100

      #3
      Originally posted by crokett
      Code:
      Dim newport As fibreport = ISLs(1)
      fibreport.portname = "tester"
      I think you mean this:

      Code:
      Dim newport As fibreport = ISLs(1)
      [B]newport[/B].portname = "tester"
      online at http://www.theFrankes.com
      while ( !( succeed = try() ) ) ;
      "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

      Comment

      • crokett
        The Full Monte
        • Jan 2003
        • 10627
        • Mebane, NC, USA.
        • Ryobi BT3000

        #4
        It seems a cleaner solution is multiple interfaces with the methods/properties I need declared in them.

        For the casting for every port/switch/etc I wanted to update I'd have to cast it, make the change then cast back into the array.

        thanks Alex

        <edit>
        On second thought I think I need to stick with casting or not use interfaces. There a few properties unique to each type of port that can't be implemented in the interface.
        </edit>
        Last edited by crokett; 09-22-2007, 08:23 PM.
        David

        The chief cause of failure in this life is giving up what you want most for what you want at the moment.

        Comment

        • Alex Franke
          Veteran Member
          • Feb 2007
          • 2641
          • Chapel Hill, NC
          • Ryobi BT3100

          #5
          Originally posted by crokett
          It seems a cleaner solution is multiple interfaces with the methods/properties I need declared in them.
          Yeah, I agree. Or simple inheritance if in fact you're talking about two different flavors of the same object. For example, you might use an IDrivable interface and implement it for things like a Car, Boat, or even a Conversation or GolfBall. But if you're really talking about objects that represent a Coupe, FamilySedan, and PickupTruck, then you'd probably just use a Car base class with the properties and functions you want and inherit those for your subclasses.

          Originally posted by crokett
          For the casting for every port/switch/etc I wanted to update I'd have to cast it, make the change then cast back into the array.
          Yes and no -- you'd have to cast out of ILinkable (and perhaps more importantly you'd have to know what type of object you want to cast to), but you shouldn't have to cast back to ILinkable to get it back into an ILinkable array. In fact, you probably won't have even removed the item from the array in the first place...

          No problem, crokett -- always happy to help!
          online at http://www.theFrankes.com
          while ( !( succeed = try() ) ) ;
          "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

          Comment

          Working...