Overloaded methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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(ByVal row as Datarow)
Select Case row.GetType.Name
Case "MyTable1Row"
MySub(Ctype(row,MyTable1Row))
Case "MyTable2Row"
MySub(Ctype(row,MyTable2Row))
End Select
....
End Sub

I was hoping for something more like this

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

Thanks,

Pat
 
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
 
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
 
pmcguire said:
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(ByVal row as Datarow)
Select Case row.GetType.Name
Case "MyTable1Row"
MySub(Ctype(row,MyTable1Row))
Case "MyTable2Row"
MySub(Ctype(row,MyTable2Row))
End Select
...
End Sub

Use "If TypeOf .. Is" instead of comparing strings to avoid misspelled type
names. The compiler can't recognize them.
I was hoping for something more like this

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


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
 
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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top