[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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?
0
pieter78
Asked:
pieter78
  • 10
  • 9
2 Solutions
 
b1xml2Commented:
where is DiseaseArray instantiated???
0
 
pieter78Author Commented:
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..
0
 
b1xml2Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pieter78Author Commented:
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?
0
 
b1xml2Commented:
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;
0
 
pieter78Author Commented:
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?
0
 
b1xml2Commented:
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.
0
 
b1xml2Commented:
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.
0
 
pieter78Author Commented:
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?
0
 
b1xml2Commented:
The question here is what exactly is DiseaseArray when you pass it as such to the MessageBox.Show method.

Is it null and for which the frmMain.DiseaseArray[0,0] really does not exist!

Static members are available to all instances of the class.

In your case when you create an instance of frmMain and then populate the static member DiseaseArray since it is (I presume done inside the frmMain class body), then all is good. The question here is very much about scope for which I do not actually recommend using the static member.

This is what I had in mind:

Application.Run(new frmResults(frmMain1.DiseaseArray));
 where frmMain1 is an instance reference of the frmMain class and where DiseaseArray is an instance member.
0
 
pieter78Author Commented:
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..
0
 
b1xml2Commented:
well you could do this:

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

where DiseaseArray is a public instance member:

public string[,] DiseaseArray;
0
 
pieter78Author Commented:
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...
0
 
pieter78Author Commented:
Btw: thanks for your patience! ;-)
0
 
b1xml2Commented:
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
0
 
pieter78Author Commented:
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.
0
 
b1xml2Commented:
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.
0
 
b1xml2Commented:
notice the slight shift in casing. It will compile but will die at runtime if the property is asked for anywhere in the code....
0
 
pieter78Author Commented:
Well.. let's say there are many things left to learn for me... ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now