How do I create public variables/functions in C#? + Other basic questions

matt_cowdell
matt_cowdell used Ask the Experts™
on
I am new to C Sharp .Net and I have a few questions.

I am moving on from VB.Net, and I've noticed a lot of differences.

How am I meant to create public variables, procedures and functions?

In VB.Net I could create a module with a procedure/function in it and call that procedure/function from anywhere in my program. How is this done in C#?

Also, in VB.Net I could go into code, i.e. Form_Open and click the combo box in the top right of Visual Studios 2005 and select from any of the other events like Form_Close. I have found how to add these events I think; by adding them into the designer.cs file of the form. Is this the only way?

And, can someone explain what all the difference public/static/void/etc terms mean and what the VB.Net equivalent (if any!) is?

Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
Commented:
public variables, yea u cant like vb.net. One way to achieve this is to create a static class
eg

static class MyGlobals
{
    private string m_dummy = "";

    public static string Dummy
    {
        get {
            return m_Dummy;
        }
        set {
            m_Dummy = value;
        }
    }
}

Now u can reference it like this

something = MyGlobals.Dummy;


In C#, u need to create classes and use them that way instead of defining modules and calling them

Not sure about your event handlers

public means its accessible to the outside world. i.e. public procedure fred
static means a it still exists even when the block of code it was initialised in terminates. U can use static in vb
void means it doesnt return anything, much like a procedure (sub) instead of a function in vb
icr
Commented:
http://msdn2.microsoft.com/en-us/library/x53a06bb(vs.71).aspx is a list of keywords which you can click for definitions and examples.
http://www.codeproject.com/dotnet/vbnet_c__difference.asp is a list of differences between VB and C#. The differences and usage aren't clearly explained, but it should be a good starting point for what to google.
http://support.microsoft.com/kb/308470 is perhaps a better resource. A lot of the information is available online dotted around msdn, but this provides a nice central document. For some reason it's a downloadable, self extracting doc file :S

Author

Commented:
Thanks! those explanations are great!
Thanks. Explanations more help full people are moving from vb.net to c# like me

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial