|
You are testing the value of formatFailed in your try block, but you set it inside the catch block. This is not the way to code a try catch sequence. You should be displaying the failure message in the catch block, and break out of the loop only when the parsing succeeds.
Use the best guess
|
|
|
|
|
Richard MacCutchan wrote: You are testing the value of formatFailed in your try block, but you set it inside the catch block. This is not the way to code a try catch sequence. You should be displaying the failure message in the catch block, and break out of the loop only when the parsing succeeds.
Richard
Actual code of the function is 150-200 lines with lots of Reflection and stuff. I had only posted tail portion of code which was the problem area. What happens is as follows. For each field, I have custom attribute that spells out what format is going to be present which I call tryFirstFormat. So, there is one another parseexact before code enters loop. If it enters the loop, it is 100% certain that formatFailed will be set to 1. Fortunately, C# will not let me compile without initialized variable antway.
Lastly, Message that gets logged inside try block is actually to report a successful parse after blah-blah number of attempts. Message in catch block is when parsing fails where attempt count increases
Tonight, I spent time thumbing thru few other sites and decided to change the loop index (i) to be a volatile member of class Will discover how this performs tomorrow.
|
|
|
|
|
You will also have a problem with the first two of your format strings when the month and day are both less than 13.
Use the best guess
|
|
|
|
|
Richard MacCutchan wrote: You will also have a problem with the first two of your format strings when the month and day are both less than 13.
Actually, I did have that problem when I was using TryParse and using locale.
What I've done is following. For each datetime field, I've added custom attribute that spells out which format is most likely to be most appropriate for that particular field. This way, on some fields, I do instruct it to read it as dd/mm and on others I do ask it to read mm/dd.
|
|
|
|
|
I would address this a different way, assuming each stream is identifiable and is internally consistent, I would register a format for each source.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: I would address this a different way, assuming each stream is identifiable and is internally consistent, I would register a format for each source.
They are consistent in a sense that if mm was before dd, it does seem to remain that way ... but I'd be in trouble if I assume that it will always be slash / between characters. Sometimes it is dash or space too. So, I weed out all unwanted characters using String.Replace and replace them with what slashes as I have
'em on my format string before even attempting to parse the date.
Further, I do have custom attribute defined for each and every date/time field which essentially leads to one of the eight format strings as "tryFirstFormat". So, yes! I do have one format registered for each field although I know that expecting it to remain consistent is a bit-too-much at this point in time....
In the code, before the loop, I use parseExact to parse the date. Naturally that too is in its own try-catch block. Only if that fails, code will enter the loop.
I did not post all of the above code assuming that more code will confuse people and felt that it was not relevant to problem. But it seems from the early responses that partial code is confusing ppl more.
modified 7-Aug-13 1:02am.
|
|
|
|
|
Will it help if I publish entire parsing code? ... Certainly there is little bit of refactoring that can reduce the lines which I've postponed until it works correctly?
I had omitted code above the for loop because (a) it is working fine (as I coded it). (b) I did not want to post too big code here
|
|
|
|
|
Hi,
I'm developing window application ...
My question is simple, i'm getting form name in a string...
now i want to open the form using string name
how to convert string to form object in c# window application
please reply me.........
modified 6-Aug-13 3:44am.
|
|
|
|
|
Some variation of this
Assembly assembly = System.Reflection.Assembly.GetAssembly();
classAsType = assembly.GetType(classNameAsString);
|
|
|
|
|
Have you created your form object already? where you want to open that form, can you tell how you want to do, i hope can help you with that
|
|
|
|
|
susanna.floora wrote: My question is simple, i'm getting form name in a string...
The name of a form-object (name property) or the name of the forms' class? In other words, does the form already exist that you're looking for, or are you showing a new instance of that form?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Form already exists in the application.
form name is in a string.
how do i open that form.
|
|
|
|
|
Assembly assembly = System.Reflection.Assembly.GetAssembly();
classAsType = assembly.GetType(<big>classNameAsString</big>);
classNameAsString as string is where you will type in your form's name as string. After this you obtain constructor using reflection and show the class
|
|
|
|
|
Simple:
var t = System.Type.GetType("MyClass.FormName");
var form = Activator.CreateInstance(t);
|
|
|
|
|
|
Welcome, upvote it.

