newbie persistence question

I have a small dialog app that I'm unable to get data I'm pulling from
files to persist when passed to a container in another class. The data
retrieval is done from within the dialog class when a control button is
pushed. So, here's the example:

MyDlg.h
#inlude "OtherClass.h"

MyDlg.cpp
void MyDlg::OnFileScan()
{
  //... iterate through files and get data CString and integer

  OtherClass a;   //is this the problem here? new instance everytime?
  a.ProcessData(word, number);
}
/////
OtherClass.h
 map<string, int> myMap;

OtherClass.cpp
void OtherClass::ProcessData(CString word, int number)
{
  myMap[word] = number;   //add data to a map
}

///////////

The size of myMap is always 0 if I check it before the number is added
to the map and always 1 right after. So, the map is being destroyed
each time the function is run from MyDlg. Why is this and how should
this be properly setup so that the data persists in myMap in the
OtherClass?

Any help on this would be hugely appreciated.

Best regards,
Lorn

0
efoda5446 (11)
2/15/2006 1:46:18 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
250 Views

Similar Articles

[PageSpeed] 34

Lorn Davies wrote:
> I have a small dialog app that I'm unable to get data I'm pulling from
> files to persist when passed to a container in another class. The data
> retrieval is done from within the dialog class when a control button is
> pushed. So, here's the example:
> 
> MyDlg.h
> #inlude "OtherClass.h"
> 
> MyDlg.cpp
> void MyDlg::OnFileScan()
> {
>   //... iterate through files and get data CString and integer
> 
>   OtherClass a;   //is this the problem here? new instance everytime?
>   a.ProcessData(word, number);
> }
> /////
> OtherClass.h
>  map<string, int> myMap;
> 
> OtherClass.cpp
> void OtherClass::ProcessData(CString word, int number)
> {
>   myMap[word] = number;   //add data to a map
> }
> 
> ///////////
> 
> The size of myMap is always 0 if I check it before the number is added
> to the map and always 1 right after. So, the map is being destroyed
> each time the function is run from MyDlg. Why is this and how should
> this be properly setup so that the data persists in myMap in the
> OtherClass?
> 
> Any help on this would be hugely appreciated.
> 
> Best regards,
> Lorn
> 

Yes, you are creating a new instance of OtherClass each time.  But you 
need one object of OtherClass that continues to exist for the life of 
all the calls from your dialog.  Create this object externally to the 
dialog, before the dialog is created.  Initialize the dialog with a 
pointer or reference to the object it should call.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
2/15/2006 3:28:24 AM
Ok, great, thanks Scott for your reply.

I'm not exactly sure how to implement this though. Would it be
appropriate to create the object in my main app initialization? For
instance:

MyApp.cpp
#include "OtherClass.h"

BOOL MyApp::InitInstance()
{
  OtherClass a; // create obj here before dlg initialization?

  MyDlg dlg;
  m_pMainWnd = &dlg;
  int nResponse = dlg.DoModal();
}

Should the pointer be created in the same location or do I create this
somehow in the MyDlg class... perhaps just before the OnInitDialog
function:

MyDlg.cpp

BOOL MyDlg::OnInitDialog()
{
  //create pointer here??

  CDialog::OnInitDialog();
}

Unfortunately, I'm very inexperienced when working with pointers.
Although I understand the logic of what your suggesting I should do,
it's difficult for me to conceptualize how to implement it. Any
pointers (heh, sorry) you could give would help me out a lot.

Thanks again,
Lorn

0
efoda5446 (11)
2/15/2006 4:26:06 AM
Hi Lorn,

I'd do something like..

1) Make the container a member of your dialog (main) app, probably in the .h 
file as a class member.
        ObjectWhatever MyMap;

2) When you create an instance of the second class (OtherClass) assign a 
pointer in that class to the same container:
        OtherClass a;
        a.SetMyData(&MyMap);
        a.ProcessData(word,number);

3) Inside OtherClass you would have the SetMyData(ObjectWhatever *MyMap) 
function that sets the data over there:
        void OtherClass::SetMyData(ObjectWhatever *MyMap)
        {
                m_pMyMap = MyMap;
        }

4) Use m_pMyMap as a pointer now using -> to call functions.  m_pMyMap 
points to the object in the dialog class assuming it always stays 
instanciated for the life of the program.

Tom



