473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Overloaded Private Function in Module crashes ...

Hello -

The following code will crash if the TestA() instances are declared as
Private and work just fine if they are Public.

Is there a way to hide these instances to callers from outside the
Module? It should only be possible to call the one instance of TestA()
without the second argument.

ModuleX
Sub main()
initialize(0)
TestA(0)
End Sub

Public Class cProjectBase
End Class

Public Class cProjectA
Inherits cProjectBase
End Class

Public Class cProjectB
Inherits cProjectBase
End Class

Dim mProjectInstance As Object

Sub initialize(ByVal aFlag As Integer)
If aFlag = 0 Then
mProjectInstance = New cProjectA
End If

If aFlag = 1 Then
mProjectInstance = New cProjectB
End If
End Sub

Public Sub TestA(ByRef aDummy As Integer)
TestA(aDummy, mInstance)
End Sub

Private Sub TestA(ByRef aDummy As Integer, ByRef aX As cProjectA)
Console.WriteLine("TestA for cProjectA")
End Sub

Private Sub TestA(ByRef aDummy As Integer, ByRef aX As cProjectB)
Console.WriteLine("TestA for cProjectB")
End Sub
End Module

Thanks,
Joe

Nov 21 '05 #1
6 1289
Interesting. I copied the code above and cannot recreate your error.
The only change I made was your reference to mInstance in the
TestA(ByRef aDummy As Integer) function.

The module I created only showed the public functions outside of the
module. I'm a bit confused as to how it crashes. Where in your
debugger does it crash? And what error do you get?

Derek Woo

Nov 21 '05 #2
Hello -

Sorry for the late reply but I was out for a few days. Here is the
error message I get ...

