Results 1 to 10 of 10

Thread: "Global"

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    "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.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    wow , 30minutes ago !!!
    Place it in a class file marked as public .

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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.

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  6. #6

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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.


    ØØ

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  8. #8

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    You can have it......don't take a look at all those file menu items with the default names.......just tested to see if it was that easy to add them...

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  10. #10

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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
  •  



Click Here to Expand Forum to Full Width