0

I have attempted to create a Computer object to hold data that is stored in a list and when I attempt to view any of the data it comes back with no output data...

The Object I have created is like so:

class Computer
{
    string companyName;
    string locationName;
    string serverName;
    string deviceName;
    string lastOnline;

    public Computer(string companyName, string locationName, string serverName, 
        string deviceName, string lastOnline)
    {
        this.companyName = companyName;
        this.locationName = locationName;
        this.serverName = serverName;
        this.deviceName = deviceName;
        this.lastOnline = lastOnline;
    }

    public string CompanyName { get; set; }    
    public string LocationName { get; set; }    
    public string ServerName { get; set; }    
    public string DeviceName { get; set; }
    public string LastOnline { get; set; }
}

I initialize the list as so:

List<Computer> computerList = new List<Computer>();

For a test I have added five items to the list:

computerList.Add(new Computer("Hello", "World", "Test", "Machine", "2-12-13"));
computerList.Add(new Computer("Hello", "World", "Test", "Machine", "2-12-13"));
computerList.Add(new Computer("Hello", "World", "Test", "Machine", "2-12-13"));
computerList.Add(new Computer("Hello", "World", "Test", "Machine", "2-12-13"));
computerList.Add(new Computer("Hello", "World", "Test", "Machine", "2-12-13"));

and to output my items I use:

foreach (var author in computerList)
{
    Console.WriteLine("Test : {0}", author.LastOnline);
}

Can anybody see why I am getting no output other than:

Test :
Test :
Test :
Test :
Test :

Rufus L
  • 32,853
  • 5
  • 25
  • 38
Ollie Beumkes
  • 304
  • 1
  • 16
  • `Computer.lastOnline` and `Computer.LastOnline` have no relationship with each other. You probably want to delete the former entirely. – Jeroen Mostert Mar 26 '18 at 16:35
  • 1
    You need to edit the property getters to return the private instance fields. Right now the compiler is generating new backing fields for the properties – St. Pat Mar 26 '18 at 16:36
  • 2
    Above comments are spot on. Read here (https://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose#6001941) for more information about what happened. – Gus Mar 26 '18 at 16:39
  • @St.Pat I have tried get { return lastOnline; } set { lastOnline= value; } if this is what you mean? as for Jeroen which lastOnline are you referring too – Ollie Beumkes Mar 26 '18 at 16:40
  • @Ollie yes that is what I was referring to. That will make the property return the private field that was given a value in the constructor – St. Pat Mar 26 '18 at 16:44
  • `author.LastOnline` is refering to a method that does not really do anything. the way it is writte. your `string lastOnline` is not referenced by the `author.LastOnline` you already have you short hand properties written below. you could change your constructor from `this.lastOnline = lastOnline;` to `this.LastOnline = lastOnline;` and that should work also but then your top list of strings are irrelevant. – thanatorr Mar 26 '18 at 16:45
  • You don't need backing fields for [Auto-Implemented Properties](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties) – Rufus L Mar 26 '18 at 16:48

2 Answers2

2

You are setting private variables so public properties still have default value.

Just modify your constructor to set the public properties:

public Computer(string companyName, string locationName, string serverName, string deviceName, string lastOnline)
        {
            this.CompanyName = companyName;
            this.LocationName = locationName;
            this.ServerName = serverName;
            this.DeviceName = deviceName;
            this.LastOnline = lastOnline;
        }
Sahil Sharma
  • 3,027
  • 3
  • 29
  • 71
1

Your properties are not quite set correctly as mentioned in the comments. try one of these two and delete the properties and access methods you have.

1:

public string CompanyName { get; set; }

OR

2:

private string _companyName;

public string CompanyName
{
    get { return _companyName; }
    set { _companyName = value; }
}

EDIT

Or as mentioned in the other answer, fix your constructor to point to the properties and not the local variables. You should the DELETE your local variables so as not to get confused.

thanatorr
  • 105
  • 1
  • 9