Solved

Array contains checking in vb.net

Posted on 2016-08-05
10
24 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

12 Experts available now in Live!

Get 1:1 Help Now