Link to home
Start Free TrialLog in
Avatar of pieter78
pieter78

asked on

Variable scope problem

I want to pass a variable from Form1 to Form2, and the variable is a two-dimensional array of type string[,].
Now, in Form2 I have code:

            public frmResults(string[,] DiseaseArray)
            {
                  InitializeComponent();
                  string[,] currDiseaseArray = DiseaseArray;
                  lblOne.Caption = currDiseaseArray[0,0];
            }

and later on it says:

            static void Main()
            {
                  Application.Run(new frmResults(DiseaseArray));
            }

There is something wrong with this second part, I get error CS0103 saying "The name 'DiseaseArray' does not exist in the class or namespace 'CZ_demo_hoofdpijn.frmResults'".

I don't know how to solve this. If I leave DiseaseArray away, I get an understandable error that this method overloading does not take 0 arguments.

I guess it has something to do with the scope, the Main() does just not know about my array. But how do I solve this?
Avatar of b1xml2
b1xml2
Flag of Australia image

where is DiseaseArray instantiated???
Avatar of pieter78
pieter78

ASKER

I declared DiseaseArray and filled it with content in frmMain, and now I want to use it to display data in frmResults.

frmMain:
---------------------------------------------
string[,] DiseaseArray = null;
// some other code
DiseaseArray[i,0] = Headache[i,0];
DiseaseArray[i,1] = Chance.ToString();
// other code
frmResults ResultsForm = new frmResults(DiseaseArray);
ResultsForm.Show();
---------------------------------------------

frmResults:
---------------------------------------------
// as written above (in question)
---------------------------------------------

This worked when I used a string called Disease instead of the Array... just after changing that, things got wrong... (and yes, I need the array ;-) )

Hope this is what you meant..
static void Main()
{
      Application.Run(new frmResults(DiseaseArray));
}

will be a problem because you'd need to get the reference of the instance of frmMain, and then do like so:
Application.Run(new frmResults(frmMain1.DiseaseArray));

and you'd have to make DiseaseArray public
Getting better...

In frmMain, I deleted my declaration from within the btnSubmit_ClickEvent, and now I put
-----------------------------------------
public string[,] DiseaseArray = null;
-----------------------------------------
somewhere in the class itself (instead of some subroutine).
The compiler has no problems with that.

Then, in frmResults, I put:
-----------------------------------------
static void Main()
{
      Application.Run(new frmResults(frmMain.DiseaseArray));
}
-----------------------------------------

which now gives me error CS0120 saying "An object reference is required for the nonstatic field, method, or property 'CZ_demo_hoofdpijn.frmMain.DiseaseArray' "

So... quite close, I guess, but what now?
static void Main()
{
     Application.Run(new frmResults(frmMain.DiseaseArray));
}

would require that DiseaseArray be a static member like so:

public class frmMain : Form
{
      public static string [,] DiseaseArray = null;
}

as I said, you need to get the reference to the INSTANCE of the frmMain class you instantiated, unless you want DiseaseArray to be available to all instances of the frmMain class , in which case, the syntax provided above is valid;
Very weird... I did as you said, so
----------------------------------------------------------
public static string[,] DiseaseArray = null;
----------------------------------------------------------
 And then, the compiler did not gave any errors and the program ran. So I was about to be happy, say thanks and close this question, when I noticed that after I click the button that should do the trick, it goes wrong....

To test, I created a button with a MessageBox that should appear when I click on it.

When I use MessageBox.Show(Headache[0,0]) it goes fine (Headache is my original array with all the values, and I use DiseaseArray to display the calculated values).

Then, in my code is written:
----------------------------------------------------------
DiseaseArray[i,0] = Headache[i,0];  // in a for-loop
----------------------------------------------------------

but when I try MessageBox.Show(DiseaseArray[0,0])  there is NO reaction. The MessageBox even does not show up.

Any idea? I don't know whether this has to do with the scope, but it's funny that is does react on my original array, but not on this one. Could the "static" be involved in this?
The thing though is if you use a static member, unless you have created an instance before of the frmMain class and populated the static member, it is null.

I'd say that it is better to get the instance of the frmMain class and access the instance member instead of the static member.

I am pleased that you did not just gave the points just yet. It is always paramount to test the code first. It breeds good habits for you.
as a matter of good coding, if you are calling static members, you should always indicate the class as in
MessageBox.Show(frmMain.DiseaseArray[0,0]) but this is just an aside.
Quote:
******************************************************************************************************
The thing though is if you use a static member, unless you have created an instance before of the frmMain class and populated the static member, it is null.
I'd say that it is better to get the instance of the frmMain class and access the instance member instead of the static member.
******************************************************************************************************

I am not sure if I understand that. But anyhow, how come that the MessageBox won't even pop up?? I mean, I could understand if I got an empty string, but it just refuses to show itself. What do you think of that? Ideas for a solution?
SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
my current frmMain is declared as
-------------------------------------
public sealed frmMain: Form
-------------------------------------
and this includes the string[,] DiseaseArray

Now what should I do?

Like:
frmMain frmMain1 = new frmMain();
and then use frmMain1 like you did in frmResults

But:
1) where do I put the frmMain1 declaration? in frmMain, I guess, or in frmResults?
2) (I am not familiar with instantiation) DiseaseArray: declared in frmMain, or in frmMain1?

Thanks..
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First, when I do that I get two errors...

1. Static member 'CZ_demo_hoofdpijn.frmMain.DiseaseArray' cannot be accessed with an instance reference; qualify it with a type name instead
2. The type or namespace name 'frmMain1' could not be found (are you missing a using directive or an assembly reference?)

Second, the actual problem is that the second form (frmResults) cannot be reached. If I uncomment
------------------------------------------------------------------------------------------------
DiseaseArray[i,0] = Headache[i,0];
DiseaseArray[i,1] = Chance.ToString();
// this is some code in a for-loop that I use to populate the array
------------------------------------------------------------------------------------------------

everything after this code seems to be ignored. If I comment the DiseaseArray and use MessageBox.Show(Headache[0,0]) instead, there is no problem. But when I use DiseaseArray[0,0] instead (after uncommenting of course) nothing happened, the whole MessageBox even won't show up.

Therefore I am wondering what is going wrong...
Btw: thanks for your patience! ;-)
you'd need to have the correct namespace
such as
usiing myForms;
 because it appears that frmMain is an unknown class.

change public static string [,] DiseaseArray to public string [,] DiseaseArray
Well, I guess I am definitely going mad now... I did what you teld, grumbled because I did not understand, tried harder, read the help file of the error I got, started to understand what you are trying to tell me, did that, got another error, declared myself stupid for not reading well, changed code, re-checked (Ctrl-Shift-B) and YES>>> compiler worked, so I was looking forward to see the program in action finally:

ERROR:
An unhandled exception of type 'System.StackOverflowException' occurred in CZ demo hoofdpijn.exe

.... and it marked my array which includes the data yellow. Perhaps related to Crossfire, I don't know anymore. I feel like what you say in your profile: start again at step 1.

Let me know if you have additional comments, but I guess my question has been answered. There seems to be another problem for now, which I have not been able to solve yet....

Anyway, I appreciated your help ~ thanks for that.
a tip about this StackOverflowException...
it commonly occurs when there is a reference that loops through itself.

e.g.
private string myValue

public string MyValue
{
  get { return myValue == null ? string.Empty : MyValue; }
}

in the case where the private myValue member has a valid value (non-null), a stack overflow will occur when something asks for this property.
notice the slight shift in casing. It will compile but will die at runtime if the property is asked for anywhere in the code....
Well.. let's say there are many things left to learn for me... ;-)