如何使用 CLI 通过引用传递复选框值?
我有一个用 C++/CLI 编写的 GUI 应用程序,其中包含大量可配置选项。 我有一些重载的函数,它们从我的数据源中获取值,我想将我的选项连接到这些值。
所以这里有几个数据检索器:
bool GetConfigSingle(long paramToGet, String^% str, char* debug, long debugLength);
bool GetConfigSingle(long paramToGet, bool^% v_value, char* debug, long debugLength);
我希望传入复选框的 Checked
getter/setter,如下所示:
result = m_dataSource->GetConfigSingle(CONFIG_OPTION1, this->myOption->Checked, debug, debugLen);
...但由于某种原因,我收到一个奇怪的编译器错误,表明 Checked
值没有按我的预期传递:
1>.\DataInterface.cpp(825) : error C2664: 'bool DataInterface::GetConfigSingle(long,System::String ^%, char*, long)' : cannot convert parameter 2 from 'bool' to 'System::String ^%'
以前此代码传递了复选框并修改了值本身,但我渴望打破我们的数据收集当前对 Windows 窗体的依赖关系。
那么我在这里缺少什么?
[编辑] 我已经按照原来的方式填写了函数定义,以避免混淆 - 我尝试减少不相关信息的尝试失败了。
I have a GUI app written in C++/CLI which has a load of configurable options. I have some overloaded functions which grab values from my data source and I'd like to connect my options to those values.
So here's a couple of data retrievers:
bool GetConfigSingle(long paramToGet, String^% str, char* debug, long debugLength);
bool GetConfigSingle(long paramToGet, bool^% v_value, char* debug, long debugLength);
I was hoping to pass in the checkbox's Checked
getter/setter as follows:
result = m_dataSource->GetConfigSingle(CONFIG_OPTION1, this->myOption->Checked, debug, debugLen);
...but for some reason I get an odd compiler error which suggests the Checked
value isn't being passed as I'd expect:
1>.\DataInterface.cpp(825) : error C2664: 'bool DataInterface::GetConfigSingle(long,System::String ^%, char*, long)' : cannot convert parameter 2 from 'bool' to 'System::String ^%'
Previously this code passed the checkbox in and modified the values itself, but I'm keen to break the dependency our data collection currently has on windows forms.
So what am I missing here?
[Edit] I've filled out the function definitions as they originally were to avoid confusion - my attempt to reduce the irrelevent information failed.
I'm fairly certain that the CheckBox getter / setter returns a bool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是我问题的答案,但值得注意 - 显然有 通过引用传递属性时的一个怪癖。
This isn't an answer to my question, but worth being aware of - apparently there's a quirk in passing properties by reference.
我想我会澄清上面的评论,并使其成为“真正的”答案...
当您调用 Checked 时,您返回的返回值是一个 bool 值,表示 CheckBox 的当前状态。 然而,它不是对保存复选框状态的实际数据成员的引用。 事实上,正确封装的类不应该允许访问它。 此外,由于 Checked 按值返回 bool,因此该 bool 是一个临时对象,在调用 GetCongigSingle 时不一定存在。
这给您留下了多种选择。 要么按值传递 bool,然后设置 CheckBox 的状态,要么按引用传递 CheckBox 本身,然后在任意位置“检查”它。
Figured I'd clarify my comments from above and make it a "real" answer...
When you call Checked, what you're getting back as a return value is a bool that represents the current state of the CheckBox. It is not, however, a reference to the actual data member that holds the CheckBox's state. In fact, a properly encapsulated class shouldn't give access to it. Furthermore, since Checked returns a bool by value, that bool is a temporary object that doesn't necessarily exist by the time GetCongigSingle is called.
This leaves you with several options. Either pass the bools by value, and later set the CheckBox's state, or pass the CheckBox itself by reference and "check" it wherever you want.
您提到的方法
GetConfigSingleFile
的两个重载都采用两个参数,而您向该方法传递了 4 个参数。 有默认参数吗? 如果是,您能否重现原始方法声明?最有可能的是,此方法的 4 个参数重载需要
String^%
作为第二个参数。 无论如何,这就是编译器的建议。 但如果我们能看一下方法声明,就可以帮助诊断问题。The two overload of the method
GetConfigSingleFile
that you have mentioned both take two arguments whereas you are passing 4 arguments to the method. Are there any default arguments? If yes, can you please reproduce the original method declarations?Most probably, the 4 argument overload of this method is expecting a
String^%
as the 2nd argument. This is what the compiler is suggesting anyway. But if we can have a look at the method declarations that could help diagnosing the problem.