Switch not working correctly

  • Thread starter Thread starter PhreakRox
  • Start date Start date
P

PhreakRox

The ToClose switch in this program is not working as expected, it
allways returns a null value, if anyone knows a way to fix up the code,
or can suggest a better method of doing so, your help would be greatly
appreciated

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace sampleProgram
{
class InitForms
{
sampleProgram.Properties.Settings settings = new
sampleProgram.Properties.Settings();

sampleProgramAbout About;
sampleProgramHelp Help;
sampleProgramMain Main;
sampleProgramSettings SettingsForm;

public InitForms()
{

}

public void InitMajorForms()
{
if (settings.boolFirstRun == true)
{
settings.justOpened = false;
settings.Save();

About = new sampleProgramAbout();
About.Visible = false;
About.Show();

Help = new sampleProgramHelp();
About.Visible = false;
Help.Show();

Main = new sampleProgramMain();
Main.Visible = false;
Main.Show();

}
else
{
settings.justOpened = false;
settings.Save();

About = new sampleProgramAbout();
About.Visible = false;
About.Show();

Help = new sampleProgramHelp();
Help.Visible = false;
Help.Show();

SettingsForm = new sampleProgramSettings();
SettingsForm.Visible = false;
SettingsForm.Show();

}
}

public void LoadForm(String ToOpen, String ToClose)
{
System.Windows.Forms.Form FormToClose;

switch (ToClose)
{
case "Main":
FormToClose = Main;
break;
case "Help":
FormToClose = Help;
break;

case "Settings":
FormToClose = SettingsForm;
break;

case "About":
FormToClose = About;
break;

default:
FormToClose = Main;
break;
}

switch (ToOpen)
{
case "Main":
Main.Location = new Point(FormToClose.Location.X,
FormToClose.Location.Y);
Main.Visible = true;
FormToClose.Visible = false;
break;

case "Help":
Help.Location = new Point(FormToClose.Location.X,
FormToClose.Location.Y);
Help.Visible = true;
FormToClose.Visible = false;
break;

case "Settings":
SettingsForm.Location = new
Point(FormToClose.Location.X, FormToClose.Location.Y);
SettingsForm.Visible = true;
FormToClose.Visible = false;
break;

case "About":
About.Location = new Point(FormToClose.Location.X,
FormToClose.Location.Y);
About.Visible = true;
FormToClose.Visible = false;
break;
}
}
}
}
 
I called it correctly with both arguements, I managed to correct the
problem, I changed the switch to multiple if/if else statements, then
declared the variables correctly, as I had previously did it
incorrectly, however, thankyou for your help
 
PhreakRox said:
I called it correctly with both arguements, I managed to correct the
problem, I changed the switch to multiple if/if else statements, then
declared the variables correctly, as I had previously did it
incorrectly, however, thankyou for your help

In that case, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Have you set a breakpoint and make sure your ToClose value is desired? If
still cannot, yes, dump you cs file here.

chanmm
 
yes I did, however I could not find a solution, instead I opted to
convert all my forms into singletons
 
PhreakRox said:
The ToClose switch in this program is not working as expected, it
allways returns a null value, if anyone knows a way to fix up the code,
or can suggest a better method of doing so, your help would be greatly
appreciated

We'll need to see a *complete* program in order to try to reproduce
your problem.

Jon
 
Jon I no longer require assistance, as I have found a solution, I
decided to code all my forms as singletons, thankyou for your help
however.
 
PhreakRox said:
Jon I no longer require assistance, as I have found a solution, I
decided to code all my forms as singletons, thankyou for your help
however.

Well, I'm glad you're sorted - but I would suggest you might still want
to get to the bottom of what was going on. I'm pretty sure that it's
not "switch" itself that's the issue.

Jon
 
I will look into that as soon as my project as complete, as it does
interesnt me, thankyou for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top