Solved

# (((MyObject)myList[foundIndex]).ID.ToString());  ?? try to understand this

Posted on 2014-09-11
Medium Priority
134 Views
Could someone explain me what these two lines mean?
int foundIndex = myList.BinarySearch(new MyObject() { ID = 4 });

Debug.WriteLine(((MyObject)myList[foundIndex]).ID.ToString());

`````` ArrayList myList = new ArrayList();
myList.Add(new MyObject() { ID = 2 });

myList.Add(new MyObject() { ID = 4 });
myList.Add(new MyObject() { ID = 6 });
myList.Add(new MyObject() { ID = 10 });
myList.Sort();

//foreach (MyObject i in myList)
//{
//  Debug.WriteLine(i.ID.ToString());
//}
int foundIndex = myList.BinarySearch(new MyObject() { ID = 4 });
if (foundIndex >= 0)
{
Debug.WriteLine(((MyObject)myList[foundIndex]).ID.ToString());

}
else
{
}
``````
0
Question by:yguyon28
[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
1 Comment

LVL 8

Accepted Solution

Jeff Tennessen earned 2000 total points
ID: 40317317
The first line is doing a binary search against the list. A binary search is an algorithm for searching a sorted list much faster than a standard linear search. In this case, it is searching the list for the instance of "MyObject" whose ID property is 4. If it finds it, it stores the object's position within the list in the variable "foundIndex."

The second line writes the value of the ID property out to the debugging console. This appears to be simply a way of ensuring that the object with ID 4 was, indeed, found. To break it down, "myList[foundIndex]" gets the item from the list that resides at the position indicated by foundIndex. This item is then cast as type MyObject: "((MyObject)myList[foundIndex])". Finally, it gets the value of the ID property from the cast item and converts it to a string for writing to the debugging console. (Technically, the .ToString() method is unnecessary here, as Debug.WriteLine() will call ToString() itself for any object passed to it.)

Does that give you the type of information you were looking for?
0

## Featured Post

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month10 days, 12 hours left to enroll