Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
want to ask about void type, so I could know that it works or no
in PHP i could have a condition like this:
if(mysql_query($query))
{ bla bla }
else
{ print error }
how to do like that on ASP.NET?
i'm trying like this:
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ print error }
but of course, it cannot be like that, because void isn't boolean
Usually void functions that do work that can fail will have some other way of informing you that they failed. Often they will throw an Exception:
try
{
k.EditPassword(...)
}
catch(ApplicationException ex)
{
// print Exception
}
Response.Redirect(...)
Other times they will set a status variable or something:
k.EditPassword(...)
if (k.Result == Result.OK)
Response.Redirect(...)
else
// print error...
Looking at documentation or source code for the conditions you are trying to handle is the only way to know how to handle it.
You can use a literal control and add your text to it. So your code will go something like,
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ Literal1.Text = error; }
Literal should be in design file, you can add it from toolbox.
But a better and proper way would be to,
Log it. (You would need a log mecahnism)
Write a unit test :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I have this code:
<div class="col-lg-12 control-section toast-default-section">
<SfToast ID="toast_default" #ref="ToastObj" Title="Error" Content="#a" </SfToast>
</div>
#code{
a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
When this "ToastObj" open, I need click 2 times to refresh. I don't know why.
For example:
First Result: Test
Second Result: Test Hello
I need click two times to refresh.
For the future, it would be helpful to see all the code, including the SFToast component.
However, You are calling it correctly. All you have to do to Get the page to refresh is to have the code hit the "StateHasChanged()" function. I think the problem is that you have code sitting outside of any function. Consider the #code{} to act like a normal class file with a few diffrences. For example there is no "Constructor" but the first method called on creation will be the OnInitalized(). I suggest wrapping the code and making sure all variables are initialized
#code{
protected override void OnInitialized()
{
var a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
}
Try use InvokeAsync(StateHasChanged);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I make a TextBox in C# to allow a maximum of one . (dot)?
Thus, abcdef and abc.def would be valid inputs whereas ab.cd.ef wouldn't.
By allow I mean that the user should not be able to enter a dot if there is already one in the text field.
Java has DocumentFilters for that purpose, is there something similar in C#?
I guess this is for validating user inputs. You should create a button and tell the user when he is done, press it so that you can check whether there is only one . in the string.
Assumptions:
Let your text box be called tb. Let your button's Click event handler be BtnOnClick.
Now we can start to write code. First create the handler:
private void BtnOnClick (object sender, EventArgs e) {
}
In the handler, you need to loop through the string and check each character. You can use a foreach for this:
int dotCount = 0;
string s = tb.Text;
if (s.StartsWith(".")) //starts with .
//code to handle invalid input
return;
if (s.EndsWith(".")) //ends with .
//code to handle invalid input
return;
foreach (char c in s) {
if (c == '.') {
dotCount++;
}
if (dotCount >= 2) { //more than two .
//code to handle invalid input
return;
}
}
// if input is valid, this will execute
Alternatively, you can use a query expression. But I think you are unlikely to know what that is.
string s = tb.Text;
if (s.StartsWith(".")) //starts with .
//code to handle invalid input
return;
if (s.EndsWith(".")) //ends with .
//code to handle invalid input
return;
var query = from character in s
where character == '.'
select character;
if (query.Count() > 1) {
//code to handle invalid input
return;
}
// if input is valid, this will execute
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an class like this (this is an example ) I need return whole of class from everywhere I need it
Line1 :Public void test(bool start)
Line2 :{
Line3 : if (start) //do something;
Line4 : else // i use else for update with help other code and... forget it
Line5 : {
Line6 : if (xxX)
Line7 : if (sadasdas)
Line8 : if(sdaeqwr324f)
Line9 : if (Asdasdsa)
Line10: for (int a=0;a<6;a++)
Line11: {
Line12: if (c[a]==1321334)
Line13: {
Line14: t=c[1];
Line15: /* <<< i neeeed reset trace of test to first line of test Line16 :>>>*/
Line17: }
Line18: }
Line19: there are about 400 lines codes like Up at under of this line }
Line20:}
My target is line 15 – if I add a return at The tracer jump to line 17 – I need tracer jump to line 2
Sorry for my bad English ( I search to find why to solve it but I cant find anythings )
To "break" from a function use return:
public void test(bool start)
{
if (start) //do something;
else // i use else for update with help other code and... forget it
{
//do somethings (it is loop )
pleas reset trace of test to first line of test
return; // This ends the method, and returns to the caller
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is the right way (if any...) to validate user input
This one (first throw the exception):
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item == null)
{
throw new ArgumentException("work flow item must have value");
}
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
Or this one (first do the action):
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item != null)
{
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
else
{
throw new ArgumentException("work flow item must have value");
}
}
Is there any guidelines?
There are no real guidelines or rules, but the first one is often preferred, because you can remove the else, removing one level of indention.
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item == null)
{
throw new ArgumentException("work flow item must have value");
}
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
Less indention makes for code that is easier to understand, especially in scenarios with multiple such checks.
Oh, and when checking a parameter for null you usually throw an ArgumentNullException with the parameter name as the first parameter:
throw new ArgumentNullException("item");
Like commentor stated i would go as follows:
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item == null)
{
throw new ArgumentException("work flow item must have value");
}
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
and doing validation at first is usually my preference. You need to check the correctness or the state or the parameter.
It's exactly the same to me and for as far as I know there are no guidelines to this.
For readability I'd suggest to put the eception first though.
E.g.
if (...) throw new Exception();
do your thing
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a method which used some nested if statements, so I would like to know if there is a better way to write the same logic.
For example I would like remove avoid the twice
_typologyRepository.Update(typology);
_typologyRepository.Save();
Could you point me out in the right direction? Thanks
public void Update(Typology typology, string nameOriginalValue)
{
if (typology.Name == nameOriginalValue)
{
_typologyRepository.Update(typology);
_typologyRepository.Save();
}
else
{
if (IsUniqueName(typology.Name))
{
_typologyRepository.Update(typology);
_typologyRepository.Save();
}
else
_validatonDictionary.AddError("Name", errorMessageNameUnique);
}
}
if (typology.Name == nameOriginalValue || IsUniqueName(typology.Name))
{
_typologyRepository.Update(typology);
_typologyRepository.Save();
}
else
{
_validatonDictionary.AddError("Name", errorMessageNameUnique);
}
RedFilter's answer is how it should be written. One other note about your code, though:
Usually, when people do cascading if/elses, they keep everything at the same indentation level. RedFilter's answer is better because you don't need cascading if/elses, but if you did need them, most people would write them like this:
public void Update(Typology typology, string nameOriginalValue)
{
if (typology.Name == nameOriginalValue)
{
_typologyRepository.Update(typology);
_typologyRepository.Save();
}
else if (IsUniqueName(typology.Name))
{
_typologyRepository.Update(typology);
_typologyRepository.Save();
}
else
_validatonDictionary.AddError("Name", errorMessageNameUnique);
}
}