views:

284

answers:

3

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.

+1  A: 

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.

Aamir
Whoops, that's some bad editing for release on my part - see my revised answer in a moment.
Jon Cage
Right, made the corrections - there's no default arguments, the full source has a couple of extras on every single method so that's not the issue. I'm thinking it has more to do with the fact that the checkbox's get and set aren't being passed through properly?
Jon Cage
Then make sure that the getter of 'Checked' property is returning a bool. Further, try casting it explicitly to (bool) to see if it works. BTW, I still don't understand how you can call a 2 argument method with 4 arguments?
Aamir
Tried casting it which still didn't help.
Jon Cage
OK, another attempt, if you have copy-pasted it, then the place where you are making a function call, after 2nd argument, there is a '.' instead of a ','. Is this the problem?
Aamir
Typos now fixed - keen eyes, but not the problem ;-)
Jon Cage
A: 

This isn't an answer to my question, but worth being aware of - apparently there's a quirk in passing properties by reference.

Jon Cage
+1  A: 

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.

Tal Pressman
That's the same conclus9ion I'd come to. Using the poperty would have been a really clean way of doing things, but since it's not exposing a changeable reference, I've written an interface which allows the GetConfig function to modify a value which can then set the checkbox.
Jon Cage