Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class property is a 'property' but is used like a 'type'

Tags:

c#

I created a class call Sales with a Amount get/set:

class Sales 
{

 public static string Amount { get; set; }

}

Then I called from other class, out of a function scope:

class Test
{

  Sales.Amount = "10";

  public static VoidSales() 
  {
   .....
  }

}

I get the following error:

'Fun.Sales.Amount' is a 'property' but is used like a 'type'

But When I use in a function, it is correct.

Thank you.

like image 441
Alvin Avatar asked Jan 16 '23 16:01

Alvin


2 Answers

You can't place these type of assignment statements inside the open class context. You can only make these type of assignments inside a method. The only assignments that can be done in the class context are initializations of class level fields.

If this assignment needs to be made when the class is instantiated, then it should be done in the constructor.

like image 136
jdmcnair Avatar answered Jan 20 '23 16:01

jdmcnair


I may not be able to explain it well. But a get/set property is actually sort of equivalent to a method. Having said that, I believe you can't call a method outside of a function right?

like image 34
C_Rance Avatar answered Jan 20 '23 16:01

C_Rance