"Lorn Davies" <efoda5446@yahoo.com> wrote in message 
news:1139977566.033182.279810@g14g2000cwa.googlegroups.com...
> Ok, great, thanks Scott for your reply.
>
> I'm not exactly sure how to implement this though. Would it be
> appropriate to create the object in my main app initialization? For
> instance:
>
> MyApp.cpp
> #include "OtherClass.h"
>
> BOOL MyApp::InitInstance()
> {
>  OtherClass a; // create obj here before dlg initialization?
>
>  MyDlg dlg;
>  m_pMainWnd = &dlg;
>  int nResponse = dlg.DoModal();
> }
>
> Should the pointer be created in the same location or do I create this
> somehow in the MyDlg class... perhaps just before the OnInitDialog
> function:
>
> MyDlg.cpp
>
> BOOL MyDlg::OnInitDialog()
> {
>  //create pointer here??
>
>  CDialog::OnInitDialog();
> }
>
> Unfortunately, I'm very inexperienced when working with pointers.
> Although I understand the logic of what your suggesting I should do,
> it's difficult for me to conceptualize how to implement it. Any
> pointers (heh, sorry) you could give would help me out a lot.
>
> Thanks again,
> Lorn
> 


0
tserface (3861)
2/15/2006 6:55:54 AM
Reply:

Similar Artilces:

Filter Question
I have a combo box with all the employee names. A name is selected and a filter is applied to the form as follows: DoCmd.ApplyFilter , "[employeename]='" & Me.Employee & "'" The problem I am having are that 2 of the employees have a ' in their name, like O'Conner. This causes an error. Is there any way I can handle this? "hotplate" <hotplate74@gmail.com> wrote in message news:f63bdf08-11cd-4b2e-a71d-840e1d04f732@w3g2000vbw.googlegroups.com... >I have a combo box with all the employee names. A name is selected &g...

Formatting Question #6
I have a field in my spreadsheet that is typed in as 6/29/5. How can I change this to 6/29/2005? Select the cells, go to Format>Cells, click Date in the list of categories & pick what you want from the choices offered. You should find what you want there. HTH |:>) "Metalteck" wrote: > I have a field in my spreadsheet that is typed in as 6/29/5. How can I change > this to 6/29/2005? ...

SELECT question?
Hi, I want to retrieve data under some conditions like below; SELECT T.A,T.B FROM TABLE1 T WHERE T.X = PARAM1 AND T.Y = PARAM2 what i need to do is, to retrieve data ; 1) if no params (PARAM1 and PARAM2) entered 2) if one of the params (PARAM1 or PARAM2) entered 3) both of the params (PARAM1 and PARAM2) entered Is there a way to construct this kind of SELECT statement? There is a good web page at http://www.sommarskog.se/dyn-search.html which will lead you to one of two pages depending on which version of SQL you are running. They have a good discussion of the methods...

Label design question
I have a label designed that I included the "Supplier" on. The problem is that it prints the Primary supplier defined in the item code rather than the supplier that I purchased from when printing from a PO. Does anyone know of a way to have it print the actual supplier associated with the PO rather than the primary one defined in the item properties? Check out RITE Label - it's a label printing add-in to RMS that adds functionality beyond the Label Wizard's ability. I'm not sure if this is included, but it's worth a look... The label wizard is not going to ...

Line Chart Questions
How can I Update the Line Chart automatically having different groups of data entered? Eg. Operator key in figures from the numbers 1-2 in column A then the chart for this range is updated automatically. When Operator key in 3-4 in column A, then another chart for 3-4 range is updated. How to do this? ...

Combo Box & Report Questions
1) I have a combo box that I want to use to create the content for a report and then have the combo box open the report. My strWhere code is returning a compile error: Variable Not Defined. What needs changing to make this work? 2) Can I open the report directly from the combo box instead of adding another command to open the report after the combo box content is selected? 3) How do I get the combo box to automatically remove the previous selection when I return to the form? 4) Will this work on a bound form? Following is my code (The table name is ASSIGNEES): Privat...

Trendline question
I have the following data: Ene-08 Feb-08 Mar-08 Abr-08 May-08 Jun-08 Jul-08 8 5000 3802 10400 7600 7400 9800 With this trendline obtaine from the chart: y = 44,772x - 2E+06 How do I forecast Ago-08 Should x=8 If I want to forecast Feb-09 should x=14? Thanks JP Please do not cords post. I have answered this in Charts "juanpablo" <juanpablo@discussions.microsoft.com> wrote in message news:65BC6FFC-0EA3-48E7-BCB2-4A0E76C34076@microsoft.com... > I have the following data: > > Ene-08 Feb-08 Mar-08 Abr-08 May-08 Jun-08 Jul-08 > 8 5000 3802 10...

