Solved

main method in c#, starting routine

Posted on 2014-10-02
9
131 Views
Last Modified: 2014-10-02
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?
0
Comment
Question by:Mike Eghtebas
9 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 40357115
Application.Run( new frmCS01());
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357214
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357227
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?
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
ID: 40357249
Just an FYI you're better off creating a static class.

eg:

public static class Globals
{
          public static string cnString = "";
}

then you can call

Globals.cnString from anywhere
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357311
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 40357339
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.
It's probably of no consequence to the topic at hand, but for the sake of completeness, when will the MessageBox below be visible?

int x = 0;

while (true)
{
    x++;
}

MessageBox.Show("Test");

Open in new window


What about this one?

int x = 0;

while (x < 10)
{
    x++;
}

MessageBox.Show("Test");

Open in new window


A message loop in a Windows Form is very very similar to this.

FYI, I removed the message box in the form...but when I closed the form, the message in the main didn't fire...
Well of course not...you removed it! Try running this code. Close the form and see if you see the MessageBox:

static void xMain ()
{
    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);
}

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357413
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357422
What is the error?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357446
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel import error " External table is not in expected format" 12 48
Vb.net dynamic formulas in runtime 11 61
Syntax Error 2 43
Help with C#, MVC, razor. 6 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now