tags:

views:

60

answers:

3

Hi,

c# .Net 3.5 visual studio 2008, windows xp

I have a main form in a project, given a specific set of circumstances another form is instantiated and displayed to the user:

Form frmT = new frmTargetFolder(expName, this);
        frmT.Show();

As you can see, I am passing a reference to the new form from the current one. My question is, what do I have to do to a method so that it is exposed to the new form, the same for a variable?

I have tried defining the functions as public, but I can't seem to access them, also I have written a Get and Set method for a variable, again how do I expose these functions and methods to other forms?

public void hit()
    {
        MessageBox.Show("hit it");
    }


bool setOverRide
    {
        get
        {
            return OverRide;
        }
        set 
        {
            OverRide = value;

        }
    } 

The main form is called frmDataXfer and the form, form which I am trying to call the functions and methods of frmDataXfer is called frmTargetFolder, an instance of which is created in the frmDataXfer and referenced as frmT.

Thanks, R.

A: 

You need to store an instance of frmDataXfer as a field in the frmTargetFolder class, then call the methods on that instance.

SLaks
I have done that, when I call he constructor for FolderSelector (as it is now named) I pass the handle (this) for the current form and store it in a variable. However, the methods are still not accessible, defined as public etc.
flavour404
You need to change the field type to the actual type of the form.
SLaks
Ok, I realized what I was doing, SLaks and Aaronaught were both on the same track, I had my TargetFolder form variable defined as a Form, which of course was causing the problems... Did I mention that I have done about 120 hours this week... I think I am going to go and have a beer. Thanks everyone for the help, have a good one.
flavour404
A: 

Your setOverride property is not marked public. Aside from the fact that properties shouldn't be named this way (setOverride sounds like the name of a method, not a property), the default access modifier for class members is private. You need to add the public modifier to all members that you want to make public.


Edit:

Looking at the code again, the problem is clearly here:

Form frmT = new frmTargetFolder(expName, this);
frmT.Show();

Writing this, you'll only get the methods/properties that are members of the Form class. That doesn't include any of the members you added to the specific form class. Your code needs to be written as:

frmTargetFolder frmT = new frmTargetFolder(expName, this);
frmT.Show();

If you declare it as the correct type, then you can code against the new properties.

Aaronaught
I have done that, but still it is not exposed.
flavour404
A: 

Just remember that forms are first-and-foremost classes like any other, they just inherit from System.Windows.Forms.Form to give it special UI functions.

So, that being said, any public (or internal in the same project) field, property or method is accessible provided you have an instance of the object.

You don't need to store a form as a field to be able to reference it. You could just pass it as a parameter to a method call.

Enigmativity