7 Replies - 179 Views - Last Post: 08 February 2012 - 09:04 AM Rate Topic: -----

Topic Sponsor:

#1 Dinaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 10

How do I get an object's instance?

Posted 08 February 2012 - 07:42 AM

Hey guys, I've been at this problem for a little over a day now, and today isn't proving to be very productive either.

My issue is that I want to get a value from an object that gets created earlier in the application. My thought is that if I can get that object instance into the current object than I can use the value I'm after by accessing the member. That is proving more difficult for me for some reason. I can't seem to wrap my head around how to do it.

I don't have any code for this issue as I don't quite know what to use.

I was thinking for a time that I could use a static member to get that value, but my class is not structured to do that very easily.

If there's a tutorial on this or a good article to read let me know.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How do I get an object's instance?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: How do I get an object's instance?

Posted 08 February 2012 - 07:48 AM

Quote

My issue is that I want to get a value from an object that gets created earlier in the application.


How much earlier? Is it on the same form? Another form? Where is it created? Where do you need to use it again?
Was This Post Helpful? 0
  • +
  • -

#3 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: How do I get an object's instance?

Posted 08 February 2012 - 07:58 AM

Just show us the code you do have!
Have you tried using a pointer or a reference to point to the variables area in memory?

This post has been edited by Toadill: 08 February 2012 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Re: How do I get an object's instance?

Posted 08 February 2012 - 08:02 AM

View PostDinaste, on 08 February 2012 - 08:42 AM, said:

My issue is that I want to get a value from an object that gets created earlier in the application.


Let me guess, you made the object instance from within a method.

private void SomeMethod()
{
   customObject myObject = new customObject();
   // other stuff
}


And now you wan some way to reference that instance myObject. Is that right?

The most direct means of this is make a property that can be reached.

public customObject myObject { get; set;}

private void SomeMethod()
{
   myObject = new customObject(); // 
   // other stuff
}

This post has been edited by tlhIn`toq: 08 February 2012 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dinaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 10

Re: How do I get an object's instance?

Posted 08 February 2012 - 08:47 AM

Sorry let me be more specific.

In my application a form gets spawned, and the user then chooses a selection from a combobox. My objective is to get the value of whatever they chose in the combobox to the next form that gets spawned when they click the load button.

The instance of each form gets created in a new thread and the previous one gets closed.

private void NewTeamBtn_Click(object sender, EventArgs e)
        {
            System.Threading.Thread NewThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProcNewTeam));
            NewThread.Start();
            this.Close();           
        }
        public static void ThreadProcNewTeam()
        {
            Application.Run(new CreateATeam());
        }


Hmm, just thought of something.

Could I technically create a constructor for the following form that takes a parameter and said parameter would be the value? I could then just map that parameter back to a field for the next form?

Do you guys think that would work?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: How do I get an object's instance?

Posted 08 February 2012 - 08:52 AM

View PostDinaste, on 08 February 2012 - 11:47 AM, said:

Could I technically create a constructor for the following form that takes a parameter and said parameter would be the value? I could then just map that parameter back to a field for the next form?

Do you guys think that would work?


Yes, that is what you should do.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Re: How do I get an object's instance?

Posted 08 February 2012 - 08:52 AM

You have a real design problem. If the object is created in form3, and you pass that to form4, then CLOSE form3 (the parent form of the object) the object can potentially be disposed of.

Yes you can pass the object as a constructor parameter. But you should then clone it so it is its own fresh new object that belongs to the receiving form.

But honestly - I don't care for the description of the program design: Constant creation and destruction of forms. Personally I would keep one form so it stays where the user puts it. Then everything you are currently making new Forms for should be new UserControls. You can then keep a single instance of the object and just swap out user controls on your form. The menubar stays in place, the statusbar stays in place. Just the content in the middle changes.
Was This Post Helpful? 0
  • +
  • -

#8 Dinaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 10

Re: How do I get an object's instance?

Posted 08 February 2012 - 09:04 AM

View PosttlhIn`toq, on 08 February 2012 - 08:52 AM, said:

You have a real design problem. If the object is created in form3, and you pass that to form4, then CLOSE form3 (the parent form of the object) the object can potentially be disposed of.

Yes you can pass the object as a constructor parameter. But you should then clone it so it is its own fresh new object that belongs to the receiving form.

But honestly - I don't care for the description of the program design: Constant creation and destruction of forms. Personally I would keep one form so it stays where the user puts it. Then everything you are currently making new Forms for should be new UserControls. You can then keep a single instance of the object and just swap out user controls on your form. The menubar stays in place, the statusbar stays in place. Just the content in the middle changes.


I'm sure it's not quality design by any means. This is my first project and it's meant to be more of a learning experience. I see what you're saying though, and the method that I just linked is what some people gave me as advice on here for starting up new forms. I knew even less about what I was doing then. Personally I don't know how to do what you just described, but it sounds awesome!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1