|
Okay, the problem here seems to be that whatever you have that triggers the switch isn't being set to a neutral state. In other words, you are in a situation where you have Ability.TurnRight set, and then you're switching based on m_facing, which sets a case that gets triggered again immediately. What I would do, immediately after your switch statement, reset the variable that contains Ability.TurnRight.
|
|
|
|
|
Thanks for the response! Ok, that makes sense, but I thought the "break;" would prevent the command from continuing? (Still digging through the files)
And for more clarity I'm using a parent "MainChar" and child of "PacMan". I set up the array of "moves" (like exampled in my previous post) in the "MainChar" and I'm trying to issue the commands in "PacMan" so I can make him turn and move forward to collect all the pips before dying.
|
|
|
|
|
No, break is a keyword that is required in a switch statement to say that a particular piece of code has "finished". It doesn't reset any variables. Let me demonstrate with a simple example:
bool finished;
int i = 0;
while (!finished)
{
switch (i)
{
case 0:
Console.WriteLine("Loopy");
break;
case 1:
finished = true;
break;
}
} As you can see, this will never finish because i is never set to 1.
|
|
|
|
|
Thanks for the example. I'm starting to see what you mean by the "looping". To apply that to another ability, with the following example "Forward" PacMan will continue moving forward as long as the space in front of him is empty because the only check for success is that the space in "front" of him is empty, as determined by "LookForward"?
case Ability.Forward:
GameObject.Type objectTypeInDesiredPosition = LookForward();
if (objectTypeInDesiredPosition == GameObject.Type.Empty)
{
SetPosition(GetFowardPosition());
}
break;
|
|
|
|
|
The issue of "involuntary recursion" is sometimes something you can't avoid having to handle: for example, events of certain WinForm Controls will be called when a Form is initialized, which may trigger side-effects that need to be stopped.
If you can't easily re-design your flow-of-control to eliminate this problem, then a "fix" is to:
0. first test to see if setting 'm_facing is triggering the recursion: create a boolean variable in Form scope:
private bool dontRecurse = false;
private void MyMethod(...)
{
if(dontRecurse) return;
dontRecurse = true;
switch(...)
{
case Ability.TurnRight:
switch (m_facing)
{
case World.Direction.North:
m_facing = World.Direction.East;
break;
case World.Direction.South:
m_facing = World.Direction.West;
break;
case World.Direction.East:
m_facing = World.Direction.South;
break;
case World.Direction.West:
m_facing = World.Direction.North;
break;
}
}
dontRecurse = false;
} Observe if this fixes the problem: if it does not then:
1. using the debugger: set a break-point in the code where you the first change occurs: in your case that would in one of the Switch/Case clauses before 'm_facing is set.
2. run your code making sure you trigger a case where you hit the break-point, and then single-step (F11) from the break-point to isolate what is triggering the re-execution of the the case statement.
When you have a clear understanding of what triggers the recursion, then re-design or use boolean flag as necessary.
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
Thanks for the tips! I'll try to use this. ^^
|
|
|
|
|
I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#:
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA=");
string hex = BitConverter.ToString(bytes);
hex.Replace("-", String.Empty);
The 'hex' output is in the following format:
4D5459334D5445344D4467413D
However, required output should be in the format:
#44ff11 (example)
|
|
|
|
|
I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.
|
|
|
|
|
Apologies for any confusion.. I just gave sample values.
I am trying to get the values you mentioned programmatically.
|
|
|
|
|
I use Encoding.GetString ( System.Convert.FromBase64String ( Source ) ) , which yields TmpVME1EZ0E9 from your sample data.
|
|
|
|
|
And hex should be "4E6A55304D4467413D"
|
|
|
|
|
|
Thanks for your help. I was able to solve it. Following is what I ended up with:
byte[] decoded = Convert.FromBase64String("UUEncoded string to be decoded");
String value = ASCIIEncoding.ASCII.GetString(decoded);
int output = Convert.ToInt32(value);
// Convert int to hex format and reverse the byte
String.Format("{0:X02}{1:X02}{2:X02}", (output & 0x0000FF) >> 0, (output & 0x00FF00) >> 8, (output & 0xFF0000) >> 16);
|
|
|
|
|
Hi,
I would like to anyone tried to knows where to get details on how to post sales into Sage 50 Accounting using C# and sData[^]
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Have some google foo[^] have you had a rat through their doco?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Hello...My self Swati. I'm from rajkot..
I want some extra report's name for my project..my project name is "BILLING SYSTEM"..so please suggest the report name for my project.which helps to improve my marks in project..thank you..
|
|
|
|
|
How about:
[Course Instructor's Name] Off-Shore Account Report [Month] [Year]
Where you'd fill in the text in brackets above with the appropriate text data ?
No, seriously, how about just:
Billing System Report [report number] [Time-interval Year/Quarter/Month/Week/Day]
सौभाग्य
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
I'm sorry, but what does this have to do with C#?
|
|
|
|
|
You knocked on the wrong door.
|
|
|
|
|
I'm developing a Smart Array (it's a request I cannot use a List of int that I know it's easier because I made both codes). I have done this class and below is the example of how I use it. The error is often in this line (153 from class):
// Ensure that the lock is released.
Monitor.Exit(array);
If I use a List nothing wrong happens just when I translate to an array. Thanks for your help.
SmartArray3.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SmartArray
{
class SmartArray3
{
private int[] array;
private int size = 0;
private int count = 0;
public SmartArray3()
{
Resize(1);
}
public SmartArray3(int size)
{
this.size = size;
array = new int[this.size];
}
public bool Resize(int size)
{
try
{
if (array == null)
array = new int[size];
else
Array.Resize(ref array, size);
this.size++;
return true;
}
catch
{
return false;
}
}
private void add(int value)
{
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
throw new System.IndexOutOfRangeException("Index out of Range.");
}
}
public void Add(int value)
{
Monitor.Enter(array);
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
finally
{
Monitor.Exit(array);
}
}
public bool TryAdd(int value)
{
if (Monitor.TryEnter(array))
{
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
finally
{
Monitor.Exit(array);
}
return true;
}
else
{
return false;
}
}
public int Get(int index)
{
try
{
return array[index];
}
catch (IndexOutOfRangeException ex)
{
throw new System.IndexOutOfRangeException("Index out of range");
}
}
}
}
Code for called the Class:
private static int threadsRunning = 0;
private SmartArray3 sa = new SmartArray3();
private List times;
private static string[] titles ={
"Add ", "Add failed ", "TryAdd succeeded ", "TryAdd failed "};
private static int[][] results = new int[3][];
private void newTest()
{
for (int i = 0; i < 3; i++)
{
Thread t = new Thread(ThreadProc);
t.Start(i);
Interlocked.Increment(ref threadsRunning);
}
}
private void ThreadProc(object state)
{
times = new List();
DateTime finish = DateTime.Now.AddSeconds(10);
Random rand = new Random();
int[] result = { 0, 0, 0, 0};
int threadNum = (int)state;
while (DateTime.Now < finish)
{
Stopwatch sw = Stopwatch.StartNew();
int what = rand.Next(250);
int how = rand.Next(25);
if (how < 16)
{
try
{
sa.Add(what);
result[(int)ThreadResultIndex.AddCt] += 1;
times.Add(sw.Elapsed.TotalMilliseconds);
}
catch
{
result[(int)ThreadResultIndex.AddFailCt] += 1;
}
}
else
{
if (sa.TryAdd(what))
{
result[(int)ThreadResultIndex.TryAddSucceedCt] += 1;
}
else
{
result[(int)ThreadResultIndex.TryAddFailCt] += 1;
}
}
sw.Stop();
}
results[threadNum] = result;
if (0 == Interlocked.Decrement(ref threadsRunning))
{
StringBuilder sb = new StringBuilder(
" Thread 1 Thread 2 Thread 3 Total\n");
for (int row = 0; row < 4; row++)
{
int total = 0;
sb.Append(titles[row]);
for (int col = 0; col < 3; col++)
{
sb.Append(String.Format("{0,4} ", results[col][row]));
total += results[col][row];
}
sb.AppendLine(String.Format("{0,4} ", total));
}
Console.WriteLine(sb.ToString());
}
}
private enum ThreadResultIndex
{
AddCt,
AddFailCt,
TryAddSucceedCt,
TryAddFailCt
}
Federico Navarrete
|
|
|
|
|
The object you pass to Monitor.Exit needs to be the same object that you passed to Monitor.Enter / Monitor.TryEnter .
When you call your Resize method, you're setting the array field to a new array instance. You then pass that new instance to Monitor.Exit , even though you've never passed it to Monitor.Enter .
The simplest solution would be to use a separate read-only object for all Monitor calls:
class SmartArray3
{
private readonly object _locker = new object();
...
public void Add(int value)
{
Monitor.Enter(_locker);
try
{
...
}
finally
{
Monitor.Exit(_locker);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good spot!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi,
In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that.
I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times.
If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help.
Thanks
|
|
|
|
|
I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well.
How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code.
I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|