Custom List casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Goran Djuranovic

    #1

    Custom List casting

    Hi all,
    I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

    For example,

    Dim myList As New MyList
    Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

    Thanks
    Goran Djuranovic
  • Marina Levit [MVP]

    #2
    Re: Custom List casting

    No, that is not going to happen. Because MyList is an IList - that is perfectly legal. I don't see how you can make the compiler think it is not.

    However, that exact line you typed in will be prohibited anyway, because you can't instantiate a variable with New, and yet assign it a value. Not to mention IList is an interface and thus has no constructor and is not something you can instantiate.
    "Goran Djuranovic" <goran.djuranov ic@newsgroups.n ospam> wrote in message news:O42TZxedGH A.1436@TK2MSFTN GP05.phx.gbl...
    Hi all,
    I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

    For example,

    Dim myList As New MyList
    Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

    Thanks
    Goran Djuranovic

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Custom List casting

      "Goran Djuranovic" <goran.djuranov ic@newsgroups.n ospam> schrieb:[color=blue]
      >I have a custom made list (MyList) that inherits from CollectionBase. Is it
      > possible to prohibit the casting of that list into other objects? I would
      > like to get an error at design-time.
      >
      >For example,
      >
      >Dim myList As New MyList
      >Dim yourList As New IList = CType(MyList, IList) <---- prohibit this[/color]

      No, you cannot avoid this. That's how subtyping and interface
      implementation are intended to work and that's why interfaces are used :-).

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Goran Djuranovic

        #4
        Re: Custom List casting

        Thanks both for you comments. The original problem was to create a custom type-safe list of some objects (f.e., Cars), and have it be able to accept only Car objects. But, it looks like that list could be cast(ed) into IList and then passed Object type, which would make it fail at run time. I know type-safe lists are implemented in VB 8, but thought the same coulb be done in VB 7.

        Goran Djuranovic
        "Goran Djuranovic" <goran.djuranov ic@newsgroups.n ospam> wrote in message news:O42TZxedGH A.1436@TK2MSFTN GP05.phx.gbl...
        Hi all,
        I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

        For example,

        Dim myList As New MyList
        Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

        Thanks
        Goran Djuranovic

        Comment

        • Goran Djuranovic

          #5
          Re: Custom List casting

          Well, right after I posted my previous post, I ran into this:

          "You can bypass the "Typed interface" of the collection very simply.. All you need to do is use the IList interface which allows .Add, .Insert etc for type object.." As I said in my previous post.

          This can be prevented by Overriding OnInsert and OnSet. Both of these methods allow you the opertunity of raising an exception if the incorrect datatype
          is passed in through the IList interface...

          It just makes the whole colelction a little safer...

          Eddie de bear
          MCSD "

          Will try it tomorrow.
          GD

          Comment

          Working...