nikhilk/scriptsharp — Issue #128: Properties Set is incorrect
Author: jimmygilles
Created: 2011-11-26 21:04:26+00:00
State: open

Hello,

I found a problem with the "set" of a property. Here is an example (I know this is not a common code but everything is possible) :

```c#
using System;
namespace Demo
{
public static class Class1
{
private static Int32 _val = 0;

public static Int32 Val
{
get { return _val; }
set
{
if (value > 0)
return;

_val = value - 100;
}
}

public static void Test()
{
Int32 v = Val++;
// ... do something with v
}
}
}
```

This code is converted into javascript like that:

```javascript
Demo.Class1.get_val = function Demo_Class1$get_val() {
///
return Demo.Class1._val;
}
Demo.Class1.set_val = function Demo_Class1$set_val(value) {
///
if (value > 0) {
return;
}
Demo.Class1._val = value - 100;
return value;
}
Demo.Class1.test = function Demo_Class1$test() {
var v = Demo.Class1.set_val(Demo.Class1.get_val() + 1) - 1;
}
```

I see multiple problems here:
1) The first "return" returns nothing
2) The second "return" returns "value" instead of "_val". A solution would be to return a call to the "get_val" method.
ex: "return Demo.Class1.get_val()"
3) But... calling the get method automatically on each return is not a good solution. Because this can result in a lot of unecessary processing (the get method can be a complexe method).

A better solution, in my opinion would be:
Removing all return in the set method.

And converting
Int32 v = Val++;
into
var v = Demo.Class1.get_val(Demo.Class1.set_val(Demo.Class1.get_val() + 1) - 1);
(passing a parameter to the get_val method is not a problem in javascript)
The first "get_val" (on the left) can be removed if it is not necessary.

What do you think ?
Thanks !