Trouble linking classes I write to a form

Hi, this I am pretty new to windows programing and ive been trying to create an application using c++.net. The user interface is a windows form, but there are other classes ive written to do other things behind the scenes. I want my classes to be able to access data from the form, such as the values of numericalUpDown controls. I made a data member in my own class which contains a pointer of type 'form' so that I could access my forms properties from my own class. The trouble is of course, my form is not a generic form, but has derived from 'form' and is of type 'form1' so I cant use a pointer of type 'form' to access it. I tried making the pointer in my class of type 'form1' but that required me to include 'form1.h' in my classes header file which I didnt think was a good idea. If I understand correctly I could keep the pointer to be of type 'form' but cast it to form1 before using it, but this seems to be a bodge.

What is the usual way to accomplish what I'm trying to do

Any help would be appreciated.

Thanks.



Answer this question

Trouble linking classes I write to a form

  • Gad Rosenthal

    Form properties should really stay private to a form. If you find that you have to access them from another location then maybe it would be a good idea to rethink what you are doing. But if this is what you really want then the way to do it is as follows.

    In the class that you want to access your form from you need.

    #using namespace System::Windows::Forms;

    #using namespace <whatever your application namespace is>

    This will allow you to get access to the forms metadata. You then need to get a pointer to the form from somewhere, once that is done you can then access Form1 properly.

    But you seem to be mixing a few things from traditional c++ programming up with .net. C++/clr and managed extentions work by metadata not includes so you don't need to use header files to access external managed data.



  • Frederik Gheysels

    I had a feeling my problem was probably more to do with taking a wrong approach to the problem as you suggest. Accessing the value of numericUpDown's is probably a bad example, changing the text of lables when things change was more what I had in mind. Even if I keep these values private and write methods within my form to change them, i still need a pointer to my form to access those methods, so the problems seems to remain in that respect.

    With regards to using traditional c++ includes where I shouldnt, that sounds like a fair cop. I've done some (I wont say a lot) c++ programming before so I am probably harking back to what I used to do when I shouldnt be. So much for a book explaining what to do, the one Ive been using to learn .net has very poor examples from which it is very difficult to learn anything practicle. I dont think any of them address the problem of using multiple source files in one program!


  • GLAmb

    The best way to do this is to create helper member functions which can access the private member and can delegate the work for you... btw, this IS a valid (and a very common) concept of class design:

  • Trouble linking classes I write to a form