Aleksa Ristic
Aleksa Ristic

Reputation: 35

Access class variable from another newly created class

I have code like this:

class First
{
    public int a { get; set; }
    public int b { get; set; }
    public int c = 2;
    public _second;

    public First()
    {
        _second = new Second(c);
        this.a = _second.a;
        this.b = _second.b;
    }
}


class Second
{
    public int a;
    public int b;

    public Second(int c)
    {
        if(c == 0)
        {
            a = 1;
            b = 2;
        }
        else
        {
            a = -1;
            b = -2;
        }
    }
}

How can I pass a and b from First class into second class and then directly from second class set their values withohut using static as declaration in First class.

I have tried this:

class First
{
    public int a;
    public int b;
    public int c = 2;
    public _second;

    public First()
    {
        _second = new Second(a, b, c);
    }
}


class Second
{
    public Second(int a, int b, int c)
    {
        if(c == 0)
        {
            a = 1;
            b = 2;
        }
        else
        {
            a = -1;
            b = -2;
        }
    }
}

but it is not doing the job.

Upvotes: 0

Views: 43

Answers (3)

Aleksa Ristic
Aleksa Ristic

Reputation: 35

I accomplished it with Interface.

public interface IClass
{
    int a { get; set; }
    int b { get; set; }
}

class First : IClass
{
    public int a { get; set; }
    public int b { get; set; }
    public int c = 2;
    public _second;

    public First()
    {
        _second = new Second(this, c);
    }
}


class Second
{
    public Second(IClass ic, int c)
    {
        if(c == 0)
        {
            ic.a = 1;
            ic.b = 2;
        }
        else
        {
            ic.a = -1;
            ic.b = -2;
        }
    }
}

Upvotes: 0

Steve
Steve

Reputation: 216353

You can simply add a constructor in the Second class that receives the instance of First and updates the public variables of the instance passed

public class Second
{
    public Second(int a, int b, int c)
    {
        // old constructor if still needed
        ...
    }
    public Second(First f)
    {
        int sign = f.c == 0 ? 1 : -1;
        f.a = 1 * sign;
        f.b = 2 * sign;
    }
}

By the way, I suggest to use properties instead of public fieds.

public class First
{
    public int a {get;set;}
    public int b {get;set;}
    public int c {get;set;} = 2;
    public Second _second {get;set;}

    public First()
    {
        _second = new Second(this);
    }
}

UPDATE
Looking at your image it is clear what is the source of your problem. You are receiving a Form class instance in your Forma constructor. You need to receive a Main instance like

 public Forma(Main form, int modulID) 
 {
     .....
 }

The base class Form has no knowledge of methods defined in custom form classes. In alternative you can still receive a Form instance but you need to add something like this

 public Forma(Form form, int modulID) 
 {
     Main m = form as Main;
     if(m != null) 
        m.helpWindow = new Help(modulID);
 }
     .....
 }

Upvotes: 2

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250366

You can pass the fields as ref meaning that you pass a reference to the field passed as parameter not the value. This means changes to the fields inside the Second constructor will be reflected in the First class

class First
{
    public int a;
    public int b;
    public int c = 2;
    public Second _second;

    public First()
    {
        _second = new Second(ref a, ref b, c);
    }
}


class Second
{
    public Second(ref int a, ref int b, int c)
    {
        if (c == 0)
        {
            a = 1;
            b = 2;
        }
        else
        {
            a = -1;
            b = -2;
        }
    }
}

You can pass by ref to any method not just a constructor. You should read more about this topic, here for example

Upvotes: 0

Related Questions