Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

           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

Posting Guidelines

 

Avatar by Lebb

Posted

This is a function

static void Run
{
  ...
}

 

This is a method:

static void Run()
{
...
}

 

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

Exemple:

 

public int Age
{
  get { return m_Age; }
  set { m_Age = value; }
}

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

  • Leaders
Posted

The parentheses make the difference between a function/property, i.e.

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).

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).

[sIGPIC]e[/sIGPIC]
Posted

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

Posting Guidelines

 

Avatar by Lebb

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...