8 Replies - 402 Views - Last Post: 07 December 2011 - 02:32 PM Rate Topic: -----

#1 joi93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

Accessing an instance of a class from another class

Posted 07 December 2011 - 08:45 AM

I have three classes, let's call them A, B and C.

B creates an instance of C. B then puts information in a varibale in the C instance.
A now has to acces that information that B put in its instance of C, and this is the problem, I've no idea how to do this.

Help appreciated!
Is This A Good Question/Topic? 0
  • +

Replies To: Accessing an instance of a class from another class

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 08:51 AM

I am assuming that A has access to B. Now, just add a getter (function to return C) in B and then A can use that to get to C. In C make sure there are functions to get to the data that B put there. This is also assuming that B doesn't keep around the data it stores in C (that would be redundant).

Hopefully that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 joi93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 12:08 PM

View PostBetaWar, on 07 December 2011 - 08:51 AM, said:

I am assuming that A has access to B. Now, just add a getter (function to return C) in B and then A can use that to get to C. In C make sure there are functions to get to the data that B put there. This is also assuming that B doesn't keep around the data it stores in C (that would be redundant).

Hopefully that makes sense.



Basically I want to do as i said in the first post, but doing as you said (i think i've done the right thing?) doesen't solve my problem. The "ID" variable has value 0 in Class "C" (it has value 5 in class "A", but i'd like it to work for Class "C"). I hope this code makes sense to you.


namespace ConsoleApplication1
{
    class MainClass  // think of this class as "A"
    {

        static void Main()
        {
            troll troll2;
            troll2 = new troll();

            hejsan hejsan2;
            hejsan2 = new hejsan();

            troll2.hej();


            hejsan2.write();

            //Console.WriteLine(troll2.ID); using this it works fine, but this is not how I want to do it.

            Console.ReadKey();

        }
    }
    

        class troll // think of this class as "B"
        {
            private int o;
            public int ID
            {
                get { return o; }
                set { o = value; }
            }
            
            
            public void hej()
            {
                             
                hejsan hejsan2;
                hejsan2 = new hejsan();

                // hejsan2.value(); // this runs code in class "C" that gives a int.
                ID = hejsan2.h; // this sends that int to the "getter".
            }
             
                                                                              
        }

        class hejsan // think of this class as "C"
        {
            public int h = 0;

            public void value()
            {
                h = 5;
            }

            public void write()
            {
                troll troll2;
                troll2 = new troll();

                Console.WriteLine(troll2.ID); // The ID has value 0 here, which it's not supposed to have.

            }


        }


    
}


Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,230
  • Joined: 31-December 10

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 12:22 PM

This is the C/C++ language forums.
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 12:33 PM

Alright, I went ahead and moved this to Java, as that appears to be what you are attempting to use. At the very least that isn't C/C++ code posted above.

Now, I have a few other questions, because the code doesn't look like any Java I have ever seen either... but it is a closer match... maybe C#?

First, you need to declare your functions with parens and any parameters you are expecting.
Second, your getter method isn't following what I said before. I was suggesting that you return a reference to the hejsan that the troll contains, then you can store a local copy of that in Main and use it to get information back.
Was This Post Helpful? 1
  • +
  • -

#6 joi93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 02:01 PM

View PostBetaWar, on 07 December 2011 - 12:33 PM, said:

Alright, I went ahead and moved this to Java, as that appears to be what you are attempting to use. At the very least that isn't C/C++ code posted above.

Now, I have a few other questions, because the code doesn't look like any Java I have ever seen either... but it is a closer match... maybe C#?

First, you need to declare your functions with parens and any parameters you are expecting.
Second, your getter method isn't following what I said before. I was suggesting that you return a reference to the hejsan that the troll contains, then you can store a local copy of that in Main and use it to get information back.


Yes it's inded C#. Guess i was a bit too fast at posing it. But what you said in this post still applies?
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 02:20 PM

The second point still applies for sure, and I believe the first does as well.

Sorry, but at this point we are outside of my expertise, so I will be letting someone who knows C# help you from here.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 02:26 PM

Read this:

http://www.dreaminco...ny-other-forms/

And let us know if it solves your issue.
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Accessing an instance of a class from another class

Posted 07 December 2011 - 02:32 PM

You would do it exactly how BetaWar stated.

ClassC
public class ClassC
{
    public int ID { get; set; }
}



ClassB
public class ClassB
{
    private ClassC _myClassC;

    // constructor
    public ClassB()
    {
        // sets the ID of ClassC to 5
        _myClassC = new ClassC();
        _myClassC.ID = 5;
    }

    public int GetClassCID()
    {
        // gets the ID from the local instance
        return _myClassC.ID;
    }
}



ClassA
public class ClassA
{
    private ClassB _myClassB;

    public int ClassCID
    {
        get { return _myClassB.GetClassCID(); }
    }

    public ClassA()
    {
        _myClassB = new ClassB();
    }
}



Then, let's say we want to show a message box in a WinForm application with the value of the ID of ClassC by only using ClassA...

ClassA myClassA = new ClassA();
MessageBox.Show(myClassA.ClassCID.ToString());



So we call the property ClassCID of ClassA, which calls the GetClassCID method of ClassB, which returns the value of the ID from ClassC.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1