Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the code below, when I try to access int y; in the "son" class inside the parent class Form1, it tells me:
An object reference is required for the nonstatic field, method, or property Form1.y

When I hover over "y" in x = y;
To circumvent this, I did an awkward move by creating an instance in the class Program.
C#
public static Form1 f;

and
C#
Application.Run(f= new Form1());

and
C#
x = Program.f.y;


What I have tried:

public partial class Form1 : Form
    {

        class shape
        {
            int x,z;
            public test(int w)
            {
                x = y;
                z = w;
            }
        }
        List<shape> lstShapes= new List<shape>();

        int y = 1;

        public Form1()
        {
            InitializeComponent();
            lstShapes.Add(new Shape(8));
        }
    }
Posted
Updated 15-Feb-22 7:17am
v8
Comments
Richard MacCutchan 15-Feb-22 6:15am    
The variable y is a member of the Form1 class, so it cannot be accessed directly in the class. But why have you created the test class, as it is not used, and would seem to serve no purpose.
FierceLion 15-Feb-22 6:17am    
It is a class for example in a game, the class' name is: Class shape {} and it has members for each shape like an int[,] array of its data and bool available; if it's available, I want the inside of class test to access the same instance of the class they're in at that time.
Richard MacCutchan 15-Feb-22 6:26am    
You need to pass an instance of the parent to the inner class, probably via the constructor. Something like the following:
class test
{
    Form1 parent = null;
    int x;
    public test(Form1 outer)
    {
        parent = outer;
        x = outer.y;
    }
}

// in Form1 declare a test object
test myTest;

// and in the constructor add
myTest = new test(this);
FierceLion 15-Feb-22 6:31am    
Thx, works, are you sure there's no simpler way?, please submit as an answer so I accept it.
Richard MacCutchan 15-Feb-22 8:55am    
No, this is the only way, but it is still a bad design.

1 solution

test is not derived from Form1, so it doesn't have access to Form1 data - and even if it did, it wouldn't "know" which of potentially hundreds of Form1 instances contained the right value of y.

Think about it: if you put your mobile in the glove box of your car, and then we go for a drive in my car, do you expect to be able to open the glove box in front of you and retrieve your mobile? Of course not - you know it is in the glove box of your car, not mine and that the two cars, and the two glove boxes are completely separate and do not share anything.
To access the glove box content you have to explicitly specify the car: my car, yoru care, this car, that car: you can access each glove box in turn.

It's the same with classes: to access a variable in Form1 you have to specify exactly which instance of the form class you are talking about. Inside the form class itself, that is done via an implicit or explicit use of this:
C#
public Form1()
{
    InitializeComponent();
    y = 666;
}
(Implicit)
C#
public Form1()
{
    InitializeComponent();
    this.y = 666;
}
(Explicit)

From outside the form class, you need to use an explicit reference to the actual instance of the form that you want to access.

And ... that's a bad idea in your case, because the test class should not know that the Form1 class even exists, let alone what variables it contains - it if does then you "lock" the two classes together, and you can no longer make a change to either without considering the effects on the other (which may be breaking changes if Form1 decides that y should be a display control instead of a simple variable).
 
Share this answer
 
Comments
FierceLion 15-Feb-22 6:22am    
See question modification, I think "this.y" should do the job but it doesn't.
OriginalGriff 15-Feb-22 6:45am    
No, it won't - because "this" always refers to the current instance of the current class.
Inside any code in the test class, "this" refers to the current instance of the test class, which does not contain a definition for y.
in the Form1 class, "this" refers to Form1.y (current instance) which does exist.

Unless you have a reference to the correct instance of the Form1 class inside your test class, you cannot access any of its data!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900