Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:22:21+00:00 2026-05-28T00:22:21+00:00

I use static variables in the beginning of code file because I use it

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:22:22+00:00Added an answer on May 28, 2026 at 12:22 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's impossible to use static variables on a session bean code. Is this restriction
A colleague of mine told me that I should never use static variables because
Is it reasonable to use private static variables to establish invariants in your class?
I heard that static methods should use only static variables in java. But, main
Checked ActiveRecord, DataMapper, Sequel: some use globals (static variables) some require open db connection
I have some C# library classes that make use of static variables. I use
When I use static variables in C++, I often end up wanting to initialize
Is it better to use static const variables than #define preprocessor? Or does it
In what segment (.BSS, .DATA, other) of an executable file are static variables stored
Can I use static variables in my web application ? what are the alternatives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.