2

If I create a new Class, and in this class I put a property like so:

public class CurrentDirectory
{
    private string cd;

    public string CurrentDirectory
    {
        get
        {
            return cd;
        }
        set
        {
            cd = value;
        }
    }

I then, at one point in my program, create a new instance of this class like so:

CurrentDirectory myCurrentDirectory = New CurrentDirectory();

I then set a value to CurrentDirectory like so:

myCurrentDirectory.CurrentDirectory = @"C:\MyFiles\Here";

Then, at another point in my program I create another instance of CurrentDirectory and 'get' the value of CurrentDirectory like so:

CurrentDirectory myCurrentDirectory1 = new CurrentDirectory();
string putFilesHere = myCurrentDirectory1.CurrentDirectory;

Will this return the value I set earlier or do I need to 'get' and 'set' my value within the same instance?

Thanks

Yahia
  • 69,653
  • 9
  • 115
  • 144
JMK
  • 27,273
  • 52
  • 163
  • 280

6 Answers6

5

No, this does not hold values in this way, no more than setting one user's name to "Heisenburg" affects my name of "Anthony Pegram". Each instance of the class is a different object, and instance properties and members of one instance do not carry over to other instances.

User user = new User(); // this is my object!
user.Name = "Anthony Pegram"; // this is my name! 

User otherUser = new User(); // this is your object!
otherUser.Name = "Heisenburg"; // this is your name! 

// my object is not your object

If you need to share properties to where some other place sees the same value you set elsewhere, you need to either share the instance, or make the data itself shared via the static keyword on the property.

class Foo
{
    public static string Bar { get; set; }
}

If using statics, the state becomes global, and is not tied to a particular instance. It, in fact, does not need an instance. You would simply access it via the class name directly, not via an object of that class.

Foo.Bar = "Blah"; // no instance necessary
string data = Foo.Bar;
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • Thanks very much for this, all of the other answers were really helpful also =) – JMK Dec 05 '11 at 18:21
4

You need to get and set within the same variable instance.
If you need a global class, you could create a singleton class (see this example)

Community
  • 1
  • 1
Marco
  • 56,740
  • 14
  • 129
  • 152
3

No that won't return the value you set earlier since that property is instance-specific. IF you want a property being shared by all instances then you need to make it static.

Yahia
  • 69,653
  • 9
  • 115
  • 144
3

Two different instances are will have two different property values. When you create a new instance, you're creating a whole new object with it's own CurrentDirectory property.

If you're trying to create a copy of the object you already have for some reason, look into Cloning.

AllenG
  • 8,112
  • 29
  • 40
2

No.

This is a new object and would have the default values for the new object. In this case it would be nothing.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
1

Your new instance, is, as the word indicates, a new instance therefore, the value you set earlier will hold for the other instance (myCurrentDirectory), myCurrentDirectory1 is an object of the same type with its own values. You need to set and get the values on the same instance on this case.

If you set myCurrentDirectory1=myCurrentDirectory; then both will hold the same values since they are really pointers to the same place in memory.

Icarus
  • 63,293
  • 14
  • 100
  • 115