Updating portion of byte array not working
-
You have a
try
without acatch
or finally, so I don't see how your code is compiling. Simplifying your problem:private int convertStringToByte(String theString, ref byte theByte)
{
theByte = System.Convert.ToByte(theString);
return 1;
}public void RunSnippet() { byte\[\] tempByteArr = new byte\[\]{1}; convertStringToByte("3", ref tempByteArr\[0\]); Console.WriteLine(tempByteArr\[0\]); }
The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:
public static int ConvertStringToByte(string s, out byte result)
{
try
{
theByte = System.Convert.ToByte(theString);} catch { return 0; //Or whatever - don't catch "all exceptions" like this! } return 1; }
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
You have a
try
without acatch
or finally, so I don't see how your code is compiling. Simplifying your problem:private int convertStringToByte(String theString, ref byte theByte)
{
theByte = System.Convert.ToByte(theString);
return 1;
}public void RunSnippet() { byte\[\] tempByteArr = new byte\[\]{1}; convertStringToByte("3", ref tempByteArr\[0\]); Console.WriteLine(tempByteArr\[0\]); }
The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:
public static int ConvertStringToByte(string s, out byte result)
{
try
{
theByte = System.Convert.ToByte(theString);} catch { return 0; //Or whatever - don't catch "all exceptions" like this! } return 1; }
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
My code actually has a catch for my try. I just didn't include it here to simplify. So it works for you basically as-is?
It is working. Given what you have said, the problem could well be in the
catch
. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
It is working. Given what you have said, the problem could well be in the
catch
. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.
Glad you found it!
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.
-
void Get_NameChangedToProtectTheInnocent_()
{
try
{
...
}
catch
{
//Bury the error, like the classic site
}
}There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
void Get_NameChangedToProtectTheInnocent_()
{
try
{
...
}
catch
{
//Bury the error, like the classic site
}
}There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^]Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)I was being a bit (OK, very) toungue-in-cheek. 99.99% of the time I really wouldn't do this. In this case he exception is very unimportant, for various reasons I'd have to write my own logging infrastructure (which would take longer than the rest of the change, and my code is slated for replacement by an MVC app very soon) and I can't handle in code as the try-block is calling out to an asmx service and I'm guarding against technical errors. I could, I suppose tighten which errors are being caught.
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^]