Conversion from a base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael D. Ober

    #1

    Conversion from a base class

    I have a class:


    Private Class ClientList
    Inherits StringCollectio n

    Public InternalUser As Boolean = False

    Public Shadows Function Contains(ByVal ClientNumber As String) As
    Boolean
    If InternalUser Then Return True
    Return MyBase.Contains (ClientNumber)
    End Function
    End Class

    I need to do the following:

    dim mClientList as ClientList
    dim mStringCollecti on as new StringCollectio n

    mClientList = mStringCollecti on.

    What do I do to my class definition to allow this? Option Strict On flags
    this as an illegal implicit conversion. I'm open to using

    mClientList = new ClientList(mStr ingCollection) as well.

    Thanks,
    Mike Ober.


  • Tom Leylan

    #2
    Re: Conversion from a base class

    I guess the standard question is "why?" You'll agree that a
    StringCollectio n <ClientList, so what creates the "need?"


    "Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
    news:u2jPMzzPHH A.3668@TK2MSFTN GP02.phx.gbl...
    >I have a class:
    >
    >
    Private Class ClientList
    Inherits StringCollectio n
    >
    Public InternalUser As Boolean = False
    >
    Public Shadows Function Contains(ByVal ClientNumber As String) As
    Boolean
    If InternalUser Then Return True
    Return MyBase.Contains (ClientNumber)
    End Function
    End Class
    >
    I need to do the following:
    >
    dim mClientList as ClientList
    dim mStringCollecti on as new StringCollectio n
    >
    mClientList = mStringCollecti on.
    >
    What do I do to my class definition to allow this? Option Strict On flags
    this as an illegal implicit conversion. I'm open to using
    >
    mClientList = new ClientList(mStr ingCollection) as well.
    >
    Thanks,
    Mike Ober.

    Comment

    • wfairl@gmail.com

      #3
      Re: Conversion from a base class

      Create a constructor in the derived class that accepts an instance of
      the base class.

      On Jan 23, 2:21 pm, "Michael D. Ober" <obermd.@.alum. mit.edu.nospam>
      wrote:
      I have a class:
      >
      Private Class ClientList
      Inherits StringCollectio n
      >
      Public InternalUser As Boolean = False
      >
      Public Shadows Function Contains(ByVal ClientNumber As String) As
      Boolean
      If InternalUser Then Return True
      Return MyBase.Contains (ClientNumber)
      End Function
      End Class
      >
      I need to do the following:
      >
      dim mClientList as ClientList
      dim mStringCollecti on as new StringCollectio n
      >
      mClientList = mStringCollecti on.
      >
      What do I do to my class definition to allow this? Option Strict On flags
      this as an illegal implicit conversion. I'm open to using
      >
      mClientList = new ClientList(mStr ingCollection) as well.
      >
      Thanks,
      Mike Ober.

      Comment

      • _AnonCoward

        #4
        Re: Conversion from a base class


        "Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
        news:u2jPMzzPHH A.3668@TK2MSFTN GP02.phx.gbl...
        :
        : I have a class:
        :
        :
        : Private Class ClientList
        : Inherits StringCollectio n
        :
        : Public InternalUser As Boolean = False
        :
        : Public Shadows Function Contains(ByVal ClientNumber As String) As
        : Boolean
        : If InternalUser Then Return True
        : Return MyBase.Contains (ClientNumber)
        : End Function
        : End Class
        :
        : I need to do the following:
        :
        : dim mClientList as ClientList
        : dim mStringCollecti on as new StringCollectio n
        :
        : mClientList = mStringCollecti on.
        :
        : What do I do to my class definition to allow this? Option Strict On
        : flags this as an illegal implicit conversion. I'm open to using
        : mClientList = new ClientList(mStr ingCollection) as well.
        :
        : Thanks,
        : Mike Ober.


        Use this syntax:

        mClientList = CType(mStringCo llection, ClientList)

        That said, don't actually try this. What you are doing here doesn't
        make sense. Your new derived class ClientList exposes a new bit of
        functionality (the InternalUser field) not found in the base
        StringCollectio n Class. If you assign an instance of a
        StringCollectio n type to the mClientList variable, your code would end
        up choking if it ever tried to access that property. The following
        compiles fine, but throws an error when run:


        Dim mClientList As ClientList
        Dim mStringCollecti on As New StringCollectio n

        mClientList = CType(mStringCo llection, ClientList)

        'oops: can't get to the InternalUser field!
        Console.WriteLi ne(mClientList. InternalUser)

        The CType() method gets rid of that pesky compiler error, but the code
        still bombs when you get to the WriteLine statement because while the
        mClientList type (ClientList) exposes the InternalUser property, the
        instance you are referencing (StringCollecti on) does not.

        Now, it does work in reverse:

        mStringCollecti on = mClientList

        This compiles and runs without issue because the ClientList type
        inherits from the StringCollectio n type and therefore exposes all of
        the functionality of that base type (the functionality may be
        overridden, but it's there). You'll never be able to access the
        InternalUser property as long as the code think's it's working with an
        instance of the StringCollectio n type, but that isn't an issue - there
        are ways around that. For example, you can do the following:


        Dim mClientList As New ClientList
        Dim mStringCollecti on As StringCollectio n

        mStringCollecti on = mClientList

        Dim intUser As Boolean
        intUser = CType(mStringCo llection, ClientList).Int ernalUser

        Console.WriteLi ne(intUser)


        Ralf
        --
        --
        ----------------------------------------------------------
        * ^~^ ^~^ *
        * _ {~ ~} {~ ~} _ *
        * /_``>*< >*<''_\ *
        * (\--_)++) (++(_--/) *
        ----------------------------------------------------------
        There are no advanced students in Aikido - there are only
        competent beginners. There are no advanced techniques -
        only the correct application of basic principles.


        Comment

        • Michael D. Ober

          #5
          Re: Conversion from a base class

          OK - I tried and came up blank before I posted my original message. How do
          I assign the base class object?

          public sub New(ByVal sc as StringCollectio n)
          ' What goes here? I can't use "mybase = sc"
          ' Do I actually need to loop through sc and add each element to mybase
          mybase.new()
          for each s as String in sc
          mybase.Add(s)
          next s
          end sub

          Thanks,
          Mike.


          <wfairl@gmail.c omwrote in message
          news:1169599686 .444310.188710@ 13g2000cwe.goog legroups.com...
          Create a constructor in the derived class that accepts an instance of
          the base class.
          >
          On Jan 23, 2:21 pm, "Michael D. Ober" <obermd.@.alum. mit.edu.nospam>
          wrote:
          >I have a class:
          >>
          >Private Class ClientList
          > Inherits StringCollectio n
          >>
          > Public InternalUser As Boolean = False
          >>
          > Public Shadows Function Contains(ByVal ClientNumber As String) As
          >Boolean
          > If InternalUser Then Return True
          > Return MyBase.Contains (ClientNumber)
          > End Function
          >End Class
          >>
          >I need to do the following:
          >>
          >dim mClientList as ClientList
          >dim mStringCollecti on as new StringCollectio n
          >>
          >mClientList = mStringCollecti on.
          >>
          >What do I do to my class definition to allow this? Option Strict On
          >flags
          >this as an illegal implicit conversion. I'm open to using
          >>
          >mClientList = new ClientList(mStr ingCollection) as well.
          >>
          >Thanks,
          >Mike Ober.
          >
          >


          Comment

          • Michael D. Ober

            #6
            Re: Conversion from a base class

            Business Rules create the need.

            Mike.

            "Tom Leylan" <tleylan@nospam .netwrote in message
            news:eKtGo4zPHH A.3944@TK2MSFTN GP06.phx.gbl...
            >I guess the standard question is "why?" You'll agree that a
            >StringCollecti on <ClientList, so what creates the "need?"
            >
            >
            "Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
            news:u2jPMzzPHH A.3668@TK2MSFTN GP02.phx.gbl...
            >>I have a class:
            >>
            >>
            >Private Class ClientList
            > Inherits StringCollectio n
            >>
            > Public InternalUser As Boolean = False
            >>
            > Public Shadows Function Contains(ByVal ClientNumber As String) As
            >Boolean
            > If InternalUser Then Return True
            > Return MyBase.Contains (ClientNumber)
            > End Function
            >End Class
            >>
            >I need to do the following:
            >>
            >dim mClientList as ClientList
            >dim mStringCollecti on as new StringCollectio n
            >>
            >mClientList = mStringCollecti on.
            >>
            >What do I do to my class definition to allow this? Option Strict On
            >flags this as an illegal implicit conversion. I'm open to using
            >>
            >mClientList = new ClientList(mStr ingCollection) as well.
            >>
            >Thanks,
            >Mike Ober.
            >
            >
            >


            Comment

            • Tom Leylan

              #7
              Re: Conversion from a base class

              I suppose I was a bit too cryptic. A StringCollectio n isn't a ClientList so
              unless something changes in the way OOP is implemented in DotNet I don't
              think you'll just pass a reference and have it magically connect up.

              You can copy the elements in the StringCollectio n into the ClientList and
              probably fairly quickly using CopyTo and AddRange(). I could be mistaken
              but I don't think you'll find syntax that will permit what you are hoping to
              do.

              Tom


              "Michael D. Ober" <obermd.@.alum. mit.edu.no.spam wrote in message
              news:zaCth.1473 0$w91.4537@news read1.news.pas. earthlink.net.. .
              Business Rules create the need.
              >
              Mike.
              >
              "Tom Leylan" <tleylan@nospam .netwrote in message
              news:eKtGo4zPHH A.3944@TK2MSFTN GP06.phx.gbl...
              >>I guess the standard question is "why?" You'll agree that a
              >>StringCollect ion <ClientList, so what creates the "need?"
              >>
              >>
              >"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
              >news:u2jPMzzPH HA.3668@TK2MSFT NGP02.phx.gbl.. .
              >>>I have a class:
              >>>
              >>>
              >>Private Class ClientList
              >> Inherits StringCollectio n
              >>>
              >> Public InternalUser As Boolean = False
              >>>
              >> Public Shadows Function Contains(ByVal ClientNumber As String) As
              >>Boolean
              >> If InternalUser Then Return True
              >> Return MyBase.Contains (ClientNumber)
              >> End Function
              >>End Class
              >>>
              >>I need to do the following:
              >>>
              >>dim mClientList as ClientList
              >>dim mStringCollecti on as new StringCollectio n
              >>>
              >>mClientList = mStringCollecti on.
              >>>
              >>What do I do to my class definition to allow this? Option Strict On
              >>flags this as an illegal implicit conversion. I'm open to using
              >>>
              >>mClientList = new ClientList(mStr ingCollection) as well.
              >>>
              >>Thanks,
              >>Mike Ober.

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Conversion from a base class

                Business Rules create the need.
                >
                Mike.
                >
                Mike,

                I cannot resist on this, this kind of answers let me think about

                Is there to a Business Rule that say thay you have to pee with your head in
                the urinal and everybody is doing their best to fullfil that.

                Cor




                Comment

                • Michael D. Ober

                  #9
                  Re: Conversion from a base class

                  It turns out that the only place where I need to convert the
                  StringCollectio n to ClientList is in a line such as:

                  dim sc as StringCollectio n
                  dim cl as ClientList
                  ' do some processing that may or may not create an actual StringCollectio n
                  if sc is Nothing then cl = new ClientList else cl = new ClientList(sc)

                  The code in this application is littered with cl.Contains, which as I stated
                  in another clarification post, sometimes needs to always return true (this
                  is actually part of a web-site and internal users get additional features)

                  I ended up implementing ClientList.New( StringCollectio n) by interating
                  through the collection and adding it to mybase.items.

                  Thanks for all the feedback.
                  Mike Ober.

                  "Tom Leylan" <tleylan@nospam .netwrote in message
                  news:ua8sAJ5PHH A.2468@TK2MSFTN GP06.phx.gbl...
                  >I suppose I was a bit too cryptic. A StringCollectio n isn't a ClientList
                  >so unless something changes in the way OOP is implemented in DotNet I don't
                  >think you'll just pass a reference and have it magically connect up.
                  >
                  You can copy the elements in the StringCollectio n into the ClientList and
                  probably fairly quickly using CopyTo and AddRange(). I could be mistaken
                  but I don't think you'll find syntax that will permit what you are hoping
                  to do.
                  >
                  Tom
                  >
                  >
                  "Michael D. Ober" <obermd.@.alum. mit.edu.no.spam wrote in message
                  news:zaCth.1473 0$w91.4537@news read1.news.pas. earthlink.net.. .
                  >Business Rules create the need.
                  >>
                  >Mike.
                  >>
                  >"Tom Leylan" <tleylan@nospam .netwrote in message
                  >news:eKtGo4zPH HA.3944@TK2MSFT NGP06.phx.gbl.. .
                  >>>I guess the standard question is "why?" You'll agree that a
                  >>>StringCollec tion <ClientList, so what creates the "need?"
                  >>>
                  >>>
                  >>"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
                  >>news:u2jPMzzP HHA.3668@TK2MSF TNGP02.phx.gbl. ..
                  >>>>I have a class:
                  >>>>
                  >>>>
                  >>>Private Class ClientList
                  >>> Inherits StringCollectio n
                  >>>>
                  >>> Public InternalUser As Boolean = False
                  >>>>
                  >>> Public Shadows Function Contains(ByVal ClientNumber As String) As
                  >>>Boolean
                  >>> If InternalUser Then Return True
                  >>> Return MyBase.Contains (ClientNumber)
                  >>> End Function
                  >>>End Class
                  >>>>
                  >>>I need to do the following:
                  >>>>
                  >>>dim mClientList as ClientList
                  >>>dim mStringCollecti on as new StringCollectio n
                  >>>>
                  >>>mClientLis t = mStringCollecti on.
                  >>>>
                  >>>What do I do to my class definition to allow this? Option Strict On
                  >>>flags this as an illegal implicit conversion. I'm open to using
                  >>>>
                  >>>mClientLis t = new ClientList(mStr ingCollection) as well.
                  >>>>
                  >>>Thanks,
                  >>>Mike Ober.
                  >
                  >
                  >


                  Comment

                  • Tom Leylan

                    #10
                    Re: Conversion from a base class

                    "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                    news:OI3PmXEQHH A.3668@TK2MSFTN GP02.phx.gbl...
                    Is there to a Business Rule that say thay you have to pee with your head
                    in the urinal and everybody is doing their best to fullfil that.
                    I'd just like to save everybody the effort and point out that I have indeed
                    tried this and while it may seem practical at first, it does not work!


                    Comment

                    • Ravichandran J.V.

                      #11
                      Re: Conversion from a base class

                      You would need to overload the = operator with the implicit / explicit
                      keyword. The operator overloading method should return a type of the
                      other class.

                      with regards,


                      J.V.Ravichandra n
                      - http://www.geocities.com/
                      jvravichandran
                      - Or, just search on "J.V.Ravichandr an"
                      at http://www.Google.com

                      *** Sent via Developersdex http://www.developersdex.com ***

                      Comment

                      Working...