Solved

Creating an arraylist as global variable ?

Posted on 2004-08-27
9
415 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:e_poirier
  • 4
  • 4
9 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 50 total points
ID: 11923427
have you considerred using the singleton pattern ?
0
 

Author Comment

by:e_poirier
ID: 11930738
Singleton pattern? Can you show me an sample?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11930809
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:e_poirier
ID: 11932450
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11933616
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
 

Author Comment

by:e_poirier
ID: 11934358
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11934408
sorry i didnt mean to copy/pase both ... they are the same thing (C# and C++)
0
 

Author Comment

by:e_poirier
ID: 11934663
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
 
LVL 3

Expert Comment

by:Statick001
ID: 12073129
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ class logger 6 108
destructor called after for each 7 36
How do I write a C++ program (coursework) 20 140
Decompiler for vb and vb.net - Visual Studio community edition 9 96
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question