views:

170

answers:

2

hi friends i have problem with using get or set in class in c# when i use get or set in gives error(invalid token { in class) pls, see below code,i have this problem in it

static int abcd
{  
    get           
    {  
       return  _abcd; 
    }  
}  

thanx

this is the complete code,i dont have this problem with any of your codes but just this:

namespace ConsoleApplication2
{
   class Program
   {
      class Car
      {
         private int _speed;  
         public int Speed;
         {
              get
              {
                return _speed
              }
          }
       }
   }
}
+7  A: 

There are two errors in your code.

  • You have a semicolon where there shouldn't be one (spotted by Oded).
  • You are missing a semicolon where there should be one.

Try this instead:

namespace ConsoleApplication2
{
    class Program
    {
        class Car
        {
            private int _speed;  
            public int Speed     // <-- no semicolon here.
            {
                get
                {
                    return _speed;  // <-- here
                }
            }
        }
    }
}

I noticed that the code you originally posted was formatted badly. I would suggest that you format your document automatically in Visual Studio to make the braces line up. This should make the error more obvious. When the formatting of the code looks wrong you know that there is an error nearby. You can find this option in the menu: Edit -> Advanced -> Format Document or use the keyboard shortcut (Ctrl-E D for me, but might be different for you, depending on your settings).

I would also suggest that you consider using auto-implemented properties instead of writing the getter out in full:

namespace ConsoleApplication2
{
    class Program
    {
        class Car
        {
            public int Speed { get; private set; }
        }
    }
}
Mark Byers
You may want to drop the semicolon after `public int Speed` in your first example.
Dirk D
@Dirk D: Thanks. So actually there were two errors in his code! It just shows that the auto-implemented property is much easier to get right and definitely the way to go for simple properties.
Mark Byers
@Mark Byers: Actually it was @Oded who spotted it first in his answer :)
Dirk D
@Dirk D: Oh, right I didn't see that. But to be fair, you were the one that noticed the error in my code... so huge credits for that. Thanks.
Mark Byers
+8  A: 

The snippet you have posted is fine as it is, also in regards to the error, as it has the correct number of { to } and in the right order.

Look at where you have placed it (possibly outside of a class), or look for extra } in the file.

Update: (following edit in question)

Your issue is here:

public int Speed; // <-- ; should not be here

And:

return _speed // <-- missing the ;

The property should be implemented like this:

public int Speed
{
   get
   {
      return _speed;
   }
}
Oded
this is the complete code,i dont have this problem with any of your codes but just this: class Car{ private int _speed; public int Speed; { get { return _speed }}
arash
@arash - answer updated. You did not implement the property correctly.
Oded
wow.it worked.thaaannx veryyy much
arash
you may want to accept his answer :)
grilix