Well, you could modify your fullNameValue field to be the following code, instead. I think this would resolve your error.
The issue seems to be that your initialization code for your fullNameValue field is referencing the Data property (Data["FirstName"]
), but Data is an instance property, not a static property, therefore you can't reference it in a static context (i.e. when initializing a static field).
static object _syncLock = new object();
static string _fullNameValue;
string fullNameValue
{
get
{
lock (_syncLock)
{
if (_fullNameValue == null)
{
_fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FirstName"]));
}
return _fullNameValue;
}
}
}
It looks like you're using this for unit testing purposes. In that case, it looks to me like this code will generate a single value for fullNameValue and will reuse that for all test cases.
If that's what you want, then this would be ok. However, my guess is that you may find that this code behaves incorrectly if you start to use different test data for different tests, although I'm not familiar with the ArtOfTest framework. If this gives you trouble, then you might want to reconsider whether the _fullNameValue field should be static
.
Alternatively, as discussed in the comments, you could make the fullNameValue field to be non-static, and then initialize it in the constructor. Below is the code:
string fullNameValue;
string companyValue;
public PricingForm()
{
fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FirstName"]));
companyValue = // code to initialize company value
}
Yet another alternative: you might want to try converting the fullNameValue field to a property. Again, this may produce different behavior than the examples above. Does each call to GenerateUniqueId return a different value, even if the input parameter is the same? Then that means each time that you access this property you will get a different value back, so consider whether this is what you want.
string fullNameValue
{
get { return UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(this.Data["FirstName"])); }
}
In light of the discussion in the comments, my suggestion is to use the approach shown below.
This performs lazy initialization of the _fullNameValue field (which appears to be necessary because the BaseWebAiiTest class' Data property is not initialized at the time that the constructor executes). I've left out the locking code because it may not be necessary and the OP expressed concerns about the verbosity of it.
string _fullNameValue;
string fullNameValue
{
get
{
if (_fullNameValue == null)
_fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(this.Data["FirstName"]));
return _fullNameValue;
}
}