POP Access Question
We have a new guy who wants to access his exchange account via POP Account using a 3rd party client Firefox Thunderbird. We can pull up his account on OWA but the account through Thunderbird does not allow the account to be opened. Any ideas on settings we may have missed? Thanks in advance -- NC Beach Bum Hi, In Computer Management | Services check to see if the service "Microsoft Exchange POP3" is started, if not change to automatic, and start the service. Then go into Exchange System Manager and check that the Default POP3 Virtual Server, is started. (It should be if ...

multiline Edit control question
Hello, I have an EDIT control on my dialog. On init I would like to put some text to this control. I override OnInitDialog method where I use SetWindowText("") method on the control to send some text. If the control is multiline, is it possible to put more than one line to the control using one of CEdit's methods? Thank you in advance for help, Tomek "TB" <tb@amt.pl> skrev i en meddelelse news:c14kea$23c2$1@news2.ipartners.pl... > Hello, > > I have an EDIT control on my dialog. On init I would like to put some text > to this control. > > ...

Design question
I have a bunch of entity classes with just a whole bunch of properties in it. I have to count each time one of these properties is read and written to. I was hoping I could implement a pattern that would allow me to do that easily so that I didn't have to write custom setters and getters for each property (I'm using automatic properties for convenience). Does anyone have any idea how I might be able to do this easily? The more I think about it the more it seems unlikely there is any other solution. My mind keeps veering toward events when I try to decide how I might easily d...

A Webstore question
I am looking for software or packages to Integrate Microsoft Dynamics GP with Web Stores. What are there in the market? So far, I've seen Nodus Technology http://www.nodus.com/. Are there others I can shop and compare? Thank You I know of a company running a product from: http://www.ignify.com/ "clairvoyant316" wrote: > I am looking for software or packages to Integrate Microsoft Dynamics GP with > Web Stores. What are there in the market? So far, I've seen Nodus Technology > http://www.nodus.com/. > Are there others I can shop and compare? > Thank You...

Font size question
Version: 2004 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel A workbook was all 10pt font was changed to all 14 pt font. <br> Only weird thing is, when I type in a new email, it goes to 10pt. (why?) <br> What to I do? thanks! Hi Kevs: Until you tell me which program you are using, I haven't a clue. Excel has nothing to do with email. Cheers On 13/04/10 12:14 PM, in article 59bb6d9f.-1@webcrossing.JaKIaxP2ac0, "kevs14@officeformac.com" <kevs14@officeformac.com> wrote: > Version: 2004 Operating System: Mac OS X 10.6 (...

OWA
Hello, Messages with cyrillic text via OWA to a destination outside the domain are received with question marks instead of the cyrillic text. We are running Exchange 2003 Enterprise on W2K3 with ISA 2004 as proxy. Under Exchange 2000, we did not have this problem, but after migrating to 2003, the problem started. Are there language settings that need to be configured on the Exchange server or ISA server? Thanks useregionalcharset http://support.microsoft.com/kb/830827/en-us "landoa" <landoa@discussions.microsoft.com> wrote in message news:46760711-053D-46DA-821A-F...

rank question #2
I need to rank a number against a separate list. It appears that the RANK function in Excel requires your value to actually be "in" the list you're ranking it against. Is there a formula I could use to work around this limitation of the built-in RANK function. Hi Bill Assuming the range of numbers are in A1:A28 and the number you want a ranking for is in cell C1 then try... {=RANK(MAX(IF(A1:A28<C1,A1:A28)),A1:A28)} This is an array formula so enter with Ctrl+Shift+Enter ----- XL2003 Regards William willwest22@yahoo.com "Bill_S" <BillS@discussions.micro...

Linked objects question
Hi, one of my big publisher documents keep asking me if I want to update embedded objects before printing. I went to help and it says to go to 'edit', links> and click the "locked" box to prevent a file from linking. I can't find any box that that say "locked". Could one of knowledgeable people tell me what or where the "locked" box is located? Thanks Catt ...

Macro question #9
Using Excel 2000: I am creating an Excel document that lists a number of items that could be included in an analysis. I have placed checkboxes next to each items and have assigned macros to the checkboxes so that when checked, the selected items are highlighted later on in the workbook. The problem is, when the boxes go from checked to unchecked, the items don't become unhighlighted. That is, although checking the box prompts the macro to run, unchecking the box doesn't cause the macro to "un-run" or return things to their pre-checked state. Any suggestions? Thanks in advan...

