|
-
Sep 10th, 2003, 09:02 PM
#1
Thread Starter
Stuck in the 80s
[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.
-
Sep 10th, 2003, 10:09 PM
#2
PowerPoster
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.
-
Sep 10th, 2003, 10:18 PM
#3
Thread Starter
Stuck in the 80s
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.
-
Sep 10th, 2003, 10:21 PM
#4
PowerPoster
I just edited it. Sorry, I left a line out.
-
Sep 10th, 2003, 10:24 PM
#5
PowerPoster
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);
-
Sep 10th, 2003, 10:33 PM
#6
Thread Starter
Stuck in the 80s
Thanks, man. I'll give it a shot as soon as I get a chance and get back to you.
-
Sep 12th, 2003, 11:34 AM
#7
Thread Starter
Stuck in the 80s
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.
-
Sep 12th, 2003, 11:48 AM
#8
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|