Solved

Array contains checking in vb.net

Posted on 2016-08-05
10
23 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 32

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

13 Experts available now in Live!

Get 1:1 Help Now