Solved

Array contains checking in vb.net

Posted on 2016-08-05
10
25 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 200 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 33

Accepted Solution

by:
it_saige earned 300 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading the web config for a running service on Windows 10 16 40
Regular expression help 2 23
Convert datetime to time string 10 19
vb.net class 3 11
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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