Solved

Check Values in an Array if Null

Posted on 2008-10-18
7
1,177 Views
Last Modified: 2012-05-05
I'm trying to simplify my coding by putting all of my requirements in ONE potential line of code instead of assigning if functions to each silly variable i have, which leads me to my question.

I have a UI with text boxes, some of which can be left empty, some which cannot. What I'm looking for is a way to add an array containing those variables that cannot be empty, and if this is possible, for the program to check each variable (text box) for any indication that it's null, if so, throw up a msgbox stating the exact box which is empty before it writes anything to the db.

I'm currently working with a few basic text boxes. FirstName, LastName, DOB, Address, PhoneNumber.

Any help would be greatly appreciated!!
0
Comment
Question by:Arka3L
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22750005
WinForms or WebForms?  ...and what version VB.Net?
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22750078
WinForm and 2005, sorry.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22750413
You could do something like this:
(...there are many ways to approach this though!...)
Public Class Form3
 

    Private RequiredEntries() As String = {"LastName", "DOB", "PhoneNumber"}
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim tb As TextBox

        Dim ctl As Control

        For Each ctlName As String In RequiredEntries

            ctl = GetControlByName(ctlName)

            If Not IsNothing(ctl) AndAlso TypeOf ctl Is TextBox Then

                tb = DirectCast(ctl, TextBox)

                If tb.Text.Trim.Length = 0 Then

                    MessageBox.Show(ctlName, "Missing Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    tb.Focus()

                    Exit Sub

                End If

            End If

        Next
 

        ' ...if you get here then the TextBoxes have been properly filled out...

        MessageBox.Show("All Required Entries have been Filled Out")

    End Sub
 

    Private Function GetControlByName(ByVal ctlName As String) As Control

        Dim matches() As Control = Me.Controls.Find(ctlName, True)

        If matches.Length > 0 Then

            Return matches(0)

        Else

            Return Nothing

        End If

    End Function
 

End Class

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Arka3L
ID: 22750634
Wow! That was absolutely perfect and more than I even expected. It's almost a perfect solution to my problem.

However just two things, 1 which I can't figure out.

I'm not sure which version you are using but the  

Private RequiredEntries() As String = {"LastName", "DOB", "PhoneNumber"}

Had to be

Dim RequiredEntries() As String = {"LastName", "DOB", "PhoneNumber"}

It wasn't happy with the Private at all, it was unusable locally..? Now I understand that we're telling the compiler that the names i've given to the text boxes, which are also my variables, are stated in the initial RequiredEntries.

However, when it comes to returning the result to the MsgBox stating that we've missed a field, it uses the name of our Variable on the MsgBox e.g. FirstName. Because this is the variable and is being contained in ctlName, is there a way that I can change the text to First Name? DOB to Date of Birth etc.

It's extremely complicated for me reading all this at first but i'm slowly grasping the concept.

Thank you for your help!
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22750793
Ignore my incompetence with the first problem i referenced with the private vs dim, it was inside the button sub instead of it being outside lol.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22751194
Again, lots of ways to do it...here is one:
Public Class Form1
 

    Private RequiredFields As New Dictionary(Of String, String)
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim values() As String

        Dim RequiredEntries As String = "LastName,Last Name|DOB,Date of Birth|PhoneNumber,Phone Number"

        For Each pair As String In RequiredEntries.Split("|")

            values = pair.Split(",")

            RequiredFields.Add(values(0), values(1))

        Next

    End Sub
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim tb As TextBox

        Dim ctl As Control

        For Each ctlName As String In RequiredFields.Keys

            ctl = GetControlByName(ctlName)

            If Not IsNothing(ctl) AndAlso TypeOf ctl Is TextBox Then

                tb = DirectCast(ctl, TextBox)

                If tb.Text.Trim.Length = 0 Then

                    Dim msg As String = RequiredFields.Item(ctlName)

                    MessageBox.Show(msg, "Missing Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    tb.Focus()

                    Exit Sub

                End If

            End If

        Next
 

        ' ...if you get here then the TextBoxes have been properly filled out...

        MessageBox.Show("All Required Entries have been Filled Out")

    End Sub
 

    Private Function GetControlByName(ByVal ctlName As String) As Control

        Dim matches() As Control = Me.Controls.Find(ctlName, True)

        If matches.Length > 0 Then

            Return matches(0)

        Else

            Return Nothing

        End If

    End Function
 

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Arka3L
ID: 22751246
Absolute God send!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now