Creating an arraylist as global variable ?

Hi,

I am not really used in C++.NET (more used in VB.NET) and my client need me to make a C++ software. I need to use an arraylist in many forms (currently 2). The arraylist will contain a class that I have made.

Since I'm kind of new in C++ (even if I got some knowledge) I'm wondering how to access the same arraylist in 2 forms..

Could you help me and if possible, give me a detailled sample of what to do in my code ??

Thanks
Eric
e_poirierAsked:
Who is Participating?
 
gregoryyoungConnect With a Mentor Commented:
have you considerred using the singleton pattern ?
0
 
e_poirierAuthor Commented:
Singleton pattern? Can you show me an sample?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
e_poirierAuthor Commented:
Thanks a lot for your link, its really appreciated.

I have take a deep look into it, but since I'm kind of new in C++, I'm not sure how I can implement it in my code. Like I said in my post, I need to access an ArrayList containing a class.

Here is the code for my class, anyone could help me to understand how singleton would be used in that case. The software have 2 forms, the main form contains a list of items that are add/edited on another form. So both forms have to access the same information. I know that singleton will be usefull for that, but Its just how to implement it in my situation.

#pragma once
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;


public __gc class CItems
{
public:
            String* ProgramName,* StartHours,* StartMinutes,* EndHours,* EndMinutes;
            DateTime DStart;
            int DurationHours, DurationMinutes;
            bool ItemAuto, ItemCommercial;
};

0
 
gregoryyoungCommented:
in the second link I gave you will see this code ...

Figure 1 Singleton.cpp

// This program illustrates how to write a singleton class (a class that
// can have only one instance) in C++. The trick is to make the default
// constructor, copy constructor and assignment operator all private. A
// static function GetInstance returns the one and only object instance.
//
// If you attempt to compile this program, it will generate errors.
// (See main function below.)
//
class CSingleton {
public:
   static CSingleton& GetInstance() {
      static CSingleton theInstance; // one and only instance
      return theInstance;
   }

protected:
   // need default ctor for GetInstance.
   // ctor is protected, not private in case you want to derive.
   CSingleton() { }

private:
   CSingleton(const CSingleton& o) { }
   CSingleton& operator=(const CSingleton& o) { }
};
 
main()
{
    // These lines will not compile:
    CSingleton x = CSingleton::GetInstance(); // error: private
                                              // copy ctor!
    CSingleton y = CSingleton::GetInstance(); // error: private
                                              // copy ctor!
    x = y;                                    // error: private
                                              // assignment!
}


--------------------------------------------------------------------------------
Figure 2 Singleton.cs

// Singleton — list top-level visible windows
//
using System;

sealed class Singleton
{
   private Singleton() { }
   public static readonly Singleton TheInstance = new Singleton();
   public void SayHello() {
      Console.WriteLine("hello,world");
   }
}

class MyApp {
   // global command-line switches

   [STAThread]
   // main entry point
   static int Main(string[] args) {
//    Singleton s = new Singleton(); // error!
      Singleton s = Singleton.TheInstance;
      s.SayHello();
      return 0;
   }
}



basically you just use ...


Singleton s = Singleton.TheInstance;
then put methods on the Singleton class ... they will alll use the same instance of the class.
0
 
e_poirierAuthor Commented:
Thanks a lot, you are saving me.

One last thing thought, the code up there is for C#, at least the Figure 2.
Since I don't know really well C++, its worse for C#, what is the same thing in C++ language ? I mean as for the declaration of the class.

As I presume, there is no sealed class in C++ since the compiler was raising an error.

So far, I'm not sure how to translate this in C++..  I know its basically 3 lines of code, but its kind of weird. Also, do you know if I will be able to declare an ArrayList from the System::Collections and use it ?

sealed class Singleton
{
   private Singleton() { }
   public static readonly Singleton TheInstance = new Singleton();
   public void SayHello() {
      Console.WriteLine("hello,world");
   }
}

I am really sorry if I looks like very novice in C++, well I am quite novice in C++ afterall, but I really thanks you for your answers
0
 
gregoryyoungCommented:
sorry i didnt mean to copy/pase both ... they are the same thing (C# and C++)
0
 
e_poirierAuthor Commented:
No problems, its happens hehe.

I can't thank you enough. I feel I'm almost there.

I have used the code gave me, but the problem is I seems to not being able to declare an ArrayList from the System::Collections, its says: error C3265: cannot declare a managed 'arrtest' in an unmanaged 'CSingleton'


here is the code

#pragma once
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;

class CSingleton
{
public:
   static CSingleton& GetInstance() {
      static CSingleton theInstance; // one and only instance
      return theInstance;
   }
   ArrayList *arrtest;

protected:
   // need default ctor for GetInstance.
   // ctor is protected, not private in case you want to derive.
   CSingleton() { }

private:
   CSingleton(const CSingleton& o) { }
   CSingleton& operator=(const CSingleton& o) { }
};
0
 
Statick001Commented:
i think, much easier than trying the singleton method, would be to simply pass the arraylist from one form to the other

so in the second form, you would need to define an arraylist but leave it empty.
then in the first form, when u open the 2nd form you also pass it the arraylist

in the 2nd form you would need to define the arraylist as a private member of the form

private:
   ArrayList * my_array_list;

and define a public property so you can pass the arraylist from the first form
public:
   __property void set_MyArrayList(ArrayList * list)
   {
      this->my_array_list = list;
   }


then when you open the second form from the first, you can pass any arraylist you like to it

Form2 * new_form = new Form2();
new_form->MyArrayList(arraylist_being_passed);
new_form->Show();

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.