似乎无法在VBA中将接口与属性一起使用,我不知道为什么 [英] Can't seem to use Interfaces with properties in VBA and I can't work out why

查看:40
本文介绍了似乎无法在VBA中将接口与属性一起使用,我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我对PHP,Java和VBA有一定的经验,但是我主要是前端开发人员,因此Javascript更是我的专长.

Disclaimer: I have a bit of experience with PHP, Java and VBA but I am mainly a front end developer so Javascript is more my thing.

我试图创建一些在MS Access数据库中实现接口的类,但我什至无法使这个概念起作用.

I am trying to create some classes which implement an Interface in an MS Access Database and I can't even get the concept to work.

我所拥有的:

Option Explicit
Public Name As String

Public Property Get Name() As String
End Property

Public Property Let Name(ByVal vNewValue As String)
End Property

CFeed类别

Option Explicit

Implements IFeed

Private Name As String

Private Property Let IFeed_Name(ByVal newName As String)
    Name = newName
End Property

Private Property Get IFeed_Name() As String
    IFeed_Name = Name
End Property

测试模块

Public Sub testFeedClass()
    Dim test As CFeed
    Set test = New CFeed
    test.Name = "New Feed" ' Doesn't work
    Debug.Print test.Name
End Sub

我花了将近一天的时间将私人变成公共,将公共变成私人,以及其他很多事情,但是我无法使它正常工作.有人可以指出我要去哪里了吗?我相信这很简单.

I have spend almost a day changing private to public, public to private and loads of other things but I just can't get it to work. Can someone please point out where I am going wrong? I am sure it is something simple.

推荐答案

在VBA中,已实现接口的成员不会使其成为类对象本身,例如它们在C#的隐式接口实现中完成.

In VBA members of implemented interfaces do not make it into the class object itself, like e.g. they do in C#'s implicit interface implementation.

要使用已实现的接口,您需要将对象强制转换为该接口:

To use the implemented interface, you need to cast the object to that interface:

Dim test As IFeed
Set test = New CFeed
test.Name = "New Feed"

这篇关于似乎无法在VBA中将接口与属性一起使用,我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