Reputation: 448
I have just started learning C# and in the answers of specific test on the following code it was mentioned that :
The set property for Tak is either missing or incorrect.
The code is consisted of 2 classes.
Class 1 with main:
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Takis obj = new Takis();
obj.Val = 23.4;
Console.WriteLine(obj.Val);
}
}
}
Class 2:
namespace ConsoleApplication6
{
class Takis
{
double val;
public double Val
{
get
{
return val;
}
set
{
val = 3.14;
}
}
public Takis()
{
}
}
}
In Visual Studio i am gettting NOT ALWAYS the following:
'ConsoleApplication6.vshost.exe' (CLR v4.0.30319: ConsoleApplication6.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'ConsoleApplication6.vshost.exe' (CLR v4.0.30319: ConsoleApplication6.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The thread 0x1610 has exited with code 259 (0x103). The thread 0x1638 has exited with code 0 (0x0). The thread 0x13f4 has exited with code 259 (0x103). 'ConsoleApplication6.vshost.exe' (CLR v4.0.30319: ConsoleApplication6.vshost.exe): Loaded 'c:\users\stefanos\documents\visual studio 2013\Projects\ConsoleApplication6\ConsoleApplication6\bin\Debug\ConsoleApplication6.exe'. Symbols loaded. The thread 0x1480 has exited with code 259 (0x103). The thread 0x1498 has exited with code 259 (0x103).
Since i am really new to Visual studio and in C# in general i would appreciate it if someone could show me the error or explain to me what is wrong.
Upvotes: 0
Views: 441
Reputation: 37020
The set
for property Val
always sets the property to the same value. Instead, it should most likely set the property to the value entered by the user:
set { val = value; }
Alternatively, if the intent was for the value to always be 3.14 (i.e. read-only), then the setter could be removed and the get would return the value:
class Takis
{
public double Val
{
get { return 3.14; }
}
public Takis()
{
}
}
Upvotes: 4
Reputation: 77294
Your property is not behaving the way that properties normally do. When I set a value like 23.4, I expect that to stick. In your case, your class does not care what I set. When I set something, anything really, the property will be 3.14.
Your property should set the value that was given (23.4 in this case). You can do this by using the value
keyword:
public double Val
{
get
{
return val;
}
set
{
val = value;
}
}
Upvotes: 0
Reputation: 1
The set in your Val property should be val = value; value
in C# being a keyword, is the value the property is assigned with instantiated.
Upvotes: 0