4 Replies - 1034 Views - Last Post: 14 May 2008 - 04:48 PM Rate Topic: -----

#1 schooler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 08

How can I shorten this code?

Post icon  Posted 13 May 2008 - 07:37 PM

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


Is This A Good Question/Topic? 0
  • +

Replies To: How can I shorten this code?

#2 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How can I shorten this code?

Posted 14 May 2008 - 09:42 AM

Moved to VB.NET forum.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

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.

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.
Was This Post Helpful? 0
  • +
  • -

#5 schooler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 08

Re: How can I shorten this code?

Posted 14 May 2008 - 04:48 PM

View PostZhalix, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1