473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 mProjectInstanc e As Object

Sub initialize(ByVa l aFlag As Integer)
If aFlag = 0 Then
mProjectInstanc e = New cProjectA
End If

If aFlag = 1 Then
mProjectInstanc e = 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.WriteLi ne("TestA for cProjectA")
End Sub

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

Thanks,
Joe

Nov 21 '05 #1
6 1308
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.MissingM emberException: Method
'Module1.TestA' cannot be called with 2 argument(s).
at
Microsoft.Visua lBasic.Compiler Services.VBBind er.set_Internal Throw(Exception
Value)
at
Microsoft.Visua lBasic.Compiler Services.VBBind er.BindToMethod (BindingFlags
bindingAttr, MethodBase[] match, Object[]& args,
ParameterModifi er[] modifiers, CultureInfo culture, String[] names,
Object& ObjState)
at
Microsoft.Visua lBasic.Compiler Services.VBBind er.InvokeMember (String
name, BindingFlags invokeAttr, Type objType, IReflect
objIReflect, Object target, Object[] args, ParameterModifi er[]
modifiers, CultureInfo culture, String[] namedParameters )
at
Microsoft.Visua lBasic.Compiler Services.LateBi nding.InternalL ateCall(Object
o, Type objType, String name, Object[] args, St
ring[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at
Microsoft.Visua lBasic.Compiler Services.LateBi nding.LateCall( Object o,
Type objType, String name, Object[] args, String[] p
aramnames, Boolean[] CopyBack)
at ConsoleApplicat ion1.Module1.Te stA(Int32& aDummy) in
C:\...\ConsoleA pplication1\Mod ule1
..vb:line 38
at ConsoleApplicat ion1.Module1.ma in() in C:\...\Module1. vb:line 4

It crashes when it executes ...
TestA(aDummy, mProjectInstanc e)
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 mProjectInstanc e 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
mProjectInstanc e 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 mProjectInstanc e.GetType.Equal s(GetType(cProj ectA)) Then
TestA(3, CType(mProjectI nstance, cProjectA))
ElseIf mProjectInstanc e.GetType.Equal s(GetType(cProj ectB)) Then
TestA(3, CType(mProjectI nstance, 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 mProjectInstanc e 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 mProjectInstanc e 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 mProjectInstanc e

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 mProjectInstanc e As cProjectBase

Public Sub initialize(ByVa l aFlag As Integer)
If aFlag = 0 Then
mProjectInstanc e = New cProjectA
End If

If aFlag = 1 Then
mProjectInstanc e = New cProjectB
End If
End Sub

Public Sub TestA(ByRef aDummy As Integer)
mProjectInstanc e.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
1501
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
7375
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 number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
1
3013
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
6124
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
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.