I am trying to store data in a two dimensional dictionary with the programming language CLI/C++.
However, I am facing problems adding something into them.
This is my attempt which works in C#:
Dictionary<int, Dictionary<int, string>> MyDictionary;
if (!MyDictionary.ContainsKey(100))
MyDictionary.Add(100, new Dictionary<int, string>());
MyDictionary[100][1234] = "Hello World";
Now - my CLI/C++ attempt:
Dictionary<int, Dictionary<int, String^>^>^ MyDictionary = gcnew Dictionary<int, Dictionary<int, String^>^>();
//ContainsKey check here
MyDictionary[100][1234] = "Hello World"; //<--- ERROR
MyDictionary[100, 1234] = "Hello World"; //<--- ERROR
It seems that you don't add something into the Dictionary using [][]. I saw people using [100, 1234] instead of [100][1234] but that doesn't work either.
Error:
function "System::Collections::Generic::Dictionary<TKey, TValue>::default[TKey]::set [with TKey=int, TValue=System::Collections::Generic::Dictionary<int, String ^> ^]" cannot be called with the given argument list
argument types are: (int, int, String^)
object type is: System::Collections::Generic::Dictionary<int, System::Collections::Generic::Dictionary<int, String ^> ^> ^
What am I doing wrong?
You are not doing anything wrong. Note that the code compiles just fine. This is a bug in the IntelliSense parser. It has several, the EDG front-end was originally designed to parse C++ and isn't exactly bug-free when it needs to tackle C++/CLI code.
You probably don't want to ignore these false diagnostics, they are pretty annoying. The least painful workaround is:
auto item = MyDictionary[100];
item[1234] = "Hello World";
Makes no difference in speed after the optimizer is done with it.
Related
I have the following function from a dll:
aisgdll_setinfo(int dev, set_field_code field, void *data);
I know how to deal with the first two parameters. I have a textbox the user enters data into and the textbox returns a variable of type String^. I somehow need to get the data from that textbox and do something, so that I can write it to this function to the void *data parameter.
You can use this form.
I used the Thread :: Sleep to prevent abuse in processing. While your thread does not change the status of the screen will be updated.
void solve()
{
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Threading;
Thread^ bot_thread = gcnew Thread(gcnew ThreadStart(bot_run));
bot_thread->Start();
PictureBox^ PB_LoadGIF = gcnew PictureBox();
PB_LoadGIF->Visible = true;
while (bot_thread->ThreadState == ThreadState::Running)
{
Thread::Sleep(1000);
PB_LoadGIF->Parent->Refresh();
}
}
After further research online, I have found some solutions to the problems I was having. The functions I was plugging the retrieved values into needed pointers. I think these solutions can be adapted by others for their own purposes. For converting the Visual Studio Text box String^ to char*:
char* iData = (char*)Marshal::StringToHGlobalAnsi(activeBox->Text).ToPointer();
When I needed to get a floating point number from a Text box, I used the following:
int anInteger = (int)((Convert::ToDouble(activeBox->Text))*10);
int *iData = &anInteger;
i have a Antlr generated Listener, and i call my tree walker to go through the tree from a parse function in another class. Looks like this:
public double calculate(){
ANTLRInputStream input = new ANTLRInputStream("5+2");
Lexer lexer = new Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Parser parser = new Parser(tokens);
ParseTree tree = parser.calculate();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new Listener(), tree);
return 0;
}
So the listener works perfect with the enter() and quit() Functions and prints the correct value in the end:
public void exitParser(ParserContext ctx) {
result = stack.peek();
System.out.println(result);
}
But i wanna receive the final value in my calculate() function to return it there. Since exitParser(...) is void i dont know how to deal with it.
With the visitor i was able to do it like that:
public double calculate(){
// ...
String value = new WRBVisitor().visit(tree);
return Double.parseDouble(value);
}
Hope someone understands my problem and knows a solution for it.
Best regards
As mentioned in the comments: a visitor might be a better option in your case. A visitor's methods will always return a value, which is what you seem to be after. That could be a Double if your expressions always evaluate to a numeric value, or some sort of home-grown Value that could represent a Double, Boolean, etc.
Have a look at my demo expression evaluator (using a visitor) on GitHub: https://github.com/bkiers/Mu
I'm wondering if there is a possibility to translate following old Rust code:
bytes!("a\u2028t")
Into current language. It seems bytes! was deprecated by b"" but I don't see a way to translate \u2028 into a byte string literal.
If you want a true byte string equivalent, you'll need to find the UTF8 encoding of U+2028, e.g. via
fn main() {
for b in "\u2028".as_bytes().iter() { print!("\\x{:x}", *b) }
}
which prints \xe2\x80\xa8 (i.e. in pre-encoded form), so b"a\xe2\x80\xa8t" should work. Also, the above hints at another method: you can often use "a\u2028t".as_bytes(), although this will not work in static contexts.
I am a developer in C-like languages (Java/JavaScript/C#) and I am attempting to convert some Objective-C code into Java.
For the most part, it is relatively straightforward but I have hit a stumbling block with the following bit of code:
typedef struct {
char *PAGE_AREA_ONE;
char *PAGE_AREA_TWO;
char *PAGE_AREA_THREE;
} CODES;
- (CODES*) getOpCode {
CODES *result = NULL;
result = malloc(sizeof(CODES));
result->PAGE_AREA_ONE = "\x1b\x1b\x1b";
result->PAGE_AREA_TWO = "\x2d\x2d\x2d";
result->PAGE_AREA_THREE = "\x40\x40";
return result;
}
What would the Java equivalent of this be? From what I can tell in other areas of the code, it is being used to store constants. But I am not 100% certain.
Thanks.
The typedef is just creating a structure that contains three string properties. The getOpCode method is apparently trying to create a new structure and assign values to those three properties. C# code would be:
public class Codes
{
public string PageAreaOne;
public string PageAreaTwo;
public string PageAreaThree;
}
public Codes GetCodes()
{
Codes result = new Codes();
result.PageAreaOne = "\x1b\x1b\x1b"; // three ESC characters
result.PageAreaTwo = "---";
result.PageAreaThree = "##";
return result;
}
The code in question is allocating a block of memory that the size of the CODES structure, filling it with some data, and returning a pointer to the new block. The data is apparently some operation codes (that is, instructions) for something, so perhaps the data is being sent to some other device where the instructions will be executed.
import java.util.Scanner;
public class Program3_5
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
String input = new String();
System.out.println("Please enter a string: ");
input=scan.next();
int length;
length = input.length;
input.substring();
System.out.println(charAt(0));
while (length)
{
System.out.println(charAt(0 + 1));
}
}
}
I am getting an error stating that it "cannot find symbol - variable length"
I have tried numerous things yet I am having trouble getting it to work. New to Java! Thanks in advance.
For example if the user were to input: Hello There
The Output would print the letters on separate lines.
String#length() is a method, not a field, of String. You need to call the method. In Java, methods are called (or "invoked") using parentheses. So, change
length = input.length;
// to
length = input.length();
Anticipating the next compile error you see:
while (length)
won't compile in Java because length is an int, but the condition part of a while must be a boolean. I'm guessing you want to continue as long as the string is not empty, so change the while condition to be
while (length > 0)
Other problems you'll need to solve to get your code to compile:
String#substring() requires integer arguments
Also, the code will compile with the String input = new String(); but the assignment is completely unnecessary. In Java, you almost never need to new a string. Instead, use string literals.