How to increment a number and output to label - c#-4.0

The program should count the number of correct words typed (duh), and after 60 seconds, print that value to a little label.
Does it? No. It counts to 1 and then refuses to increment.
But (and this is the fun part) when I run it in Debug mode with a breakpoint, it all works fine.
I can only figure it is some sort of variable encapsulation error (which still makes no sense).
Here's a little snippet:
private void checkWord()
{
if (txtInput.Text.ToLower() == lblQuery.Text.ToLower())
{
score++;
}
}
And here is the whole source because why not...
Programming Project.zip
http://tinyurl.com/c4af2nd

I believe you are getting white space in your comparison string. Try this to see if it works.
private void checkWord()
{
if (txtInput.Text.ToLower().Trim() == lblQuery.Text.ToLower())
{
score++;
}
}

Related

Get the GazeProvider.GazeTarget when GaxePointer is always on

In our app we have the gaze pointer behaviour set to be AlwaysOn:
PointerUtils.SetGazePointerBehavior(PointerBehavior.AlwaysOn);
This seems to break the GazeProvider because when a hand is detected, this:
CoreServices.InputSystem.GazeProvider.GazeTarget
returns the object actually hit by the hand rays, instead of the object at which we are gazing (could be null).
This is the code I use to get the position of the hit:
if (CoreServices.InputSystem.GazeProvider.GazeTarget?.layer == 31)
{
Debug.Log(CoreServices.InputSystem.GazeProvider.HitInfo.point);
}
But it returns the position of the hand cursor and not the gaze
I also tried filtering by SourceType(Head) but the problem persists:
foreach (var source in CoreServices.InputSystem.DetectedInputSources)
{
if (source.SourceType == InputSourceType.Head && CoreServices.InputSystem.GazeProvider.GazeTarget?.layer == 31)
{
foreach (var p in source.Pointers)
{
if (p is IMixedRealityPointer)
{
Debug.Log("HIT");
}
}
}
}
So here is the question:
When the GazePointer is set to be always visible, how can we get the position of the gaze hit even if a hand is detected?
To get the raw data of eye gaze and hand gaze at the same time, you can use InputRayUtils class, which provides the TryGetRay method that can get the ray associated with the desired input source type and hand. And if you have any question about how to use it, you can find an example in the InputDataExample scene under Assets/MRTK/Examples/Demos/Input/Scenes/InputData.

The value is not taken from the dictionary in JMH when thread is 2?

For a JMH class, Thread number is limited to 1 via #Threads(1).
However, when I get the number of threads using Thread.activeCount(), it shows that there are 2 threads.
The simplified version of the code is below:
#Fork(1)
#Warmup(iterations = 10)
#Measurement(iterations = 10)
#BenchmarkMode(Mode.AverageTime)
#OutputTimeUnit(TimeUnit.MICROSECONDS)
#Threads(1)
public class MyBenchmark {
#State(Scope.Benchmark)
public static class BState {
#Setup(Level.Trial)
public void initTrial() {
}
#TearDown(Level.Trial)
public void tearDownTrial(){
}
}
#Benchmark
public List<Integer> get(BState state) {
System.out.println("Thread number: " + Thread.activeCount());
...
List<byte[]> l = new ArrayList<byte[]>(state.dict.get(k));
...
}
}
Actually, the value is tried to get from the dictionary using its key. However, when 2 threads exist, the key is not able to get from the dictionary, and here list l becomes [].
Why the key is not taken? I limit the thread number because of this to 1.
Thread.activeCount() answers the number of threads in the system, not necessarily the number of benchmark threads. Using that to divide the work between the benchmark threads is dangerous because of this fundamental disconnect. ThreadParams may help to get the benchmark thread indexes, if needed, see the relevant example.
If you want a more conclusive answer, you need to provide MCVE that clearly highlights your problem.

using a for each loop with pointers

First of I have 2 Classes in 2 files (both .h and .cpp files), Create.h and AI.h.
Create.h has this struct in it:
public:
struct Cell
{
//some stuff
vector<Cell*> neighbors;
State state;
};
Here is the enum class State (stored in the Create.h file):
enum class State : char
{
//some states like "empty"
};
Now in AI.cpp I have a function like this:
void AI::Function(Create::Cell cell)
{
for each (Create::Cell* var in cell.neighbors)
{
if (var->state == State::empty)
{
}
}
}
So basically I am trying to access each individual Cell which is stored in cell.neighbors with a for each so I can do some stuff to each one of them.
According to my debugger though it doesn't even reach the if (var->state == State::empty) part. Am I using the for each wrong?
EDIT: The neighbors vector has definitely elements in it
If you are compiling with optimizations enabled, then an empty if statement like that might be completely removed (it has no side-effects).
(Although, I think the debugger won't let you set a breakpoint on that line, if it were removed. So this is an easy test -- try to set a breakpoint on the if itself.)
it seems that the vector is empty. You can check this printing its size before the loop.
And I would like to answer some comments. This form of the for loop is MS VC++ language extension. It is not C++/CLI.

some doubts in the following code

have a look at the code below once and help me out by clarifying my doubts.
I have commented my doubts on each lines where i have doubts. Moreover, its a part of code from a huge one. so please ignore the variable declarations and all.
The whole code is working perfect and no errors while compiled.
double Graph::Dijkstra( path_t& path )
{
int* paths = new int[_size];
double min = dijkstra(paths); // **is a function call or not? bcz i didn't found any function in the code**
if(min < 0) { delete[] paths; return -1;}
int i = _size - 1;
while(i>=0)
{
path.push(i); // **when will the program come out of this while loop, i'm wondering how does it breaks?**
i=paths[i];
}
path.push(0);
delete[] paths;
return min;
}
Full coding is available here.
double min = dijkstra(paths); // **is a function call or not? bcz i didn't found any function in the code**
It almost certainly is. However, it could be a free function, member function, function invoked by a macro, or something else. Without seeing the rest of the code, we can only guess.
while(i>=0)
{
path.push(i); // **when will the program come out of this while loop, i'm wondering how does it breaks?**
i=paths[i];
}
The program will come out of the loop as a soon as i is less than zero. If I had to guess, I'd say the each node in the path contains a link to the previous node's index with the last node in a path returning -1 or some other negative number.

simple counter in windows forms application C++

Just practicing my vs 2010 C++ in Windows Form Applications, I haven't done this for a very long time. I am trying to develop a simple applications where a user presses a button and then label1 begins to count everytime users presses that button1. Not sure why label1 is not incrementing by 1. Can someone tell what the problem is? Thanks in advance?
EDITED
I have found the solution and I have amended the code. I will try to close the thread and if I can't, because of I have low points, I will then try tomorrow.
namespace Counter
{
int counter = 0;
//some additional namespaces
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
counter++;
label1->Text = counter.ToString();
}
The reason why this isn't working is twofold.
The way you've written this, you only set the label text once, after you finish your "loop" (*). So the text will only change once.
Even if you move the assignment inside the loop, you're keeping the main thread busy throughout the whole function. What you want is to spawn a second thread and invoke a delegate to change the label text, something like this (C# version):
void StartCounting()
{
var thread=new Thread(()=>
{
for(int i=0;i<10;++i)
label1.Invoke((MethodInvoker)()=>{label1.Text=i.ToString();});
}
}
(*) As a side note, your entire for loop is equivalent to absolutely nothing. j will never be less than i when i starts as 0.

Resources