QuitPOS qsRules hook question
I'm using the quitPOS hook to perform some cleanup when POS exits. The problem is that the quitPOS hook is called regardless of whether the user answers Yes or No to the Quit confirmation dialog box. Is there a way to determine whether the user selected yes or no to this dialog? Any help would be greatly appreciated. Thanks. -j ...

Windows 7 Question 06-03-10
I cannot find a newsgroup that answers Windows 7 questions. If there is a newsgroup for Windows 7, can you please tell me the name? We are upgrading from Windows XP to Windows 7. We have a fileserver (running Server 2003 SP2) where our users' documents are stored. We configure the user's folder with their last name, such as Smith. After we complete the configuration of the Windows 7 computer, I notice that the folder name on our fileserver has changed, say to Jones, but the share name is still Smith. So I have multiple folders on our fileserver with the folder nam...

Newbie: Can't figure out how to print my brochure
I have created a 4-page brochure: 8.5 x 14; landscape, folded. Final brocure will be 7 x 8.5. I created a 4-page 'pub' but when I try to print the brochure it wants to print my Page 1 and Page 4 separate, but will print page 2-3 (my "inside pages") as 2 pages, side-by-side, which is correct. There should be (?) an option that prints pages 1 and 4, then 2 and 3, but I can't find it. HELP! TIA, Larry Woods You need a booklet (not a brochure) so you get page 4 on the left and page 1 on the right. Page set-up, booklet and you'll probably have to check all ...

xsl question
Hi group, I use this xsl: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <table class="TekstChart" border="1"> <tr> <th bgcolor="#f5deb3" align="left">Plaats</th> <th bgcolor="#f5deb3" align="left">Vorige</th> <th bgcolor="#f5deb3" align="left">Titel<...

exchange 2003 sp2 question
hey folks is there a way to verify the size of the db after sp2 deployment. ? the service pack was installed sucesfuly with no errors of any kind, but i see no way any where with in the software to verify the size increase. thanks The DB size does not go up, the DB size limit goes up to 18GB. If you want the full 75GB, you need to make a registry change to enable that. The info is in the Exchange help file in ESM. "Stas_K" <leshiy@gmail.com> wrote in message news:1131047060.802704.39920@g14g2000cwa.googlegroups.com... > > hey folks > > is there a way t...

2 Questions
Q1) I have 2 different formulas in 2 different columns, now no matte what I want these formulas in those columns all the way down. Is ther a way to fill an entire column with the following formulas, beside using the fill option? =IF($G10="",(IF($K10="",(IF($D10="","",(SUM(EDATE($B10,12)+$I10)))),"")),"") =IF($G10="",(IF($D10="","",(IF($C10="","",(SUM(EDATE($C10,12))+$I10))))),"") Q2) Is there a way to lock only certain cells or columns withou locking the entire worksheet...

Question on Landed Cost and Invoice matching
Hi all We are currently entering our landed cost through invoice matching. The procedure is pretty much standard a. Landed cost group is defined as an estimated percentage b. The landed cost group is asigned on the Purchase Receipt and then posted c. The actual invoice for the right amount of landed cost will be entered and matched to the RCT thus revaluating the unit cost if necessary. This all works well. Our problem is due to an error in which the invoices were not linked to the RCT and the cost was not revalued. Instead the invoice for the landed cost was entered as a payables tra...

Summing Questions
I've data that looks something like this: Product Period Qty Pdt1 Q1 12 Pdt2 Q2 10 Pdt1 Q3 08 Pdt3 Q2 07 Pdt4 Q2 06 Pdt2 Q2 06 Pdt3 Q3 11 Pdt3 Q3 25 Pdt2 Q4 23 Pdt4 Q4 08 Pdt2 Q2 19 Is there a formula to sum the total quantity of Pdt2 in Q2? Hi Derrick! One way if your table (including headings) is in A1:C12 =SUMPRODUCT(($A$2:$A$12="Pdt2")*($B$2:$B$12="Q2")*($C$2:$C$12)) Another using an array formula: =SUM(($A$2:$A$12="Pdt2")*($B$2:$B$12="Q2")*($C$2:$C$12...

Junk Senders question
Outlook 2002 SP3. I have two email newsletters that I subscribe to. NEITHER is in the Junk Senders .txt yet they both get sent to the Junk folder. How do I stop this annoying action? Gordon <gordonbp1@yahoo.invalid> wrote: > Outlook 2002 SP3. I have two email newsletters that I subscribe to. > NEITHER is in the Junk Senders .txt yet they both get sent to the > Junk folder. How do I stop this annoying action? Disabling your junk mail filter shold solve that problem. Apparently, there is some string in the message that is triggering the junk mail filter when it scans th...