Zaki
Zaki

Reputation: 5600

static variable value

I have a static variable declared in my class as :

private static DirectoryInfo _extractionFolder;

in some function i change this to :

_extractionFolder = new DirectoryInfo(@"C:\TEST");

then on same function on few lines down can I change this variable again?

I am trying it and it doesn't seems to change.or is it me having a rough day.

Upvotes: 0

Views: 1184

Answers (4)

Philipp Schmid
Philipp Schmid

Reputation: 5828

Did you mean

_extractionFolder = new DirectoryInfo(@"c:\TEST");

Then you can access its properties and later after doing another 'new' you can get information for a different directory. Is that what you are attempting to do?

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503100

That code wouldn't even compile - you're trying to assign a string value to a DirectoryInfo variable.

I suspect what's happened is that you've actually declared a local variable which is hiding the string variable, like this:

private static DirectoryInfo _extractionFolder;

public void Foo()
{
    string _extractionFolder;
    ...
    _extractionFolder = @"C:\TEST"; // Modifies local variable, not static one
}

Mutable static variables are rarely a good idea though, and access to them needs to be handled carefully due to threading concerns.

Upvotes: 2

Shiny Chocobo
Shiny Chocobo

Reputation: 43

As far as static variables go, you should be able to change it. There must be something wrong with what you're setting it to or how you're using the variable once you've set it.

Upvotes: 1

Nick Butler
Nick Butler

Reputation: 24433

> or is it me having a rough day?

Rough day I think :) You should be able to set its value like any other variable.

Upvotes: 1

Related Questions