.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
ASPEvil
Ashutosh Jha
Jean Paul
satyapriyanayak
SP

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

find value in an array

Posted By:Amit Mehra       Posted Date: February 17, 2009    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  
 

find value in an array in C# or Vb.Net is very easy. We can use Array.BinarySearch() Or Array.IndexOf() to find the value is exist in given array or not. Lets see an example.

Here is our sample method to fetch value from array it will take array and key Object to search and it will return the Boolean value if the value is found in an array it will return true else false.

Now there are two scenario that your input array is sorted or not..we will see find the value in an array in both the cases.

If Input Array Is sorted array

C# Example

public static bool FindValueFromArray(object[] Values,object valueToSearch)

{

bool retVal = false;

Array myArray = (Array)Values;

int found = Array.BinarySearch(myArray, valueToSearch);

if (found != -1)

{

retVal = true;

}

return retVal;

}

VB.Net Code Sample

Public Shared Function FindValueFromArray(ByVal Values As Object(), ByVal valueToSearch As Object) As Boolean

Dim retVal As Boolean = False

Dim myArray As Array = DirectCast(Values, Array)

Dim found As Integer = Array.BinarySearch(myArray, valueToSearch)

If found <> -1 Then

retVal = True

End If

Return retVal

End Function

Test the above method.

object [] obj = new object[] { "First","Second","Third","Fourth" };

bool found = FindValueFromArray(obj, "Third");

NOTE: Array.BinarySearch() method will return you the position of search value in an array.But Array shoud be sorted Array. if you want you can modify the code.

If Input array in not sorted

If your array is not sorted and you want to find that value is exist in an array or not you can Use Array.IndexOf();

For Eg:

In C# If inpur Array is not sorted

public static bool FindValueFromArray(object[] Values,object valueToSearch)

{

bool retVal = false;

Array myArray = (Array)Values;

if (Array.IndexOf(myArray, valueToSearch) != -1)

{

retVal = true;

}

return retVal;

}

In Vb.NET

Public Shared Function FindValueFromArray(ByVal Values As Object(), ByVal valueToSearch As Object) As Boolean
    Dim retVal As Boolean = False
    Dim myArray As Array = DirectCast(Values, Array)
    If Array.IndexOf(myArray, valueToSearch) <> -1 Then
        retVal = True
    End If
    Return retVal
End Function

Hope this will help the developer who want to find the value from an array.



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend