Go Back  Xtreme .NET Talk > .NET > Syntax Specific > Visual C# .NET > A get or set accessor expected?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2006, 12:12 PM
Mike_R's Avatar
Mike_R Mike_R is offline
Junior Contributor

Preferred language:
C#
 
Join Date: Oct 2003
Location: NYC
Posts: 316
Mike_R is on a distinguished road
Default A get or set accessor expected?



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:
Code:
static void Run
            {
               MessageBox.Show("hello");

            }
But the compiler complained about the MessageBox.Show call, complaining that:
Quote:
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
Attached Images
File Type: jpg Screen Shot.JPG (6.0 KB, 23 views)
__________________
Posting Guidelines

Avatar by Lebb
Reply With Quote
  #2  
Old 05-23-2006, 03:34 PM
Arch4ngel's Avatar
Arch4ngel Arch4ngel is offline
Senior Contributor

Preferred language:
C#, ASP.NET, VB.NET
 
Join Date: Mar 2004
Location: Montreal, QC
Posts: 940
Arch4ngel is on a distinguished road
Default

This is a function
Code:
static void Run
{
   ...
}

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

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

Code:
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
Reply With Quote
  #3  
Old 05-23-2006, 04:01 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default

The parentheses make the difference between a function/property, i.e.
Code:
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).
Code:
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).
__________________
e
Reply With Quote
  #4  
Old 05-23-2006, 04:20 PM
Mike_R's Avatar
Mike_R Mike_R is offline
Junior Contributor

Preferred language:
C#
 
Join Date: Oct 2003
Location: NYC
Posts: 316
Mike_R is on a distinguished road
Default

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
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Immediate Window not functioning as expected. EracMan General 2 06-07-2006 04:03 PM
'Declaration expected' error kkirtac Visual Basic .NET 5 05-22-2006 11:55 AM
vb.net - listview not working as expected Aaron Gold Windows Forms 0 10-22-2004 09:07 AM
myConnection.Open() --Declaration Expected pcq0125 Database / XML / Reporting 2 07-23-2004 05:10 PM
What does type expected mean ? loyal Windows Forms 8 08-16-2003 04:11 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe