Public Class frmCustomerFiles Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click If Me.txtEnterFileName.Text.Equals("") = True Then MessageBox.Show("No Customers exist") Return End If If Me.txtEnterFileName.Text.Equals("Alfred") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Cynthia") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Ernie") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Hubert") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Kathryn") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Nicholas") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Patricia") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Sally") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Terrance") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Valerie") = True Then MessageBox.Show("Customer already exists") Return End If If Me.txtEnterFileName.Text.Equals("Xavier") = True Then MessageBox.Show("Customer already exists") Return End If
How can I shorten this code?need help with code
Page 1 of 1
4 Replies - 1314 Views - Last Post: 14 May 2008 - 04:48 PM
#1
How can I shorten this code?
Posted 13 May 2008 - 07:37 PM
Replies To: How can I shorten this code?
#2
Re: How can I shorten this code?
Posted 13 May 2008 - 08:24 PM
You could put all the names into an array and whenever someone clicks it'll loop through each value in the array and check to see if the text entered matches any of the names in the array. That would drastically shorten the code.
#3
Re: How can I shorten this code?
Posted 14 May 2008 - 09:42 AM
Moved to VB.NET forum.
#4
Re: How can I shorten this code?
Posted 14 May 2008 - 09:56 AM
At the very least, you can move the basic check logic that repeats into it's own function, like so.
You would presumably have a list of existant customers. If you had such a list, you should be able to loop through it for the check, which would be the prefered way.
Hope this helps.
Function HasCustomer(ByVal custName As String) As Boolean If Me.txtEnterFileName.Text.Equals(custName) Then MessageBox.Show("Customer already exists") Return True Else Return False End If End Function Sub Main() If HasCustomer("Alfred") Then Return If HasCustomer("Cynthia") Then Return End Sub
You would presumably have a list of existant customers. If you had such a list, you should be able to loop through it for the check, which would be the prefered way.
Hope this helps.
#5
Re: How can I shorten this code?
Posted 14 May 2008 - 04:48 PM
Zhalix, on 13 May, 2008 - 08:24 PM, said:
You could put all the names into an array and whenever someone clicks it'll loop through each value in the array and check to see if the text entered matches any of the names in the array. That would drastically shorten the code.
Ok can you please show me how to do that..not a versed programmer.
It would require an array and an if statement? Please show
Thanks
Page 1 of 1