Results 1 to 8 of 8

Thread: [Resolved] An Array of Classes?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] An Array of Classes?

    I'm new to C#. In fact, I've only been screwing around with it for a few hours.

    I'm trying to make an array of classes, but I can't seem to get it right. Code is:

    Code:
    class CMovieList {
        private string strMovieName;
        private double dAmountMade;
    
        public void setValues(string m, double a) {
            strMovieName = m;
            dAmountMade = a;
        }
    
        public string getMovie() {
            return strMovieName;
        }
    
        public double getAmount() {
            return dAmountMade;
        }
    }
    
    class Class1 {
        static void Main(string[] args) {
    
            CMovieList[] cMovies = new CMovieList[1];
    
            cMovies[0].setValues("Movie Name", 120000.0);
            Console.WriteLine("The movie is: " + cMovies[0].getMovie());
        }
    }
    It compiles, but bombs out when it hits a line that accesses the object.
    Last edited by The Hobo; Sep 12th, 2003 at 11:34 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    First, you don't have a constructor (I am not one hundred percent sure you need it though). Next, I would change your Methods to properties.
    Code:
    public class CMovieList {
        private string strMovieName;
        private double dAmountMade;
    
        // Constructor
        public CMovieList() {
           // Insert initialization code here.
        }
         
        // Property
        public string MovieName {
             get{ return strMovieName;}
             set{ strMovieName = value;}
         }
      
        // Property
        public double AmountMade {
             get{ return dAmountMade;}
             set{ dAmountMade = value;}
        }
    }
    
    class Class1 {
        static void Main(string[] args) {
    
                CMovieList[] cMovies = new CMovieList[1];
               cMovies[0] = new CMovieList();
    
    
               cMovies[0].MovieName = "Movie Name";
              cMovies[0].AmountMade = 120000.0;
    
               Console.WriteLine("The movie is: " + cMovies[0].MovieName);
        }
    }
    I didn't run it through the IDE or compiler, so I can't verify all the syntax for you, but try it.
    Last edited by hellswraith; Sep 10th, 2003 at 10:20 PM.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I did try it using a property, but it crashed on the same line.

    I'll try adding a constructor to see if it helps, but it'll have to wait until Friday when I can get back to one of the computer labs on campus. I don't have C# on my computer yet.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I just edited it. Sorry, I left a line out.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    So, you have to actually create the object for each in the array. So if you had more than one you would do something like this:

    Code:
    int amount = 10;
    
    CMovieList[] cMovies = new CMovieList[amount];
    
    for(int i = 0; i < amount; i++){
       cMovies[i] = new CMovieList();
    }
    
    
    cMovies[0].MovieName = "Movie Name";
    cMovies[0].AmountMade = 120000.0;
    
    // load up the properties of the other objects.  You could do it from a loop or whatever....
    
    Console.WriteLine("The movie is: " + cMovies[0].MovieName);

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks, man. I'll give it a shot as soon as I get a chance and get back to you.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Works perfectly, hellswraith. Thanks!

    By the way, is that know as instantiating? Or am I mixing up the vocab words? I was never too good with the technical stuff.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Yes, that is how I know it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width