Solved

Creating an arraylist as global variable ?

Posted on 2004-08-27
9
381 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now