I have the following code:
private void btnOK_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbVendorName.Text))
{
VendorName = tbVendorName.Text;
if (!string.IsNullOrEmpty(rtbVendorAddress.Text))
{
VendorAddress = rtbVendorAddress.Text;
if (!string.IsNullOrEmpty(tbVendorEmail.Text))
{
VendorEmail = tbVendorEmail.Text;
if (!string.IsNullOrEmpty(tbVendorWebsite.Text))
{
VendorWebsite = tbVendorWebsite.Text;
this.Close();
}
else
{
MessageBox.Show("Vendor Website Required");
}
}
else
{
MessageBox.Show("Vendor email is required");
}
}
else
{
MessageBox.Show("Vendor address is required");
}
}
else
{
MessageBox.Show("Vendor name is required");
}
}
But it just looks horrible. Is there a better way? Or even an alternative methodology which makes the code more readable?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
The better way is to master the MVVM pattern. There you could create a ViewModel and define all input data there:
class VendorViewModel
{
[Required]
public string Name { get; set; }
public string Website { get; set; }
[Regex("regex for email")]
public string Email { get; set; }
[MaxLength(160)]
public string Address { get; set; }
}
Then the framework will show input errors (if any) after each textbox or any other input element. Enable property of the button will be automatically set according of all fields validated correctly.
Put it in a method and return from the method a boolean as to valid or not.
private void btnOK_Click(object sender, EventArgs e)
{
if(!Validate())
{
//Invalid
}
//Valid so set details
}
private bool Validate()
{
if (!string.IsNullOrEmpty(tbVendorName.Text))
{
MessageBox.Show(...);
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With