Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array contains checking in vb.net

Posted on 2016-08-05
10
Medium Priority
?
35 Views
Last Modified: 2016-08-05
Hello,

Can it be done any better:

   Dim  Arr3 As New ArrayList
  Dim SQLdr As SqlDataReader
  Dim StrCol3 As String

    If StrCol3 <> String.Empty Then
                    If IsDBNull(SQLdr(StrCol3)) = False Then
                        StrColval3 = SQLdr(StrCol3)
                        StrColval3 = StrColval3.Trim
                        If StrColval3 <> String.Empty Then
                            'Check for duplicates in array
                            If Not (Arr3.Contains(StrColval3)) AndAlso Not (Arr3.Contains(StrColval3.ToLower)) Then
                                If Not (Arr3.Contains(StrColval3.ToUpper)) Then
                                    Arr3.Add(SQLdr(StrCol3))
                                End If
                            End If
                            End If
                    End If
                End If

Cheers
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41744112
https://msdn.microsoft.com/en-us/library/yw84x8be(v=vs.110).aspx

You could use the Array.Exists method, supply your own comparison function that converts the array member and what is being checked for  to both upper (or lowercase) so that

                            If Not (Arr3.Contains(StrColval3)) AndAlso Not (Arr3.Contains(StrColval3.ToLower)) Then
                                If Not (Arr3.Contains(StrColval3.ToUpper)) Then
                                    Arr3.Add(SQLdr(StrCol3))
                                End If
                            End If

can be replaced by
                                If Not (Arr3.Exists(YourComparisonFunctionGoesHere(StrColval3.ToUpper))) Then
                                    Arr3.Add(SQLdr(StrCol3))
                                End If


ps.  Arrays are fairly outdated, consider using another type of container - depends on just what you want to do
0
 

Author Comment

by:RIAS
ID: 41744115
Cheers! What is the alternative to array?
0
 

Author Comment

by:RIAS
ID: 41744116
YourComparisonFunctionGoesHere can you give a suggestion?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41744125
Comparison:  There is an example in the link I supplied.

Alternative:  Depends on what you require.  A List<T> is often good but under certain requirements a dictionary or queue or stack.....
0
 

Author Comment

by:RIAS
ID: 41744153
Ok mate will give a try and get back !
0
 

Author Comment

by:RIAS
ID: 41744184
Andy ,
It says Exists is not a member of arraylist?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41744338
Erm.  Exists should be a member of Array, but I see you talked of an array in the question title but used an arraylist instead in the question.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 total points
ID: 41744344
You could use a BinarySearch instead of Exists:
https://msdn.microsoft.com/en-us/library/b4y1ah87(v=vs.110).aspx
and use a case insensitive comparer.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 1200 total points
ID: 41744387
RIAS,

ArrayList has a Contains method which uses the Object.Equals method to compare the objects in the array with the object specified in the parameter.  My knock against using an ArrayList, though, stems from the fact that ArrayList Objects are not type specific.  Proof of concept -
Module Module1
	ReadOnly list As New ArrayList From
		{
			New Byte(),
			1,
			12.46F,
			2.38D,
			"Jeffrey",
			New Byte(10)() {}
		}

	Sub Main()
		If list.Contains(2.38F) Then
			Console.WriteLine("List has a {0} type with a value of {1}", 2.38F.GetType(), 2.38F)
		Else
			Console.WriteLine("List does not have a {0} type with a value of {1}", 2.38F.GetType(), 2.38F)
		End If

		If list.Contains(2.38D) Then
			Console.WriteLine("List has a {0} type with a value of {1}", 2.38D.GetType(), 2.38D)
		Else
			Console.WriteLine("List does not have a {0} type with a value of {1}", 2.38D.GetType(), 2.38D)
		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG
Instead, I would recommend you use one of the collections in System.Collections.Generic.  Most likely List<T> (for VB.NET that is List(Of T)) as alluded to by Andy previously.

Using a List(Of String) we could really improve your code; e.g. -
Function FillList(column As String) As List(Of String)
	Dim list As New List(Of String)
	Dim SQLdr As SqlDataReader
	Dim result As String

	If column <> String.Empty Then
		If Not IsDBNull(SQLdr(column)) Then
			result = SQLdr(column)
			result = result.Trim
			If Not String.IsNullOrEmpty(result) AndAlso Not list.Contains(result, StringComparer.OrdinalIgnoreCase) Then
				list.Add(result)
			End If
		End If
	End If
	Return list
End Function

Open in new window

Ofcourse if you are using .NET 4 or higher, then you can get rid of the Trim and use String.IsNullOrWhitespace instead; e.g. -
Function FillList(column As String) As List(Of String)
	Dim list As New List(Of String)
	Dim SQLdr As SqlDataReader
	Dim result As String

	If column <> String.Empty Then
		If Not IsDBNull(SQLdr(column)) Then
			result = SQLdr(column)
			If Not String.IsNullOrWhiteSpace(result) AndAlso Not list.Contains(result, StringComparer.OrdinalIgnoreCase) Then
				list.Add(result)
			End If
		End If
	End If
	Return list
End Function

Open in new window


Simple proof of concept -
Imports System.Data.SqlClient

Module Module1
	ReadOnly data As New List(Of String) From
		{
			"First",
			" ",
			"Second",
			"Third",
			"Fourth",
			" ",
			"sEcOnd"
		}

	Sub Main()
		Dim list As New List(Of String)
		For Each item In data
			If Not String.IsNullOrWhiteSpace(item) AndAlso Not list.Contains(item, StringComparer.OrdinalIgnoreCase) Then
				list.Add(item)
			End If
		Next

		For Each item In list
			Console.WriteLine(item)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:RIAS
ID: 41744533
Thanks Andy!
Thank you Saige Sir!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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