Method Return Issue
-
Wednesday, August 01, 2012 2:34 AM
Hi,
I know this is an easy one but it has been bothering me for quite a while. In my winforms application I am calling
protected string IsNull(Control c) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); } else { EP.SetError(c, ""); } return c.Text.Trim(); }I am calling this method for every textbox which the user is required to fill like:
Name=IsNull(txtName);
What I want to accomplish is, when there is no input in the txtName, simply the ErrorProvider is invoked, without returning the c.Text.Trim() which obviously will be blank. I could have easily accomplished this if I was not returning the value using return like:
protected void IsNull(Control c) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); return; } else { EP.SetError(c, ""); } }
But since I need to return the value and the return after c.Focus() is causing the problem, I don't know what to do.
Any suggestions??
Regards,
ZB
All Replies
-
Wednesday, August 01, 2012 3:47 AM
Just do this:
protected void IsNull(Control c) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); return string.Empty; } else { EP.SetError(c, ""); return string.Empty; } return ……; } -
Wednesday, August 01, 2012 4:18 AMPerhaps I'm missing something, but if you are calling the IsNull(...) in sequence (during validation or processing data from the form), only a single control can have the focus at a time.
David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.
-
Wednesday, August 01, 2012 4:51 AM
1st of all, if you return a string (even if its an empty one), your method must be of a string return type (like your 1st example).
Next, on every possible step of the code, it must retun something, like:
protected string IsNull(Control c) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); return null; } else { EP.SetError(c, ""); return c.Text.Trim(); } }
Mitja
-
Wednesday, August 01, 2012 4:52 AM
How about this...
protected string IsNull(Control c, string defaultName) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); return defaultName; } else { EP.SetError(c, ""); } return c.Text.Trim(); } Name = IsNull(txtName, Name);
-
Wednesday, August 01, 2012 5:24 PM
Thanks a lot everybody. Every one of you came up with a nice solution but I don't know if I am not getting the point or unable to communicate my concern. Let me try to explain it again.
Lets agree on the fact that the IsNull method needs to return a string if the string is not null and it contains valid values. But what in case the string is empty and this portion executes:
if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); }
At this point why would I even need to return anything (should that be default name like Ante Meridian suggested, string.Empty or null). Since at this point I know it is null or empty thats why this portion of code executed and all I want at this point is to set the error in Error provider and show it to user without further executing the logic. Why am I forced to return something (null or some default string) when it makes no sense, since my interest is to pop error rather than return something.
Hope I have explained my concern
Best Regards
ZB
-
Wednesday, August 01, 2012 5:34 PM
There is no necessary to return anything. If you dont need anything on the other side (from where you call this IsNull method) then make method of void retun type (so no return of any types).
If so, use your 2nd code snippet from your 1st post a bit modified:
protected void IsNull(Control c) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); } else { EP.SetError(c, ""); } }
Mitja
-
Wednesday, August 01, 2012 6:50 PM
If you do not want to return a value and assign it, then pass a reference of your object to your method and only make the assignment if it's not null or empty... probably rename your method to something like ValidateAndSet(...).
See the following for information on passing by reference parameters in C#: http://msdn.microsoft.com/en-us/library/14akc2c7(v=vs.71).aspx
Sample Code
// Using the method ValidateAndSet(txtName, ref Name); protected void ValidateAndSet(Control c, ref string controlString) { if (string.IsNullOrEmpty(c.Text.Trim())) { EP.SetError(c, "Required"); c.Focus(); } else { EP.SetError(c, ""); controlString = c.Text.Trim(); } }
David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.
- Edited by David K. Downing Wednesday, August 01, 2012 6:50 PM
- Edited by David K. Downing Wednesday, August 01, 2012 7:09 PM
- Edited by David K. Downing Wednesday, August 01, 2012 7:40 PM
- Marked As Answer by ZedBee Wednesday, August 01, 2012 9:38 PM
-
Wednesday, August 01, 2012 9:46 PM
Thanks Mitja for trying to help me out. No doubt the solutions that you suggested are definitely great but in my situation the solution suggested by David is perfect, since I did need to get some output from the method and since I wanted only the else part to return it, using ref to get the value rather than using return type (string) seems a good option to me.
Thanks every one for looking into the problem and for your kind suggestions.
Regards,
ZB
-
Wednesday, August 08, 2012 3:15 PMIf you don't want to return something, you can throw an exception.

