A get or set accessor expected?

Mike_R

Junior Contributor
Joined
Oct 20, 2003
Messages
316
Location
NYC
Please forgive me for what could bee the noob question of the year...

Normally I use VB.NET, but fooling around with C# today, I couldn't even get to first base. I attempted the following code:
C#:
            static void Run
            {
               MessageBox.Show("hello");

            }
But the compiler complained about the MessageBox.Show call, complaining that:
A get or set accessor expected
(See the screen shot below.)

Can anyone tell me what ridiculously simple thing I am missing here?

Thanks in advance!
Mike
 

Attachments

This is a function
C#:
static void Run
{
   ...
}

This is a method:
C#:
static void Run()
{
...
}

For your function though... It would require a valid type.
Exemple:

C#:
public int Age
{
   get { return m_Age; }
   set { m_Age = value; }
}
 
The parentheses make the difference between a function/property, i.e.
C#:
public int Function(){
}
 
public int Property{
}
In a property, you provide two (or at least one) accessor functions name get and set (very similar to VB).
C#:
int _Property
public int Property{
    get {
        return _Property;
    }
    set {
        _Property = value;
    }
}
You can omit one or the other to make read-only or write-only properties.

VB is very lax with parentheses and always adds them for you when you forget them. If you keep using C# you will notice lots of places where VB throws things in or auto-corrects for you that C# doesn't (usually because it can't, for example, adding parentheses for you will turn a property into a function).
 
Ah, ok, thank you guys, I really appreciate it!

Funny place for it to put the red-squigly-underline, though. It gives every impression that my call to MessageBox.Show() was invalid, whereas it really should be underlining the word 'Run', I would think, no?

Anyway, squigly-gripes aside, I thank you both for your exceptionally clear explanations. :)

-- Mike
 
Back
Top