Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

main method in c#, starting routine

In csNorthwind.xml, I have the connection string. At the start of the application, I want the connection string to be stored in a global variable cnString
using Microsoft.VisualBasic;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml;

namespace CodeSamplesCS
{
    class Main
    {
        public static string cnString;
        static void xMain ()
        {

          //  try
           // {
                XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
                cnString = xmlReader.ReadElementString("connection");
                //*** Enable MS themeing for the app
                Application.EnableVisualStyles();
                //*** Run the app with the main form
                //Application.Run('frmCS01');
                MessageBox.Show(cnString);

            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Progrma Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
    }
}

Open in new window


My starting form is frmCS01 and has no codes in it yet. Without line 23, as expected, the blank form (frmCS01) opens up. But with line 23 included, I get:  "Error      1      'CodeSamplesCS.frmCS01' is a 'type' but is used like a 'variable'      

Question: How can I correct the above code such that Main will fire first and then open frmCS01?
SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada 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
Avatar of Mike Eghtebas

ASKER

That takes care of line 23. But, the message box does I added:

MessageBox.Show("Message in Main: " + cnString);

and

MessageBox.Show("Message in frmCS01: " + cnString);

need to be fired. Neither do.

MessageBox.Show("Message in frmCS01: " + cnString); also doesn't see cnString variable.

Please take a look at lines 10 and 13 (they are not correct I think).

Thanks,

Mike
Avatar of kaufmed
Application.Run is a blocking call. Your message boxes won't run until you close the form. Take it from the documentation:

Begins running a standard application message loop on the current thread...

What happens with code that comes after a loop? It won't run until the loop ends, right?
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
Kaufmed,

re> Begins running a standard application message loop on the current thread...

I don't understand what this means/ what I should be doing.

FYI, I removed the message box in the form. I ran it; but when I closed the form, the message in the main didn't fire (did you mean it will fire?).

re:> What happens with code that comes after a loop? It won't run until the loop ends, right?

I do not know what loop you are referring to.
-----------------------------------
Kyle Abrahams,

Where do I put this code? In the form or in the main? And where it should be positioned?

Thanks,

Mike
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
Kaufmed,

Thank you for the explanations. When I put back the message box without cnString variable like:

MessageBox.Show("Message in frmCS01: ");

It fires of course. But, with cnString set in Main(), it errors. How can I correct the scope of cnString in main (Kyle added a comment but I was not sure how and where I could apply it. I tried a few scenarios but couldn't make it work).

Mike
What is the error?
I need to close this question and start new one because I was not aware that Program.cs already has the Main method. Therefore, I had started a new code file to make a main method and this is why I was forced to name it xMain etc.

In a few minutes, I will be able to figure out what my question is and post it.

Regards,

Mike