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);
}
}
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 1 year ago.
Improve this question
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{
if (false)
{
Console.WriteLine("hold UP");
}
++x;
Console.WriteLine(x);
} while (false);
}
}
}
That's the compiler warning you that some part of your code will never be executed. In your case it refers to this part.
if (false)
{
Console.WriteLine("hold UP");
}
A similar thing would happen in cases like this:
void Test()
{
return;
// more code here.
}
//or
void Test2()
{
while(true) //loop forever.
{ }
// more code here.
}
Console.WriteLine("hold UP"); is unreachable because if (false) will never be true, thus the code in the following block can never be executed.
Andy is correct. For the code to run inside an If statement the conditions must be true. What you have there is a static condition of False, thus it will not enter into the If statement.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I dont know what to do, im new to whole coding and the project im working on is going quite well, tried adding more commands and this problem jumps to the first private part here. If needed i can send the rest of the code
RegisterYNCommand();
{
}
private void RegisterYNCommand();
{
{
Commands.CreateCommand("YN")
.Do(async (e) =>
{
int yesnoIndex = rand.Next(randomTexts.Length);
string memeToPost = yesno[yesnoIndex];
await e.Channel.SendMessage(memeToPost);
});
Remove the first
RegisterNYCommand();
{
}
Looks like duplicated code...
and the ; at the end of
private void RegisterNYCommand(); // This ';'
{
Commands.CreateCommand("YN")
.Do(async (e) =>
{
int yesnoIndex = rand.Next(randomTexts.Length);
string memeToPost = yesno[yesnoIndex];
await e.Channel.SendMessage(memeToPost);
});
}
Just remove ; character after your methods declaration:
private void RegisterYNCommand()
{
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
Is this a write way?suggest me.
public Init_Circle Init_Circle(Point pt,double rad)
{
Point center=pt;
if (rbCircle.Checked==true)
{
pt.x = double.Parse(txtCirCntPtX.Text.Trim());
pt.y = double.Parse(txtCirCntPtY.Text.Trim());
rad = double.Parse(txtCirRadius.Text.Trim());
}
return this.Init_Circle(pt,rad);
}
You can't return anything from the constructor, but what you can do is create a static method that creates the object for you.
So in your constructor do it like this.
private Init_Circle()
{
}
and create a method like this
public static Init_Circle CreateInstance(// parameters here)
{
// do object creation here
}
You should create a method which will returns object. You can't return anything in a constructor.
Method should look like this:
public Init_Circle ReturnCircle(Point pt,double rad)
{
Point center=pt;
if (rbCircle.Checked==true)
{
pt.x = double.Parse(txtCirCntPtX.Text.Trim());
pt.y = double.Parse(txtCirCntPtY.Text.Trim());
rad = double.Parse(txtCirRadius.Text.Trim());
}
return this.Init_Circle(pt,rad);
}
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 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 :)