Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 28
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
All today's birthdays', congrats!
Mathias (41)
slash128v6 (52)


Next birthdays
01/31 Mathias (41)
01/31 slash128v6 (52)
02/01 Barry (70)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Visual C++ 2008 : problems of using public accessors created within a Form1 Class

Move Thread LAN_403
TheMerovingian
Thu Jan 29 2009, 11:21AM Print
TheMerovingian Registered Member #14 Joined: Thu Feb 02 2006, 01:04PM
Location: Prato/italy
Posts: 383
To allow access to properties of Form1 objects i created a public function within the form1 class
public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: aggiungere qui il codice del costruttore.
			//
		}

		String^ GetTextBox1Text()
		{

        return this->textBox1->Text;
		}
... continues

when i'm trying to access to the function

void display ()
{
	String ^st = Form1::GetTextBox1Text();

from another form or a function the compiler flags me an error (C2352) class::function' : illegal call of non-static member function

Why i cannot access the data from outside the Form if a created a public accessor with this purpose?

many thanx in advance
Back to top
Steve Conner
Thu Jan 29 2009, 03:02PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
This is a classic C++ misunderstanding. You need to instantiate an object before you can call its member functions. The class is just a kind of template for making an object, not the object itself. For example, in the beginning of your program put:

Form1 MyForm; // this creates an object of type Form1, called MyForm

Then you can call
String Mystring = MyForm.GetTextBox1Text(); // this gets the text from MyForm
provided of course that the "MyForm" object is in your scope.

If you declare the member functions static, then you can actually call them without an instance of the object, as in your example code. But this defeats the whole point of OO, since they don't have any object to operate on. I only mention this because it explains the error message you're getting.

If you're trying to call the member functions of one Form1 object from inside another Form1 object, it starts to get messy. You can do it by passing "this" pointers around, but you probably want to avoid it by redesigning your program.

Better still, forget all this OO nonsense, real men use FORTRAN with lots of inline assembler ;)
Back to top
Zenador
Thu Jan 29 2009, 03:14PM
Zenador Registered Member #1733 Joined: Thu Oct 02 2008, 03:17PM
Location: Hamilton, ON, Canada
Posts: 100
Have you "stepped" through the functions and the calls of the functions? Visual Studio has nifty debugging tools.

My first guess (without seeing all the code) would be the text box object on the form cannot be made public. I ran into this with VB all the time. For objects which I needed to access from multiple forms, I have always used variables in the (General) - Declarations. On a focus change within the containing form, the current value would be written to the general variables. This works well if all your forms are loaded on startup and just hide the ones you don't need.

If you would like, I can go through the code... It's a slow day at work... wink

Z
Back to top
Steve Conner
Thu Jan 29 2009, 04:04PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
It's C, so you can't step through it if you can't even get it to compile.

I'll restate what I said another way: You are using the "this" pointer in your GetTextBox1Text() member function. But "this" has no meaning until you've created an instance of the object. Just like the spoon in The Matrix.
Back to top
TheMerovingian
Fri Jan 30 2009, 04:03PM
TheMerovingian Registered Member #14 Joined: Thu Feb 02 2006, 01:04PM
Location: Prato/italy
Posts: 383
[quote]
This is a classic C++ misunderstanding. You need to instantiate an object before you can call its member functions. The class is just a kind of template for making an object, not the object itself. For example, in the beginning of your program put:

Form1 MyForm; // this creates an object of type Form1, called MyForm

Then you can call
String Mystring = MyForm.GetTextBox1Text(); // this gets the text from MyForm
provided of course that the "MyForm" object is in your scope.
[/quote1233331447]

It is a different function

Steve McConner wrote ...

If you declare the member functions static, then you can actually call them without an instance of the object, as in your example code. But this defeats the whole point of OO, since they don't have any object to operate on. I only mention this because it explains the error message you're getting.

I know, in fact the "this" pointer isn't available, because there's no object itself

Steve McConner wrote ...

If you're trying to call the member functions of one Form1 object from inside another Form1 object, it starts to get messy. You can do it by passing "this" pointers around, but you probably want to avoid it by redesigning your program.

I tried this route, creating a public function that returns the this pointer but this doesn't work for the same reason , cannot access the function

Steve McConner wrote ...

Better still, forget all this OO nonsense, real men use FORTRAN with lots of inline assembler ;)


I used fortran but c++ is more flexyble and windows related (using .NET 2.0 framework functions)



I dind't explain myself well. The class is created and the object too, in fact Form1 esist , and it is allocated in memory using the (gc)new statement in the "main" function, but the compiler doesn't allow me to access to public member functions of Form1 neither from different Form (Form2) member functions or pure functions (void calculate(void) {blabla} )


Zenador wrote ...

Have you "stepped" through the functions and the calls of the functions? Visual Studio has nifty debugging tools.

My first guess (without seeing all the code) would be the text box object on the form cannot be made public. I ran into this with VB all the time. For objects which I needed to access from multiple forms, I have always used variables in the (General) - Declarations. On a focus change within the containing form, the current value would be written to the general variables. This works well if all your forms are loaded on startup and just hide the ones you don't need.

If you would like, I can go through the code... It's a slow day at work... wink

Z

Using global pointers or variables defeats all the purpose of Object Oriended Programming, public accessors are cleaner and easier to maintain


I will write a small program stripped from all that doesn't is realated to the error and publish


Edit:
Yeah I see, i'm calling the Member function from the class and not from the object itself...

This is a problem without solution...


Solved this way:

int main(array<System::String ^> ^args)
{

// Attivare gli effetti visivi di Windows XP prima di creare i controlli
Application::EnableVisualStyles();
Application:: SetCompatibleTextRenderingDefault(false);

Form1 ^myform = gcnew Form1();

myform->textBox1->Text = "Vaffanculo";

// Creare la finestra principale ed eseguirla
Application::Run(myform);


return 0;
}

Of course it returns me an illegal access error because TextBox1 is private, but making it public or creating a member function will reset the problem

For course this doesn't solve the problem of accessing one form from another , unless i make the pointers global...

an ugly solution but should work...
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.