Unhandled Exception: System.MissingMemberException: Method
'Module1.TestA' cannot be called with 2 argument(s).
at
Microsoft.VisualBasic.CompilerServices.VBBinder.se t_InternalThrow(Exception
Value)
at
Microsoft.VisualBasic.CompilerServices.VBBinder.Bi ndToMethod(BindingFlags
bindingAttr, MethodBase[] match, Object[]& args,
ParameterModifier[] modifiers, CultureInfo culture, String[] names,
Object& ObjState)
at
Microsoft.VisualBasic.CompilerServices.VBBinder.In vokeMember(String
name, BindingFlags invokeAttr, Type objType, IReflect
objIReflect, Object target, Object[] args, ParameterModifier[]
modifiers, CultureInfo culture, String[] namedParameters)
at
Microsoft.VisualBasic.CompilerServices.LateBinding .InternalLateCall(Object
o, Type objType, String name, Object[] args, St
ring[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at
Microsoft.VisualBasic.CompilerServices.LateBinding .LateCall(Object o,
Type objType, String name, Object[] args, String[] p
aramnames, Boolean[] CopyBack)
at ConsoleApplication1.Module1.TestA(Int32& aDummy) in
C:\...\ConsoleApplication1\Module1
..vb:line 38
at ConsoleApplication1.Module1.main() in C:\...\Module1.vb:line 4

It crashes when it executes ...
TestA(aDummy, mProjectInstance)
in the first instance of TestA.

Thanks!
Joe

Nov 21 '05 #3
Joe,
I found your problem and was able to recreate it. The problem is
that you're trying do perform late binding in your code. Since you
declare mProjectInstance as an Object, when you try to pass this to
your overloaded TestA functions, the runtime doesn't know which one to
call. What you need to do is determine what type of object
mProjectInstance is, then ctype that into the the TestA function.
Here's what I did to get it to work:

Public Sub TestA(ByRef aDummy As Integer)
If mProjectInstance.GetType.Equals(GetType(cProjectA) ) Then
TestA(3, CType(mProjectInstance, cProjectA))
ElseIf mProjectInstance.GetType.Equals(GetType(cProjectB) ) Then
TestA(3, CType(mProjectInstance, cProjectB))
End If
End Sub

Since you don't explicitly have a TestA function that takes an Integer
and an Object as its parameters, the runtime doesn't know which of the
two functions to call. You need to tell it which one to call by
casting the mProjectInstance into the proper type. This should solve
your problem. Let me know if you need anything else.

Derek

Nov 21 '05 #4
Hello -

Thanks for your help! I used to do a lot of C++ programming and I did
not encounter a problem like that there.

What I am trying to do is have a single function that is called from
the outside and then have that function call the appropriate private
function instance. I wanted to do this without if/elseif statements
within TestA since I have a lot of functions just like TestA and I also
wanted it to be easily expandable.

Why does this work if the TestA instances are Public? I tried to
declare mProjectInstance as cProjectBase but that would not work either
because of "narrowing down" problems. It just seems that something
like that was much easier in C++.

Thanks again!
Joe

How can I declare mProjectInstance

Nov 21 '05 #5
Joe,
It's really a weird occurrence and I can't explain to you why
changing the function to Public works. I've tried other visibilities
and also get an error, albiet a slightly different one, but the same
type of exception.
I don't know enough about VB .NET and the framework to give you a
more decisive answer. I wish that I could.

What I can suggest is to possibly rethink your design... Since you
want specific functions to occur based on the type of object that is
created, why don't you make a function that is overridable in a
function in the base class? The parent class can have a mustoverrides
function that is called and defined by both child classes. This will
let you declare a BaseClass object and call the function that exists in
both the child classes. After you initialize your base object into one
of its child objects, the proper function will be called.
I've modified your code and placed it below. Notice that I make the
function a mustoverride and I override the function in each of the
child classes. Also notice that the base class is a mustinherit. This
may not exactly work for you, but it's worth taking a look at and
possibly thinking of designing your application in a similar way. Let
me know how this goes. And if you have any more questions, feel free
to reply again. Good luck.

Module X
Public Sub main()
initialize(0)
TestA(0)
End Sub

Public MustInherit Class cProjectBase
Protected msg As String

Public MustOverride Sub TestA(ByRef aDummy As Integer)
End Class

Friend Class cProjectA
Inherits cProjectBase

Public Overrides Sub TestA(ByRef aDummy As Integer)
msg = "I'm cProjectA"
MsgBox(msg)
End Sub
End Class

Friend Class cProjectB
Inherits cProjectBase

Public Overrides Sub TestA(ByRef aDummy As Integer)
msg = "I'm cProjectB"
MsgBox(msg)
End Sub
End Class

Private mProjectInstance As cProjectBase

Public Sub initialize(ByVal aFlag As Integer)
If aFlag = 0 Then
mProjectInstance = New cProjectA
End If

If aFlag = 1 Then
mProjectInstance = New cProjectB
End If
End Sub

Public Sub TestA(ByRef aDummy As Integer)
mProjectInstance.TestA(aDummy)
End Sub
End Module

Nov 21 '05 #6
Joe,
One more thing. If you don't want to use mustinherit in your base
class (in case you have a reason to actually use the base class as an
object and not one of its subclasses), you can remove the mustinherit
and change the mustoverride method to a overridable method. You will
need to supply some logic for the base class method, but you will be
allowed to override that parent method in the sub class with its own
custom logic.
See the example of the three classes defined below.

Public Class cProjectBase
Protected msg As String

Public Overridable Sub TestA(ByRef aDummy As Integer)
msg = "I wasn't overrided"
MsgBox(msg)
End Sub
End Class

Friend Class cProjectA
Inherits cProjectBase

Public Overrides Sub TestA(ByRef aDummy As Integer)
msg = "I'm cProjectA"
MsgBox(msg)
End Sub
End Class

Friend Class cProjectB
Inherits cProjectBase

Public Overrides Sub TestA(ByRef aDummy As Integer)
msg = "I'm cProjectB"
MsgBox(msg)
End Sub
End Class

Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: sotto | last post by:
If i have this Interface: Public Interface MyInterface Function test() As Boolean Function test(ByVal MyVar As String) As Boolean End Interface And then i make a Public Class MyOwnClass
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
1
by: mersinli | last post by:
Hi, I have got seven error and I dont find why compiler find error.. //commission class definition #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string;
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.