I use static variables in the beginning of code file because I use it later for verifying values:
static string fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FirstName"] ));
public void FillName() {
Pages.SitecoreCMS.Field_Company.Text = fullNameValue;
}
// break.............
public void VerifyingFullName() {
Assert.IsTrue(ArtOfTest.Common.CompareUtils.StringCompare(Pages.Contact.FrameContentIFrame.SitecoreTentativeaccountnameText.Text,fullNameValue, ArtOfTest.Common.StringCompareType.Contains));
}
Compilation is failed: An object reference is required for the non-static field, method, or property ‘ArtOfTest.WebAii.Design.BaseWebAiiTest.Data.get’
How I should change this code?
I used non static variables before, but I can’t to use it by another methods (e.g. VerifyingFullName).
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).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:
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.
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.