Type Mismatch Error when Passing Child Type to Subroutine accepting Parent Type VBA










1














Context



I have a parent interface, IParent,



Option Explicit

Public Sub DoParentStuff()
End Sub


a child interface implementing IParent, IChild,



Option Explicit

Implements IParent

Private Sub IParent_DoParentStuff()
End Sub

Public Sub DoParentStuff()
End Sub


and a concrete implementation of IChild, CStandardChild.



Option Explicit

Implements IChild

Private Sub IChild_DoParentStuff()
End Sub

Public Sub DoParentStuff()
IChild_DoParentStuff
End Sub


I then created a module that passes a variable of type IChild to a subroutine with one parameter of type IParent.



Option Explicit

Private Sub Test(ByRef parent As IParent)
parent.DoParentStuff
End Sub

Public Sub Main()
Dim child As IChild

Set child = New CStandardChild

Test child
End Sub


I can compile the VBA project without error. But, when I run Main, I get a run-time error




Run-time error '13':



Type mismatch




The debugger points to the code Test child.



Question



Why am I getting a run-time, type-mismatch error? How can I pass child to Test() without getting this error?



What I've Tried



I've looked into casting IChild to IParent. However, I'm not using VB.NET, so, I don't have access to DirectCast and CType. In saying this, if I've implemented IParent and IChild appropriately, I didn't think a cast is necessary.










share|improve this question























  • I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
    – Comintern
    Nov 12 '18 at 22:37










  • Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
    – Joshua Daly
    Nov 12 '18 at 22:59















1














Context



I have a parent interface, IParent,



Option Explicit

Public Sub DoParentStuff()
End Sub


a child interface implementing IParent, IChild,



Option Explicit

Implements IParent

Private Sub IParent_DoParentStuff()
End Sub

Public Sub DoParentStuff()
End Sub


and a concrete implementation of IChild, CStandardChild.



Option Explicit

Implements IChild

Private Sub IChild_DoParentStuff()
End Sub

Public Sub DoParentStuff()
IChild_DoParentStuff
End Sub


I then created a module that passes a variable of type IChild to a subroutine with one parameter of type IParent.



Option Explicit

Private Sub Test(ByRef parent As IParent)
parent.DoParentStuff
End Sub

Public Sub Main()
Dim child As IChild

Set child = New CStandardChild

Test child
End Sub


I can compile the VBA project without error. But, when I run Main, I get a run-time error




Run-time error '13':



Type mismatch




The debugger points to the code Test child.



Question



Why am I getting a run-time, type-mismatch error? How can I pass child to Test() without getting this error?



What I've Tried



I've looked into casting IChild to IParent. However, I'm not using VB.NET, so, I don't have access to DirectCast and CType. In saying this, if I've implemented IParent and IChild appropriately, I didn't think a cast is necessary.










share|improve this question























  • I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
    – Comintern
    Nov 12 '18 at 22:37










  • Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
    – Joshua Daly
    Nov 12 '18 at 22:59













1












1








1







Context



I have a parent interface, IParent,



Option Explicit

Public Sub DoParentStuff()
End Sub


a child interface implementing IParent, IChild,



Option Explicit

Implements IParent

Private Sub IParent_DoParentStuff()
End Sub

Public Sub DoParentStuff()
End Sub


and a concrete implementation of IChild, CStandardChild.



Option Explicit

Implements IChild

Private Sub IChild_DoParentStuff()
End Sub

Public Sub DoParentStuff()
IChild_DoParentStuff
End Sub


I then created a module that passes a variable of type IChild to a subroutine with one parameter of type IParent.



Option Explicit

Private Sub Test(ByRef parent As IParent)
parent.DoParentStuff
End Sub

Public Sub Main()
Dim child As IChild

Set child = New CStandardChild

Test child
End Sub


I can compile the VBA project without error. But, when I run Main, I get a run-time error




Run-time error '13':



Type mismatch




The debugger points to the code Test child.



Question



Why am I getting a run-time, type-mismatch error? How can I pass child to Test() without getting this error?



What I've Tried



I've looked into casting IChild to IParent. However, I'm not using VB.NET, so, I don't have access to DirectCast and CType. In saying this, if I've implemented IParent and IChild appropriately, I didn't think a cast is necessary.










share|improve this question















Context



I have a parent interface, IParent,



Option Explicit

Public Sub DoParentStuff()
End Sub


a child interface implementing IParent, IChild,



Option Explicit

Implements IParent

Private Sub IParent_DoParentStuff()
End Sub

Public Sub DoParentStuff()
End Sub


and a concrete implementation of IChild, CStandardChild.



Option Explicit

Implements IChild

Private Sub IChild_DoParentStuff()
End Sub

Public Sub DoParentStuff()
IChild_DoParentStuff
End Sub


I then created a module that passes a variable of type IChild to a subroutine with one parameter of type IParent.



Option Explicit

Private Sub Test(ByRef parent As IParent)
parent.DoParentStuff
End Sub

Public Sub Main()
Dim child As IChild

Set child = New CStandardChild

Test child
End Sub


I can compile the VBA project without error. But, when I run Main, I get a run-time error




Run-time error '13':



Type mismatch




The debugger points to the code Test child.



Question



Why am I getting a run-time, type-mismatch error? How can I pass child to Test() without getting this error?



What I've Tried



