I'm having trouble using MFC in Visual Studio 6. The problem I'm having is, say I have a text box that information is typed into. Once a button is clicked, this information is passed into code in a class ( that I created and added to the project ). This works fine. Now, there's a second button, and a second text box. When that second button is clicked, the info that was sent into my code should come out ( via another function in my class ) in that second text box. It's not happening. The text box is being filled with jumbled garbage.
One thing I did notice, is that if I perform BOTH functions inside 1 button click, it comes out fine. This makes me think that my class, my code, is going out of scope and losing it's values.
The problem is that I need my code to retain it's values. This is the first time I've ever used MFC, and I've tried everything I can think of, but nothing is working. I've never been this frustrated, and nothing I've found addresses this. It's probably something silly I'm missing, but I need help.
If what I'm saying isn't clear, please let me know, and I'll give more details. Any help is greatly appreciated.
6 1827 Banfa 9,065
Recognized Expert Moderator Expert
I'm having trouble using MFC in Visual Studio 6. The problem I'm having is, say I have a text box that information is typed into. Once a button is clicked, this information is passed into code in a class ( that I created and added to the project ). This works fine. Now, there's a second button, and a second text box. When that second button is clicked, the info that was sent into my code should come out ( via another function in my class ) in that second text box. It's not happening. The text box is being filled with jumbled garbage.
The text is not passed into a class, it is passed into an instance of the class.
The class is the type of the thing, the instance is the thing itself.
Without seeing any code it sounds like you do not have an instance of your class that persists between calls to the button handlers. It sounds like you are creating the instance as an automatic variable on the stack. An instance of this sort would be destructed when the function enters resulting in the 2 button click handler functions operating on separate instances of the class.
You need to place an instance of the class (or a pointer to an instance that is new'd) in the class that contains the button handler methods.
Banfa 9,065
Recognized Expert Moderator Expert
This looks correct which suggest the error is in your implementation of class info.
this is the declaration of my class - #ifndef INFO_H
-
#define INFO_H
-
-
class info
-
{
-
-
private:
-
char* name;
-
-
public:
-
void addName( char* n );
-
char* viewNames();
-
};
-
-
#endif
this is the definition of the class - #include "stdafx.h"
-
#include "info.h"
-
-
void info::addName( char* n )
-
{
-
name = n;
-
}
-
-
char* info::viewNames( )
-
{
-
return name;
-
}
here is where my class is being called ...the only thing it could possibly be is the "UpdateData", but I don't THINK that has anything to do with it (?), but again, if I call both functions of my class in "OnButton1()" it works fine, telling me my class is okay... - void CBookDlg::OnButton1()
-
{
-
UpdateData( true );
-
-
char temp[50];
-
strcpy( temp, m_first );
-
i.addName( temp );
-
-
UpdateData( false );
-
}
-
-
-
void CBookDlg::OnButton3()
-
{
-
UpdateData( true );
-
-
m_last = i.viewNames();
-
-
UpdateData( false );
-
}
Is it possible there's some setting I've missed? Like I said before, this is my first time using MFC. I've looked over this, and messed with it for hours, and can't find a single thing wrong with it. Maybe there's something I need to do with the project settings ... that's the ONLY thing I can think of. Any ideas?
Banfa 9,065
Recognized Expert Moderator Expert
Nope you have an implementation problem in your class info.
You are copying the pointer not what is pointed to, however where you are calling your instance of the class you are passing a pointer to the array temp which (as the name suggests) only exists temporarily as it is on the stack. As soon as the function exits that variable ceases to exist and the data is used for something else.
The instance of you class is left pointing at the place temp used to exist.
What you need to do is inside info allocate memory for storage of the supplied string using new, making sure that you don't cause any memory leaks and that you release the memory when the instance is destructed and that if the instance is reused there is enough memory for the new usage.
This memory will persist until you release it because it is allocated from the heap not the stack.
Banfa, thank you so much. Apparantly I'm a little more rusty on dealing with memory in C++ than I thought. I'll go back and read up on what I've forgotten. I really appreciate the help. Thank you, that was definately the problem.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Brian Jones |
last post by:
I'm sure the solution may be obvious, but this problem is driving me
mad. The following is my code:
class a(object):
mastervar =
def __init__(self):
print 'called a'
|
by: paul meaney |
last post by:
All,
myself and another developer have been staring blankly at a screen for
the past 48 hours and are wondering just what stunningly obvious thing
we are missing.
We are trying to load up 2...
|
by: jsale |
last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app
i have made is running on IIS v6 and consists of a number of pages that allow
the user to read information from the...
|
by: Andrew J. Marshall |
last post by:
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".
1) Does VB.NET create a default public constructor if I do...
|
by: MariusI |
last post by:
Are objects implicitly stored in the TLS of the currently running thread?
When creating multithreaded applications i get errors when accessing data
from a different thread than the thread used to...
| |
by: Rudy |
last post by:
Hello All!
I'm a little confused on Public Class or Modules.
Say I have a this on form "A"
Public Sub Subtract()
Dim Invoice As Decimal
Dim Wage As Decimal
Static PO As Decimal
Invoice =...
|
by: Jess |
last post by:
Hello,
I have a program that stores dynamically created objects into a
vector.
#include<iostream>
#include<vector>
using namespace std;
|
by: David |
last post by:
With a non-server app there is one instance of the program running and one
user 'using' it at a time. With this scenario I'm pretty comfortable with
variable scope and lifetime. With a server app...
|
by: Web Search Store |
last post by:
Hello,
I set up a web page with 2 user controls.
In classic asp, the first one did all the declarations, and the second one
used the values, and could reset it.
In ASP.Net so far I can't...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |