I've just started programming in c#, its a simple Forms application with single form.
I have a class written in a separate .cs file in its own namespace.

The rest of the code is in the Form1.cs file.
I am able to create an instance of the my class and use it in Form1.

My Problems...

  1. 1.Form1 contains an array of type char which need to use in the object instance.
  2. 2.I need to access the text field of the Form from within member function of the object
    instance.

Not sure which code sample to give u here...
I would appreciate any help provided,
thanks:)

Recommended Answers

All 2 Replies

To access something from one object (We'll call it A) in another object (B) you'll need a reference in B to A. One way to do this is in the constructor for B. You'll also need to make the item in A publicly available:

public partial claas A : Form {
    ... blah blah whatever code ...

    ... in whatever code you create the B object: ...

        B bObject = new B(this);

    public ArrayOfWhatever Array {
        return myInternalArray;
    }
}

public class B {
    private A aReference;
    public B (A referenceToA) {
        aReference = referenceToA;
    }
}

Yes, this is simplified, you'll have to work out the details for your specific implementation.

commented: The answer was strict to the point. +1

To access something from one object (We'll call it A) in another object (B) you'll need a reference in B to A. One way to do this is in the constructor for B. You'll also need to make the item in A publicly available:

public partial claas A : Form {
    ... blah blah whatever code ...

    ... in whatever code you create the B object: ...

        B bObject = new B(this);

    public ArrayOfWhatever Array {
        return myInternalArray;
    }
}

public class B {
    private A aReference;
    public B (A referenceToA) {
        aReference = referenceToA;
    }
}

Yes, this is simplified, you'll have to work out the details for your specific implementation.

Thanks for the tip , i now understand the passing a reference part of it.
I am able to access all the public members of Class A.
I would like to know as i just want to read the values can i use some other method
instead of setting the values as public.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.