3 Replies - 359 Views - Last Post: 07 December 2012 - 07:27 PM Rate Topic: -----

#1 eraebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-November 12

Help me understand searching through Arrays

Posted 07 December 2012 - 06:22 PM

I want to search through an array of DVD's and BluRay's by director name. First, I know a <list> is easier, better...etc. But for this purpose, I want to use an array. I have created the array for both DVD's and BluRay's in an Inventory class, but I am having a really hard time figuring out how to search both arrays. I am passing user input from the program class,

I tried various ways but can't get them to work. Like a foreach statment, but it gave me errors, as if i couldn't search for string in both arrays. I also tried array.find, but still couldn't get them to work. The searchMovie() is at the bottom: Could anybody take the time to explain the best way to do this?


namespace MovieInventoryApplication
{
    class Inventory
    {
        public Bluray[] BlurayMovies;
        public DVD[] DVDMovies;

        private int blurayCount;
        private int dvdCount;

        public Inventory()
        {
            BlurayMovies = new Bluray[5];
            DVDMovies = new DVD[5];
            
            blurayCount = 0;
            dvdCount = 0;
        }
        
        public void AddBluray()
        {
            String strTitle;
            int intReleaseYear;
            int intRunningTimeMinutes;
            String strDirector;
            int intPrice;
            int intRegionCode;

            try
            {
                Console.Write("Enter a title: ");
                strTitle = Console.ReadLine();

                Console.Write("Enter a release year: ");
                intReleaseYear = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the running time in minutes: ");
                intRunningTimeMinutes = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the directors name: ");
                strDirector = Console.ReadLine();

                Console.Write("Enter a rental price: ");
                intPrice = Convert.ToInt32(Console.ReadLine());

                BlurayMovies[blurayCount] = new Bluray(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice);
                blurayCount++;

                Console.Write("Enter the DVD region code: ");
                intRegionCode = Convert.ToInt32(Console.ReadLine());

                DVDMovies[dvdCount] = new DVD(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice, intRegionCode);
                dvdCount++;
            }
            catch (FormatException FormatException)
            {
                Console.WriteLine(FormatException.Message);
                Console.WriteLine("Please enter a number in this field.");
            }
        }

        public void AddDVD()
        {
            String strTitle;
            int intReleaseYear;
            int intRunningTimeMinutes;
            String strDirector;
            int intPrice;
            int intRegionCode;

            try
            {
                Console.Write("Enter a title: ");
                strTitle = Console.ReadLine();

                Console.Write("Enter a release year: ");
                intReleaseYear = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the running time in minutes: ");
                intRunningTimeMinutes = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the directors name: ");
                strDirector = Console.ReadLine();

                Console.Write("Enter a rental price: ");
                intPrice = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the region code: ");
                intRegionCode = Convert.ToInt32(Console.ReadLine());

                DVDMovies[dvdCount] = new DVD(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice, intRegionCode);
                dvdCount++;
            }
            
            catch (FormatException FormatException)
            {
                Console.WriteLine(FormatException.Message);
                Console.WriteLine("Please enter a number in this field.");
            }
        }

   

        public void SearchMovie(string Director)
        {

            Director = string director;
            string  movieSearch;
            movieSearch = Array.Find(BlurayMovies, movieSearch => movieSearch.Equals== director);
            
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help me understand searching through Arrays

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5434
  • View blog
  • Posts: 11,646
  • Joined: 02-June 10

Re: Help me understand searching through Arrays

Posted 07 December 2012 - 06:43 PM

LInes 8 and 9 - get rid of them. YOu don't need to track these seperately because you should use the array.length property to always get the current length dynamically. This way you can't get these values out of sync with the actual size of the array.

Yes, you should use a list. Array doesn't let you resize easily.


public void SearchMovie(string Director)
105        {
106 
107            Director = string director;
108            string  movieSearch;
109            movieSearch = Array.Find(BlurayMovies, movieSearch => movieSearch.Equals== director);
110             
111        }

All I can say here is: WTF? Does this even compile? What the heck do you think line 107 even does? It looks a lot like you receive Director as a parameter (line 104) then immediately overwrite its value with a new string director (line 107). It makes sense to me that none of your collection are equal to a brand new string of no value.


Quote

Could anybody take the time to explain the best way to do this?

Don't try to do the fancy stuff until you get it working with the simple stuff. Just use a foreach loop to go through the two arrays.

Quote

Like a foreach statment, but it gave me errors

Well - its not here so we can't help you with those errors. All I can suggest is look up how a foreach loop should be formed.

In the future don't delete lines that don't work. Just comment them out so we can see what you tried - and YOU can see what you've done in the past along with your comments about WHY you stopped doing it that way. In-code comments are vital, especially as your code gets more complex and time passes between the initial creation and later updates.


Looking at your code sample you're what, maybe 2 weeks into a class. You aren't ready to be building an application from scratch. You don't know how much you don't know. You can't design until you have a better command of the coding language. For now, just follow the Learning Series tutorials and pickup a couple "Learn C# in 30 days" type books to do in addition to your course work.


rookie: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated DEC 2012
Spoiler




You should also read theMSDN page on array.find

I'm not sure what string you think its going to return in line 109.
If you search an array of type DVD then the return will be an instance of the DVD. array.find returns an object of the type the array consists of. Search an array of Dogs and the return is a dog. Search an array of Homes and the return is a home.
Was This Post Helpful? 0
  • +
  • -

#3 eraebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-November 12

Re: Help me understand searching through Arrays

Posted 07 December 2012 - 06:51 PM

public void SearchMovie(string Director)
        {
            
            foreach (string Director in DVDMovies)
            {
                Console.WriteLine(Director);
            }


So with the above foreach statement, the error is "A local variable cannot be declared in this scope" and "Cannot implicitly convert MovieInventoryApp.DVD to string.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Help me understand searching through Arrays

Posted 07 December 2012 - 07:27 PM

You pass Director as a parameter then declare Director as the foreach control variable. Use different names!

Other error is telling you that you are trying to convert a class into a string. Did you mean to use one of the elements of the class?

I don't see where you define the DVD class, but I suspect you want something like:
foreach(DVD dvd in DVDMovies) {
    Console.WriteLine(dvd.Director);
}

This post has been edited by Momerath: 07 December 2012 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1