1

I have game with three levels on three different forms. I save each level's completion time and want to access it on the 4th form. I've done this:

public partial class Results : Form
{
    public int  time1, time2, time3;
    FormLevel1 rez1 = new FormLevel1();
    FormLevel2 rez2 = new FormLevel2();
    FormLevel3 rez3 = new FormLevel3();

    public Results()
    {
        InitializeComponent();


    }
    void Calculations()
    {
        time1 = rez1.levelTime;
        time2 = rez2.levelTime;
        time3 = rez3.levelTime;
        MessageBox.Show(time1.ToString());
        MessageBox.Show(time2.ToString());
        MessageBox.Show(time3.ToString());
    }
}

I get all zeros. I guess I'm doing something wrong. How can I solve this problem correctly? Thanks.

5
  • How are you updating levelTime? Commented Nov 29, 2015 at 21:05
  • @KiwiPiet I have a timer in each of levelForms, im just saving time at the moment when the level ends. Commented Nov 29, 2015 at 21:08
  • Do you create your forms there and show them from there or how do you show/switch between them? Commented Nov 29, 2015 at 21:09
  • What about a class with all the properties you need. This class is passed into these forms. If all places use the same instance you don't have to bother about "passing data"... Commented Nov 29, 2015 at 21:10
  • @Martin Can you show us from where did you call the Calculations() method? Commented Nov 30, 2015 at 3:19

2 Answers 2

1

With

FormLevel1 rez1 = new FormLevel1();
FormLevel2 rez2 = new FormLevel2();
FormLevel3 rez3 = new FormLevel3();

you are creating new instances of the three forms, not using the "previous" instances you want to use.

You need (e.g.) public properties to pass the 3 "previous" forms the the instance of Form4 (or any method to achieve the same).

But Actually, consider what you really need to pass to your form Results: from your code, it seems you just need to pass 3 integer (levelTime for each form)

[Edit] Just realize now your time1, time3 and time3 member variables are public. So in the calling code you can do something such:

Result resForm = new Result();
resForm.time1 = ... // have you saved result of form1 in a variable? use it here!
resForm.time2 = ... // same for form2
resForm.time3 = ... // same for form3
resForm.ShowDialog();
Sign up to request clarification or add additional context in comments.

3 Comments

What are those public properties and how can I do that? Sorry, if that's a stupid question. I'm self-taught and still kinda new to this.
do i have to do this in each levelform?
I have done that in each of level forms. It seems ok. But how do I get those values in Results(Form 4) now? When I write in results form like this: MessageBox.Show(time1.ToString()); It's still writes 0.
0

let me try to explain what you are doing wrong here (based on my assumption of where you do your Calculations(). Pardon me if I am wrong.)

public partial class Results : Form // (0)
{
    public int  time1, time2, time3;
    FormLevel1 rez1 = new FormLevel1(); //(1)
    ...

    public Results()
    {
        InitializeComponent(); 
        Calculations(); // (2) Assuming you call Calculations() here
    }

    void Calculations()
    {
        time1 = rez1.levelTime;
        MessageBox.Show(time1.ToString()); //(3)
        ...
    }
}

(0) At Time=0, you created your Results class instance

(1) At Time=0, you created your FormLevel1 class instance

(2) At Time=0, you do your Calculations()

(3) At Time=0, you show your message box of the FormLevel1's levelTime

i.e. (0) to (3) happened at (almost) the SAME time! Your FormLevel1 has no chance to do and complete it's Timer stuff BEFORE the Results do its Calculation(), hence the messageBox shows 0.

Without you explaining more about what you are trying to achieve here OR giving us your FormLevel's code, we cannot give you more specific solutions.

One possible solution is to call your Results.Calculation() FROM your FormLevels AFTER they finish doing their Timer stuff.

How?

public partial class Results : Form 
{
    ...
    FormLevel1 rez1 = new FormLevel1(this); //pass this instance of Results into FormLevel1
    ...

    public void Calculations() //Add public keyword so that it is accessible from FormLevel1
    {
        ...
    }
}

public partial class FormLevel1: Form 
{
    Results resultForm;

    public FormLevel1(Results callingForm) //modify constructor to accept Results
    {
        resultForm = callingForm;  
    }

    void TimerCompleted() //Call this when Timer finish its stuff
    {
        resultForm.Calculations();
    }

    ...
}

Note that this may not be EXACTLY the solution you want. But this is generally the simplest way to pass data between Forms, answering your Title question. (FormLevel1 can now access ALL its callingForm's public variables and methods)

Hope that it helps your understanding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.