Solved

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

Posted on 2014-09-11
1
125 Views
Last Modified: 2014-09-18
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
            {
                Debug.WriteLine("element not found");
            }

Open in new window

0
Comment
Question by:yguyon28
1 Comment
 
LVL 7

Accepted Solution

by:
JTennessen earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# GridRow get Old/New Value 1 55
What's the most efficient way to load up a couple of large table in C#? 5 38
Advice in Xamarin 21 56
Not seen Link button 5 15
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 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

22 Experts available now in Live!

Get 1:1 Help Now