Solved

Array contains checking in vb.net

Posted on 2016-08-05
10
32 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 34

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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