|
|
|
|
|
I usually don't comment on a question like this, when the OP has a made a comment of the form "it works," in response to a solution.
But, I'd like to note that, imho, Eddie Vluggen asked what I think is the critical question here:
"The name of a form-object (name property) or the name of the forms' class? In other words, does the form already exist that you're looking for, or are you showing a new instance of that form?"
To which the OP responded:
"Form already exists in the application.
form name is in a string.
how do i open that form."
The code shown in the response by Jean Brandelero will create a new instance of the Form based on its class-name. And, perhaps, that is exactly what the OP wanted.
But, it also seems possible the OP does not want to create a new instance, but to "open" an existing Form.
In that case there are two alternatives:
a. the Form instance is already open, and is visible: your done.
Whether an instance of a Form is visible, or hidden, it's in the Application.OpenForms collection. Note that the collection has a very limited set of operators: you can't use something like 'Contains. And, each Form in that collection is in a "vanilla" type-form: you can't automatically assign it to a variable of the type of FormX: you have to cast the result to ProjectName.FormX.
b. the Form instance is now hidden: your job is to 'Show it.
So, now you can define the alternatives here clearly: based on whether, as Eddie was pointing out, the "string" you have contains a Class Name of a Form, or the string contains the name of a form already created as an instance.
You can use Brandelero's code to create a new instance, if that's what you want, and it doesn't matter to you if there already any other instances of the Type that exist.
And, if you want to create an instance only if an instance does not already exist, or you just want to make sure an existing instance is visible: you can mess around with the Application.OpenForms collection to see what's what.
Let's look at simple test case:
private Form2 newForm2 = new Form2();
private Form2 newForm2a = new Form2();
newForm2.Name = "newForm2";
newForm2.Show();
newForm2.Hide();
newForm2a.Name = "newForm2a";
newForm2a.Show();
newForm2a.Hide();
foreach (Form theCandidateForm in Application.OpenForms)
{
if (theCandidateForm.Name == "newForm2")
{
Console.WriteLine("Found match by Name: " + theCandidateForm.Name);
}
if( theCandidateForm.GetType() == newForm2.GetType())
{
Console.WriteLine("Found match by Type " + theCandidateForm.Name);
}
} The output of this in the VS 'Output window would be:
Found match by Name: newForm2
Found match by Type newForm2
Found match by Type newForm2a
I hope this information adds something to this thread.
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
|
I have this code for autocomplete:
foreach(string match in myList){
if( !string.IsNullOrEmpty(text_box1) ){
if( match.StartsWith(text_box1.ToString(),
StringComparison.CurrentCultureIgnoreCase) ){
MessageBox.Show(match);
}
}
}
In String "The follow me", case find "the", the string is show me. But case find "follow" or "Follow", is not show. I'm tried with Contains but not solved. What can i do ?
|
|
|
|
|
You are using StartsWith . If you look for "The follow", you will find the string output.
What did you try with Contains ?
|
|
|
|
|
With Contains, when find "follow" is show the complete string. But i try with "the fol" no show more, and "the Follow", example, to not show.
|
|
|
|
|
|
Solved the problem with "||":
if( match.StartsWith(text_box1.ToString(),
StringComparison.CurrentCultureIgnoreCase) || match.Contains(text_box1) )
Tanks for all help
|
|
|
|
|
I'm writing some DLL's and have a question.
If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects.
Example:
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
cmd.Dispose();
if (conn != null)
conn.Dispose();
}
IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?
|
|
|
|
|
The finally block will almost always execute - let all dispose methods remain in the finally block.
|
|
|
|