Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,557 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,911 people online right now. Registration is fast and FREE... Join Now!




How can I shorten this code?

 
Reply to this topicStart new topic

How can I shorten this code?, need help with code

schooler
13 May, 2008 - 06:37 PM
Post #1

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 6

CODE
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

User is offlineProfile CardPM
+Quote Post

Zhalix
RE: How Can I Shorten This Code?
13 May, 2008 - 07:24 PM
Post #2

D.I.C Head
**

Joined: 7 May, 2008
Posts: 218



Thanked: 9 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How Can I Shorten This Code?
14 May, 2008 - 08:42 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Moved to VB.NET forum.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: How Can I Shorten This Code?
14 May, 2008 - 08:56 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
At the very least, you can move the basic check logic that repeats into it's own function, like so.

CODE

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.

User is offlineProfile CardPM
+Quote Post

schooler
RE: How Can I Shorten This Code?
14 May, 2008 - 03:48 PM
Post #5

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 6

QUOTE(Zhalix @ 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.


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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:20PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month