Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » How to access other classes variables?
How to access other classes variables? [message #9630] Tue, 22 May 2007 21:13 Go to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
I have a very basic problem with OOP. I have two classes and i want to access a variable in one class which is defined in the other class.

header.h
class1
{
  String a_string;
};

class2
{
 ..
};


class1.cpp
#include "header.h"

class1::class1()
{
  a_string = "TEST";
}


class2.cpp
#include "header.h"

class2::class2()
{
  PromptOK(a_string);
}


How can i use the value of a_string in class2 which is defined in class1?

It's surely very basic noob stuff, but i didn' get it after reading different tutorials.

Michael
Re: How to access other classes variables? [message #9632 is a reply to message #9630] Tue, 22 May 2007 23:12 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
michael wrote on Tue, 22 May 2007 21:13

I have a very basic problem with OOP. I have two classes and i want to access a variable in one class which is defined in the other class.

header.h
class1
{
  String a_string;
};

class2
{
 ..
};


class1.cpp
#include "header.h"

class1::class1()
{
  a_string = "TEST";
}


class2.cpp
#include "header.h"

class2::class2()
{
  PromptOK(a_string);
}


How can i use the value of a_string in class2 which is defined in class1?

It's surely very basic noob stuff, but i didn' get it after reading different tutorials.

Michael


Hi,

you have to define an object of class1 somewhere, maybe with global scope,

class1 a;

and then use it, for example a.a_string in class2.

Luigi
Re: How to access other classes variables? [message #9635 is a reply to message #9630] Wed, 23 May 2007 00:37 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Thanks Luigi, this was the perfect hint.

Michael
Re: How to access other classes variables? [message #9638 is a reply to message #9630] Wed, 23 May 2007 07:40 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
There is another problem.

I have defined an object of class1 with global scope:

class1 global;


Then i use it in class2 like this:

String a_string = global.datagrid.Get(1);


datagrid is a sqlarrayctrl and a member of class1.

But i can't get any data from the datagrid, a_string is always empty.

Any tips?

Michael
Re: How to access other classes variables? [message #9641 is a reply to message #9638] Wed, 23 May 2007 09:36 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
michael wrote on Wed, 23 May 2007 07:40

There is another problem.

I have defined an object of class1 with global scope:

class1 global;


Then i use it in class2 like this:

String a_string = global.datagrid.Get(1);


datagrid is a sqlarrayctrl and a member of class1.

But i can't get any data from the datagrid, a_string is always empty.

Any tips?

Michael


I've never used sqlarrayctrl. Anyway, are you sure you have correctly initialized class1 and populated datagrid?
Moreover, I suggest you to post your test package to easily catch the error.

Luigi
Re: How to access other classes variables? [message #9645 is a reply to message #9630] Wed, 23 May 2007 16:03 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
After testing for a while it seems that reading data from the datagrid doesn't work because there is no row selected.

Here is what i'm doing:

1.
I start my application, it reads some data from an sql-table and shows them in a sqlarrayctrl which is placed in my first topwindow.

2.
Then i open a editdialog (2nd topwindow) which tries to get the data from datagrid. The result is empty.

3.
I tried to get the selected row by using datagrid.GetCursor to check what going on and the result is -1 which means there is no row selected. This is the reason why there is no data as a result of using datagrid.Get(1) for example.

I think the problem is that i try to get the data after opening a new topwindow (my editdialog).

Anybody additional ideas?

Michael
Re: How to access other classes variables? [message #9647 is a reply to message #9645] Wed, 23 May 2007 18:48 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
It's pretty difficult to guess whats wrong becuase the there are many ways in which you could have configured the control. However, my best guess is that you are calling the wrong function.

In the simplest case I believe calling ArrayCtrl::Get(x) will return the contents of Column x on the current cursor row. If there is no cursor row you will get nothing.

I think you probably want to use either GetLine(row), which returns an array of values from a row, or Get(row, column) to get the value of a particular field.

James.
Re: How to access other classes variables? [message #9649 is a reply to message #9630] Wed, 23 May 2007 21:04 Go to previous message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Thanks, mrjt.

Your hint about using Get(row, column) works. Smile
Previous Topic: Constructor with parameter
Next Topic: How to use string array with functions?
Goto Forum:
  


Current Time: Thu Apr 18 14:11:36 CEST 2024

Total time taken to generate the page: 0.02072 seconds