|
-
Nov 22nd, 2004, 03:02 PM
#1
"Global"
I want to make an ArrayList "global" so it can be made in the Init function, and added objects to it, and then I want OnPaint to get a hold to it, so it can draw something for every instance that is in the array. Where do I place it?
PS: I have one Namespace, and then the Form that includes that namespace. (sorry started with C# 30 minutes ago... )
Last edited by NoteMe; Nov 22nd, 2004 at 03:06 PM.
-
Nov 22nd, 2004, 03:33 PM
#2
Frenzied Member
Would a class level variable work for your needs? Or, if you need it to be accessed by several classes, maybe a static public in a seperate class.
-
Nov 22nd, 2004, 03:33 PM
#3
Sleep mode
wow , 30minutes ago !!!
Place it in a class file marked as public .
-
Nov 22nd, 2004, 03:48 PM
#4
Do you guys meen here (the line in the middle)?
Code:
public class Form1 : System.Windows.Forms.Form
{
public ArrayList skyObjects;
then something else has to be wrong, because in main() I have this: And I am getting some errors:
Code:
skyObjects = new ArrayList();
Sol theSun = new Sol("Sola","1.989e30 kg",40,0,Color.Yellow);
//Subscribe to the move event in the Clock class
theSun.moveEvent(earthDay);
//name,mass,radius/10000,distanse to the sun i AU * 10,farge
skyObjects.Add(theSun);
Like This (on the last line I posted):
E:\save\Solsystem\Solsystem\Hovedwin\Form1.cs(533): 'Hovedwin.Form1.skyObjects' denotes a 'field' where a 'class' was expected
Edit:
Originally posted by Pirate
wow , 30minutes ago !!!
Place it in a class file marked as public .
Yeah....I like to start fast... ....well I hate it all ready....
Last edited by NoteMe; Nov 22nd, 2004 at 03:57 PM.
-
Nov 22nd, 2004, 04:02 PM
#5
Frenzied Member
You're having that trouble because you're trying to access a non-static member. Are you getting the "object reference is required" message also? If so, the error you show is really the second error and will go away once you take care of the first.
You could make your array list static (same thing as shared in VB), if that suits your needs.
-
Nov 22nd, 2004, 04:10 PM
#6
Yeah that saved me...havn't done VB 6 in about 3 years now (and never touched VB.NET if that was what you ment), so can't remember much from it...
But I realy can't belive this. I have 2 and a halv C# book, and they don't have ONE example of how to use an array list. So I have decleared both the array list and the objects I want to go into it as static in the top of my form declaration. Is that the same as static in C++? Now I can only have one instance of that list even if I make more form objects. If so then it is ok, beause I am not going to make more then one instance of it.
ØØ
-
Nov 22nd, 2004, 04:40 PM
#7
Frenzied Member
Seems like you have to buy several books to get one complete one - always missing something you need. Not sure if that's the same thing as static in C++, but what it means is that that member is shared among any and all instances of that object. So yes, only one instance even if you have many forms.
If it's ok, then it's ok, but you may want to think about a little design change if you want it differently. You're certainly not limited to this. For example, you may want your main method to run your app starting with Form1 (or whatever), then in that class' constructor, do your array list thing.
Edit: If that doesn't make sense, post your code and I can show you the difference.
-
Nov 22nd, 2004, 04:50 PM
#8
-
Nov 22nd, 2004, 05:04 PM
#9
Frenzied Member
Like I said, if that works for you, then it's probably fine. But, if you took out all the junk in you main method, and moved that to the constructor (right below the InitalizeComponent() call), then you could remove the static business from your class level variables.
Not sure if it really makes a difference.
-
Nov 22nd, 2004, 05:09 PM
#10
Well for this project it doesn't. But it made me understand more.....A light bulb just said hello in my head...
First of all I was confused because the main func is in the form class file. I though it was inside the form. Now I understand that the code I have there is actualy after the form is created. So I don't have access from there.
Just a little missunderstanding from my side.
Thanks for helping me understand.
ØØ
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
|