Overloaded methods

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

    #1

    Overloaded methods

    A little embarrassing but,

    Public Sub MySub(ByVal row as MyTable1Row)
    ....
    End Sub

    Public Sub MySub(ByVal row as MyTable2Row)
    ....
    End Sub


    How do I get the following to work without the Select...Case block and the
    Ctypes?

    Public Sub DoSomething(ByV al row as Datarow)
    Select Case row.GetType.Nam e
    Case "MyTable1Ro w"
    MySub(Ctype(row ,MyTable1Row))
    Case "MyTable2Ro w"
    MySub(Ctype(row ,MyTable2Row))
    End Select
    ....
    End Sub

    I was hoping for something more like this

    Public Sub DoSomething(ByV al row as DataRow)
    MySub(Ctype(row ,row.GetType)
    ....
    End Sub

    Thanks,

    Pat
    --
    Pat
  • Robin Tucker

    #2
    Re: Overloaded methods


    I think the usual way to do this might be to have the "MySub" method as
    overridden members of the MyTable1Row and MyTable2Row classes, so that the
    behaviour of "MySub" is encapsulated in the class itself; like this:

    public class MyTableRow
    Inherits DataRow

    Public Overridable Sub MySub ()
    End Sub

    End Class

    public class MyTable1Row
    Inherits MyTableRow

    Public Overrides Sub MySub ()
    ' Perform some action
    End Sub
    End Class

    Public Class MyTable2Row
    Inherits MyTableRow

    Public Overrides Sub MySub ()
    ' Perform some action
    End Sub
    End Class


    ............... ...
    ............... ...


    Public Sub Dosomething ( byval theRow as MyTableRow )
    theRow.MySub ()
    End Sub

    "pmcguire" <pmcguire@discu ssions.microsof t.com> wrote in message
    news:3BD54B79-DA48-40D7-A7DD-1E5CC1E39E46@mi crosoft.com...[color=blue]
    >A little embarrassing but,
    >
    > Public Sub MySub(ByVal row as MyTable1Row)
    > ...
    > End Sub
    >
    > Public Sub MySub(ByVal row as MyTable2Row)
    > ...
    > End Sub
    >
    >
    > How do I get the following to work without the Select...Case block and the
    > Ctypes?
    >
    > Public Sub DoSomething(ByV al row as Datarow)
    > Select Case row.GetType.Nam e
    > Case "MyTable1Ro w"
    > MySub(Ctype(row ,MyTable1Row))
    > Case "MyTable2Ro w"
    > MySub(Ctype(row ,MyTable2Row))
    > End Select
    > ...
    > End Sub
    >
    > I was hoping for something more like this
    >
    > Public Sub DoSomething(ByV al row as DataRow)
    > MySub(Ctype(row ,row.GetType)
    > ...
    > End Sub
    >
    > Thanks,
    >
    > Pat
    > --
    > Pat[/color]


    Comment

    • pmcguire

      #3
      Re: Overloaded methods

      Excellent idea. The problem (I think) is then 2 weeks later when I tweak the
      structure of the dataset VisualStudio overwrites all my custom coding in the
      autogenerated classes. Or maybe there is a way to avoid this...?

      Thanks,

      Pat

      "Robin Tucker" wrote:
      [color=blue]
      >
      > I think the usual way to do this might be to have the "MySub" method as
      > overridden members of the MyTable1Row and MyTable2Row classes, so that the
      > behaviour of "MySub" is encapsulated in the class itself; like this:
      >
      > public class MyTableRow
      > Inherits DataRow
      >
      > Public Overridable Sub MySub ()
      > End Sub
      >
      > End Class
      >
      > public class MyTable1Row
      > Inherits MyTableRow
      >
      > Public Overrides Sub MySub ()
      > ' Perform some action
      > End Sub
      > End Class
      >
      > Public Class MyTable2Row
      > Inherits MyTableRow
      >
      > Public Overrides Sub MySub ()
      > ' Perform some action
      > End Sub
      > End Class
      >
      >
      > ............... ...
      > ............... ...
      >
      >
      > Public Sub Dosomething ( byval theRow as MyTableRow )
      > theRow.MySub ()
      > End Sub
      >
      > "pmcguire" <pmcguire@discu ssions.microsof t.com> wrote in message
      > news:3BD54B79-DA48-40D7-A7DD-1E5CC1E39E46@mi crosoft.com...[color=green]
      > >A little embarrassing but,
      > >
      > > Public Sub MySub(ByVal row as MyTable1Row)
      > > ...
      > > End Sub
      > >
      > > Public Sub MySub(ByVal row as MyTable2Row)
      > > ...
      > > End Sub
      > >
      > >
      > > How do I get the following to work without the Select...Case block and the
      > > Ctypes?
      > >
      > > Public Sub DoSomething(ByV al row as Datarow)
      > > Select Case row.GetType.Nam e
      > > Case "MyTable1Ro w"
      > > MySub(Ctype(row ,MyTable1Row))
      > > Case "MyTable2Ro w"
      > > MySub(Ctype(row ,MyTable2Row))
      > > End Select
      > > ...
      > > End Sub
      > >
      > > I was hoping for something more like this
      > >
      > > Public Sub DoSomething(ByV al row as DataRow)
      > > MySub(Ctype(row ,row.GetType)
      > > ...
      > > End Sub
      > >
      > > Thanks,
      > >
      > > Pat
      > > --
      > > Pat[/color]
      >
      >
      >[/color]

      Comment

      • Robin Tucker

        #4
        Re: Overloaded methods

        This I can't answer as I have never used these autogenerated classes.
        Perhaps you could do an experiment and report back to the group ;)

        "pmcguire" <pmcguire@discu ssions.microsof t.com> wrote in message
        news:41D948D0-BD0F-4FF8-9ED1-ABEB17102EAE@mi crosoft.com...[color=blue]
        > Excellent idea. The problem (I think) is then 2 weeks later when I tweak
        > the
        > structure of the dataset VisualStudio overwrites all my custom coding in
        > the
        > autogenerated classes. Or maybe there is a way to avoid this...?
        >
        > Thanks,
        >
        > Pat
        >
        > "Robin Tucker" wrote:
        >[color=green]
        >>
        >> I think the usual way to do this might be to have the "MySub" method as
        >> overridden members of the MyTable1Row and MyTable2Row classes, so that
        >> the
        >> behaviour of "MySub" is encapsulated in the class itself; like this:
        >>
        >> public class MyTableRow
        >> Inherits DataRow
        >>
        >> Public Overridable Sub MySub ()
        >> End Sub
        >>
        >> End Class
        >>
        >> public class MyTable1Row
        >> Inherits MyTableRow
        >>
        >> Public Overrides Sub MySub ()
        >> ' Perform some action
        >> End Sub
        >> End Class
        >>
        >> Public Class MyTable2Row
        >> Inherits MyTableRow
        >>
        >> Public Overrides Sub MySub ()
        >> ' Perform some action
        >> End Sub
        >> End Class
        >>
        >>
        >> ............... ...
        >> ............... ...
        >>
        >>
        >> Public Sub Dosomething ( byval theRow as MyTableRow )
        >> theRow.MySub ()
        >> End Sub
        >>
        >> "pmcguire" <pmcguire@discu ssions.microsof t.com> wrote in message
        >> news:3BD54B79-DA48-40D7-A7DD-1E5CC1E39E46@mi crosoft.com...[color=darkred]
        >> >A little embarrassing but,
        >> >
        >> > Public Sub MySub(ByVal row as MyTable1Row)
        >> > ...
        >> > End Sub
        >> >
        >> > Public Sub MySub(ByVal row as MyTable2Row)
        >> > ...
        >> > End Sub
        >> >
        >> >
        >> > How do I get the following to work without the Select...Case block and
        >> > the
        >> > Ctypes?
        >> >
        >> > Public Sub DoSomething(ByV al row as Datarow)
        >> > Select Case row.GetType.Nam e
        >> > Case "MyTable1Ro w"
        >> > MySub(Ctype(row ,MyTable1Row))
        >> > Case "MyTable2Ro w"
        >> > MySub(Ctype(row ,MyTable2Row))
        >> > End Select
        >> > ...
        >> > End Sub
        >> >
        >> > I was hoping for something more like this
        >> >
        >> > Public Sub DoSomething(ByV al row as DataRow)
        >> > MySub(Ctype(row ,row.GetType)
        >> > ...
        >> > End Sub
        >> >
        >> > Thanks,
        >> >
        >> > Pat
        >> > --
        >> > Pat[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Armin Zingler

          #5
          Re: Overloaded methods

          "pmcguire" <pmcguire@discu ssions.microsof t.com> schrieb[color=blue]
          >A little embarrassing but,
          >
          > Public Sub MySub(ByVal row as MyTable1Row)
          > ...
          > End Sub
          >
          > Public Sub MySub(ByVal row as MyTable2Row)
          > ...
          > End Sub
          >
          >
          > How do I get the following to work without the Select...Case block
          > and the Ctypes?
          >
          > Public Sub DoSomething(ByV al row as Datarow)
          > Select Case row.GetType.Nam e
          > Case "MyTable1Ro w"
          > MySub(Ctype(row ,MyTable1Row))
          > Case "MyTable2Ro w"
          > MySub(Ctype(row ,MyTable2Row))
          > End Select
          > ...
          > End Sub[/color]

          Use "If TypeOf .. Is" instead of comparing strings to avoid misspelled type
          names. The compiler can't recognize them.
          [color=blue]
          > I was hoping for something more like this
          >
          > Public Sub DoSomething(ByV al row as DataRow)
          > MySub(Ctype(row ,row.GetType)
          > ...
          > End Sub[/color]


          CType requires a type expression, not a System.Type object because the type
          and the call must be resolved at compile time. I don't see a way around
          CType/Directcast.


          Armin

          Comment

          Working...