|
but if i am not writing this in catch block then also same problem is coming ..i just posted this in catch block to check what is the problem in my code ...i dont know much about it ....so see the code is it right ?or what could be the problems
|
|
|
|
|
For creating backups of MySQL databases and restoring them, I use the GUI tools. I tried to do it with code of my own, and run into lots of problems: the low quality of the documentation (with your case here: what does exit code/error code/ErrCode 1 mean?), characters outside the 7bit-ASCII range being changed into nonsense, etc. Consequently, I try to avoid MySQL nowadays, even though I was a fan of it in the past.
In case you have to stick with MySQL: try to restore the database with the MySQL GUI tools, perhaps you could get some better information there.
|
|
|
|
|
What would you expect foo to be after this code fragment runs? Post your answer without first peeking at the docs!
string badNumericString = "Bogus";
Decimal foo = Decimal.MinValue;
bool status = Decimal.TryParse (badNumericString, out foo);
Answer:Decimal.MinValue (since the parse failed)
- Something else (if so, what?)
/ravi
|
|
|
|
|
Well, ok I have peeked to see if the behaviour is the same as Int32.TryParse and it is. Importantly the out keyword requires that the variable passed as the argument is definitely assigned by the method and whether the method returns true or false has no bearing on that requirement.
So my answer is I would expect foo to be assigned but whether or not that changes the value depends on whether the method assigns MinValue or something else.
Alan.
|
|
|
|
|
Alan N wrote: the out keyword requires that the variable passed as the argument is definitely assigned by the method Yes, absolutely. And I was hoping TryParse() would assign the incoming value of foo when the parse failed. When it failed to do so, I decided to write my own SafeTryParse() implementation that would do just that, only to be chastised by the compiler - an out parameter is always assumed to be uninitialized and therefore its value can't be used.
/ravi
|
|
|
|
|
I did that test, only to get my wrist slapped too!
Alan.
|
|
|
|
|
Heh. So this is what I ended up creating:
public void SafeTryParse
(string stringValue,
Decimal? defaultValue,
out Decimal value)
{
if (!Decimal.TryParse (stringValue, out value) && defaultValue.HasValue) {
value = defaultValue.Value;
}
} /ravi
|
|
|
|
|
Or change out to ref and do this:
private bool SafeTryParse(string s, ref decimal result) {
decimal temp;
bool ok = Decimal.TryParse(s, out temp);
if (ok) result = temp;
return ok;
}
Alan
|
|
|
|
|
Great minds think alike.
/ravi
|
|
|
|
|
Hmm. That isn't very safe, result could contain an unintended value:
decimal val;
val=some_garbage;
someOperations(val);
bool OK=SafeTryParse("foo", ref val);
One shouldn't give two roles to a single variable, so I'd rather have two overloads both using the out keyword:
private bool SafeTryParse(string s, out decimal result);
private bool SafeTryParse(string s, out decimal result, decimal dflt);
You can still get the "overwrite when parse succeeds" effect by passing <ocde>val twice to the latter.
|
|
|
|
|
I would argue that forgetting to set a valid initial value is programmer error, however I now like the idea of an explicit default. That's what I initially implemented (link[^]), but like Alan, switched to the 2 parameter ref version. I'm going to switch back.
Thanks,
/ravi
|
|
|
|
|
Then I prefer to provide a value; something like this:
public virtual T
ExecuteScalar<T>
(
T IfNull
)
{
you could do similar for TryParse.
|
|
|
|
|
|
Do we actually care, if the value isn't being used?
string badNumericString = "Bogus";
Decimal foo;
if (!Decimal.TryParse (badNumericString, out foo)) foo = 1;
..and if the TryParse don't use the value, it'll probably initialize it with the same value it initializes an empty variable. Since the variable hasn't been set (according to application logic), we can't be sure about the value unless we explicitly set it. (Future versions of .NET might display other behavior)
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote: it'll probably initialize it with the same value it initializes an empty variable
Double checked using Reflector...
public static bool TryParse(string s, out decimal result)
{
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
}
internal static unsafe bool TryParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt, out decimal result)
{
byte* stackBuffer = stackalloc byte[0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
result = 0M;
if (!TryStringToNumber(value, options, ref number, numfmt, true))
{
return false;
}
if (!NumberBufferToDecimal(number.PackForNative(), ref result))
{
return false;
}
return true;
}
... so it WILL be zero.
|
|
|
|
|
Eddy Vluggen wrote: Do we actually care
Not particularly, but we care whether or not it does what the documentation says.
|
|
|
|
|
Eddy Vluggen wrote: Do we actually care, if the value isn't being used? It is being used. The example I gave was intentionally limited to focus on the question. In my app, foo (not its real name) is used after the TryParse() executes. Different things happen depending on whether foo is Decimal.MinValue .
/ravi
|
|
|
|
|
That is what the doc[^] says. This entire thread seams to suggest no-one reads or beliefs it.
|
|
|
|
|
Luc Pattyn wrote: no-one reads or beliefs it
Maybe I'm a one-percenter.
My understanding is that all TryParse methods are expected to set the value to zero /null /default(T) when they fail -- so that's what I do when I write a Tryxxx method.
public bool
TryParse
(
string Name
,
out T Value
)
{
bool result = true ;
Value = default(T) ;
public static bool
TryGetValue<T>
(
this object Source
,
out T Value
)
{
bool result = false ;
Value = default(T) ;
|
|
|
|
|
I agree.
|
|
|
|
|
You are right in assuming I didn't read the doc.
/ravi
|
|
|
|
|
|
I think foo would be zero, but that's just a guess. I think Int32.TryParse and Double.TryParse set the out parameter to zero if the parse fails, but I don't recall.
If TryParse fails, I usually set the value to some known default that I can handle - I don't care what TryParse sets it to, especially is MS decides to change it down the road.
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
You're absolutely correct.
dybs wrote: If TryParse fails, I usually set the value to some known default that I can handle I agree that's the advisable thing to do.
/ravi
|
|
|
|
|
I need urgent a class in c# that can sort the data from a textfile
For Example I have a file data.txt
AAAAA
BBBBB
CCCCC
AAAAA
BBBBB
and the sort to be like that
AAAAA
AAAAA
BBBBB
BBBBB
CCCCC
please help me I need it so much...thank you
|
|
|
|