C # - line in line?

I'm not sure what the problem is. I am working with 2 lines and I keep getting the error "Field initializer cannot refer to non-static field, method or property" Captcha.Capture.CaptureTime "".

Here's a snippet of code:

string CaptureTime = DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();


string SaveFormat = Properties.Settings.Default.SaveFolder + "Screenshot (" + CaptureTime + ")." + Properties.Settings.Default.ImageFormat;

      

I won't go into detail on why I use strings in this particular vein. Everything works fine. I'm guessing it has something to do with the line being inside another line? It may be quite obvious, but I really have no idea. Any ideas?

+2


source to share


4 answers


No, it has nothing to do with it - it's just that instance field initializers cannot use other instance fields or instance methods. Here's a simple example:

class Test 
{
    int x = 0;
    int y = x + 1;
}

      

It is probably easiest to move the initialization to the body of the constructor. By the way, you shouldn't call DateTime.Now

multiple times like in the same initializer - it can change between calls, which leads to terrible results. Use a local variable instead - which is once again easier to do from the constructor:



string CaptureTime;
string SaveFormat;

public YourType()
{
    DateTime now = DateTime.Now;

    CaptureTime = now.Month + "-" + now.Day + "-" + now.Year + "-" + 
        now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
    SaveFormat = Properties.Settings.Default.SaveFolder + 
        "Screenshot (" + CaptureTime + ")." + 
        Properties.Settings.Default.ImageFormat;
}

      

By the way, an easier way to create CaptureTime would be:

CaptureTime = now.ToString("MM-dd-yyyy-HHmmss");

      

+7


source


The error tells you exactly what is going on. You are trying to initialize an instance field SaveFormat

with a value that depends on the instance field CaptureTime

using field initializer syntax. Move the initialization of these values ​​to an instance-level constructor and you should be fine.



By the way, store DateTime.Now

in a temporary value instead of calling it again like you are now. What if you are looking at the date boundary while your code is running? Ugh.

+3


source


In C # it is forbidden to write non-static field initializers that refer to each other, since the initialization order is usually undefined.

Move initialization to your class constructor

And you are really better off using String.Format or DateTime.Format (string) for date formatting.

+1


source


You need to set the member in the constructor or assign it via a property.

Btw, you can clean up this DateTime code using the ToString overload.

string CaptureTime = DateTime.Now.ToString("your format");

      

0


source







All Articles