I've looked into casting IChild to IParent. However, I'm not using VB.NET, so, I don't have access to DirectCast and CType. In saying this, if I've implemented IParent and IChild appropriately, I didn't think a cast is necessary.







excel vba interface runtime-error






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:00

























asked Nov 12 '18 at 22:32









Joshua Daly

3011312




3011312











  • I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
    – Comintern
    Nov 12 '18 at 22:37










  • Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
    – Joshua Daly
    Nov 12 '18 at 22:59
















  • I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
    – Comintern
    Nov 12 '18 at 22:37










  • Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
    – Joshua Daly
    Nov 12 '18 at 22:59















I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
– Comintern
Nov 12 '18 at 22:37




I'm not sure I understand the question. Are you just looking for a way to cast to an IParent in VBA? Just declare Dim foo As IParent: Set foo = child
– Comintern
Nov 12 '18 at 22:37












Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
– Joshua Daly
Nov 12 '18 at 22:59




Hi @Comintern, I'm trying to pass child to Test() without receiving a Type mismatch error. However, your comment may be a way to do it, I'll give it a shot, thanks.
– Joshua Daly
Nov 12 '18 at 22:59












1 Answer
1






active

oldest

votes


















1














If I understand correctly what you're trying to do, it looks like you're trying to extend IParent with IChild members. You can't do that in VBA - it would be awesome, but it's part of what makes .NET a more flexible framework to work with.



To take a C# analogy - this is what I think you're trying to do (and is illegal in VBA):



interface IFoo void DoSomething() 
interface IBar : IFoo void DoStuff() // inherits members of IFoo


If you need CStandardChild to be accessible through both IParent and IChild interfaces, you need both Implements statements in that class:



Option Explicit
Implements IChild
Implements IParent

'implement members of both interfaces...


Then you can pass an instance of that class and "cast" it to either interface.






share|improve this answer




















  • Thanks, this is great to know.
    – Joshua Daly
    Nov 12 '18 at 23:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271055%2ftype-mismatch-error-when-passing-child-type-to-subroutine-accepting-parent-type%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If I understand correctly what you're trying to do, it looks like you're trying to extend IParent with IChild members. You can't do that in VBA - it would be awesome, but it's part of what makes .NET a more flexible framework to work with.



To take a C# analogy - this is what I think you're trying to do (and is illegal in VBA):



interface IFoo void DoSomething() 
interface IBar : IFoo void DoStuff() // inherits members of IFoo


If you need CStandardChild to be accessible through both IParent and IChild interfaces, you need both Implements statements in that class:



Option Explicit
Implements IChild
Implements IParent

'implement members of both interfaces...


Then you can pass an instance of that class and "cast" it to either interface.






share|improve this answer




















  • Thanks, this is great to know.
    – Joshua Daly
    Nov 12 '18 at 23:05















1














If I understand correctly what you're trying to do, it looks like you're trying to extend IParent with IChild members. You can't do that in VBA - it would be awesome, but it's part of what makes .NET a more flexible framework to work with.



To take a C# analogy - this is what I think you're trying to do (and is illegal in VBA):



interface IFoo void DoSomething() 
interface IBar : IFoo void DoStuff() // inherits members of IFoo


If you need CStandardChild to be accessible through both IParent and IChild interfaces, you need both Implements statements in that class:



Option Explicit
Implements IChild
Implements IParent

'implement members of both interfaces...


Then you can pass an instance of that class and "cast" it to either interface.






share|improve this answer




















  • Thanks, this is great to know.
    – Joshua Daly
    Nov 12 '18 at 23:05













1












1








1






If I understand correctly what you're trying to do, it looks like you're trying to extend IParent with IChild members. You can't do that in VBA - it would be awesome, but it's part of what makes .NET a more flexible framework to work with.



To take a C# analogy - this is what I think you're trying to do (and is illegal in VBA):



interface IFoo void DoSomething() 
interface IBar : IFoo void DoStuff() // inherits members of IFoo


If you need CStandardChild to be accessible through both IParent and IChild interfaces, you need both Implements statements in that class:



Option Explicit
Implements IChild
Implements IParent

'implement members of both interfaces...


Then you can pass an instance of that class and "cast" it to either interface.






share|improve this answer












If I understand correctly what you're trying to do, it looks like you're trying to extend IParent with IChild members. You can't do that in VBA - it would be awesome, but it's part of what makes .NET a more flexible framework to work with.



To take a C# analogy - this is what I think you're trying to do (and is illegal in VBA):



interface IFoo void DoSomething() 
interface IBar : IFoo void DoStuff() // inherits members of IFoo


If you need CStandardChild to be accessible through both IParent and IChild interfaces, you need both Implements statements in that class:



Option Explicit
Implements IChild
Implements IParent

'implement members of both interfaces...


Then you can pass an instance of that class and "cast" it to either interface.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 23:01









Mathieu Guindon

41.1k762140




41.1k762140











  • Thanks, this is great to know.
    – Joshua Daly
    Nov 12 '18 at 23:05
















  • Thanks, this is great to know.
    – Joshua Daly
    Nov 12 '18 at 23:05















Thanks, this is great to know.
– Joshua Daly
Nov 12 '18 at 23:05




Thanks, this is great to know.
– Joshua Daly
Nov 12 '18 at 23:05

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271055%2ftype-mismatch-error-when-passing-child-type-to-subroutine-accepting-parent-type%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党