cannot convert from 'cli::array<Type> ^' to 'cli::array<Type> ^[]' - visual-studio-2005

I'm pretty new to C++/CLI and I am trying to convert a System::String to a System::Char array.
Here's what I have so far:
private: System::Void modeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Mode frmMode;
if(frmMode.ShowDialog() == System::Windows::Forms::DialogResult::OK){
array <Char>^ load [] = gcnew array<Char>(txtbxName->Text->ToCharArray());
}
}
txtbxName is a textbox inside a the form. Supposedly, this should work, but I get the compiler error:
error C2440: cannot convert from 'cli::array<Type> ^' to 'cli::array<Type> ^[]'
for the fourth line of the snippet.

ToCharArray is already giving you the array, you don't need to create a new one. In addition, as the other answerer noted, you don't need to specify [] to create the array variable, array<> is sufficient.
array<Char>^ load = txtbxName->Text->ToCharArray();

You are attempting to assign a reference to an array (array<Char> ^) to a C-style array of references to arrays (array<Char> ^ []).
You most likely intended this line instead:
array <Char>^ load = gcnew array<Char>(txtbxName->Text->ToCharArray());

Related

storing a function that was retrieved from FunctionCallbackInfo

I'm pretty much trying to make a AddInputEvent but, after a month, can't find a way to turn a local "function from FunctionCallbackInfo"(i'll just call this argf) in to a Persistent Function so that garbage collection doesn't erase the pointers.
Most stakeoverflow threads and example code I can find just say to Cast argf with a Local Function; then to throw that in to a Persistent New. This results in a error: cannot convert 'v8::Local<v8::Function>' to 'v8::Function*'
here is the code, not completely sure why I can't convert it
class inputevnt_feld{
public:
char* call_on;
v8::Persistent<v8::Function> func;
};
int entvcount = -1;
vector<inputevnt_feld> event_calls; //this is pretty much a array of events that we can call later
// in js looks like this "AddInputEvent("string", function);"
void AddInputEvent( const v8::FunctionCallbackInfo<v8::Value>& args ) {
v8::HandleScope handle_scope(args.GetIsolate());
//gotta make sure that we ain't letting in some trojan horse that has nothing in it
if (args[1]->IsFunction() && args[0]->IsString()) {
inputevnt_feld newt;
//converts js string to char array
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
const char* cstr = ToCString(str);
newt.call_on = (char*)cstr;
//here is where the problem is with function casting
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
newt.func = v8::Persistent<v8::Function>::New(args.GetIsolate(), callback);
//push the new stuff in to even array
event_calls.push_back(newt);
//getting vector array size is too much for my smol brain
//so I'ma just do this myself
entvcount++;
//cout << event_calls[entvcount].call_on << endl; //debug
}
}
Most stakeoverflow threads and example code I can find just say to Cast argf with a Local Function; then to throw that in to a Persistent New
Yes, that's correct. If you know how to read it, the C++ type system is your friend for figuring out the details.
If you look at the definition of v8::PersistentBase<T>::New, you'll see that it takes a T* (for its template type T). If you look at the v8::Local<T> class, you'll see that a way to get a T* from it is to use its operator*. That leads to:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>::New(..., *callback);
Alternatively, you can use the Persistent constructor directly, and pass it the Local without dereferencing it first:
v8::Local<v8::Function> callback = ...Cast(args[1]);
... = v8::Persistent<v8::Function>(..., callback);
Both options are entirely equivalent. Personally I'd prefer the latter as it takes slightly fewer characters to spell out, but that's really the only difference.
(Your current code as posted does something else: it ignores the result of the cast and passes the original args[1] directly to Persistent::New -- that's not going to work.)

Using processing and getting an unexpected token on my array. #hackathon

String[] files= {};
int[] fileNumber = {0};
String commandPromptTxt = "";
String CPTDummy = "";
String blankDummy = "";
String[] currentFile = {};
void makeFile(String[] file, int fileNum, String name1, int level1, int[]parents1, int[] children1, String type1) {
//Warning if you make a file and use the same file number more than once you will override the file
files[fileNum]= {"10"};
};
So I have that amazing piece of code in processing and I am getting an error unexpected token:{ where I say files[fileNum] = {}; also even when I enter values into the brackets I get the same error. Any ideas of a fix for this? Thanks.
Why are you including brackets at all?
The syntax you're using is an array initializer. You use it correctly here:
String[] files= {};
This initializes your files variable to an empty array. You also use the syntax correctly here:
int[] fileNumber = {0};
This initializes your fileNumber variable to an array with a single index, and in that index is the value 0.
This line is where it stops making sense:
files[fileNum]= {"10"}
First of all, you've already initialized your files variable to an array with zero indexes. That means that even if this would compile, you'd get an ArrayIndexOutOfBoundsException, because you're trying to use indexes of an array that doesn't have any.
Secondly, you're misusing the array initialization syntax. I'm pretty sure you don't want the indexes of your array to also be arrays, otherwise you'd have to make them 2D arrays.
So, to sum it up, you need to do two things:
1: Initialize your arrays to actually have indexes. Something like this:
String[] files = new String[10]; //array with 10 indexes
2: Stop misusing the array initialization syntax and just pass values into the array indexes:
files[fileNum]= "10";
You might be better off using ArraysLists instead though. Then you don't need to know how many indexes you'll have ahead of time, and you can simply call the add() function to add stuff to them.
More info can be found in the Processing reference.

C++/CX way of iterating Map<String^, Object^>^?

I have a object of type of Map<String^, Object^>^. How do I iterate in C++/CX way? I am trying to use iterator but I am not clear about syntax. Documentation doesn't provide an example.
C++/CX collections follow the same principles as c++ collections, so they have iterators and begin, end functions.
IMap<Platform::String^, Platform::Object^>^ map = ref new Map<Platform::String^, Platform::Object^>();
map->Insert("key1", "val1");
map->Insert("key2", 2.0f);
// Exactly like you would iterate over a map, but instead of std::pair you have IKeyValuePair
std::for_each(begin(map), end(map), [](IKeyValuePair<Platform::String^, Platform::Object^>^ pair)
{
// do stuff
auto key = pair->Key;
auto value = pair->Value;
});
for( auto pair : map )
{
// do stuff
auto key = pair->Key;
auto value = pair->Value;
}
Also, don't forget to include collection.h and use namespace Platform::Collections.

KeyTyped key not showing key

I am trying to convert from processing to processingjs and have something I just can't understand.
In processing the following code returns whichever letter you type in, though in processingjs it just comes back with the keycode value but I need the letter not the code.
String name="";
void setup(){
size(200,200);
}
void draw(){
}
void keyPressed() {
if(key==ENTER) {
name="";
}
else {
name+=key;
println(name);
}
}
After hours of searching and the above answer I've found the answer here peepproject.com/forums/thread/266/view more eloquently than I. Basically the solution is to convert the int to a char() before constructing a String and putting it into an array.
Instead of name += key, try name += key.toString().
Processing's println automatically does type conversion for you, so the char value of PApplet.key gets printed as a letter. JavaScript string concatenation works differently; the integer value of PApplet.key will be appended to the string as an integer and will not automatically be converted to a string. You have to do it manually.
You need to use the char conversion function in Processing and Processing.js:
http://processingjs.org/reference/char_/
The reason why it's displaying as a number is this line:
char x = 97; //supposed to be an 'a'
Does a cast in Java (may require char x = (char)97).
However in processing.js it is executed as:
var x = 97;
Since javascript has dynamic typing. You therefore need to explicitly force type casts such as from int->char.

ConcurrentDictionary->AddOrUpdate in VS C++

i am using Visual Studio 2010 C++ Express and i mant to add an item to my ConcurrentDictionary:
i have such code:
String^ key = gcnew String("key");
int value = 123;
myDictionary->AddOrUpdate(key,value,/*WHAT TO ADD HERE?*/);
AddOrUpdate Method takes 3 argument, not like normal Dictionary 2.
Microsoft sites says it takes such arguments:
public:
TValue AddOrUpdate(
TKey key,
TValue addValue,
Func<TKey, TValue, TValue>^ updateValueFactory
)
on microsoft sites i also found code in C#:
cd.AddOrUpdate(1, 1, (key, oldValue) => oldValue + 1);
but it does not work in C++. what i must put as 3rd argument?
The third parameter is a delegate, which in the C# sample code you found is a lambda. However C++/CLI does not support lambdas, so you'd have to do it with a standalone method.
static int UpdateFunc(String^ key, int value)
{
return value + 1;
}
cd->AddOrUpdate("foo", 1, gcnew Func<String^, int, int>(MyClass::UpdateFunc));
However, you said "I want to add an item to my ConcurrentDictionary". There's no simple "add" method, because it's always the case that other thread may have modified the ConcurrentDictionary. Therefore, there are a couple choices for how to put stuff in the dictionary.
AddOrUpdate: Add a value, or modify an existing value if that key already exists. (Passes the current value to a delegate, which returns the modification.)
GetOrAdd: Add a value, or retrieve the existing value if that key already exists. (Doesn't modify the dictionary if the key already exists.)
this[] (Indexer, uses square brackets): Add a value, or replace the existing value with a constant value.
If all you want is a simple 'add', it's probably the square brackets you're interested in.
cd["foo"] = 1;

Resources