简体   繁体   中英

Can't access property inside object initializer

Consider the following code.

List<MyFiles> files = new List<MyFiles>();
....
while (reader.Read())
{
    var a = new MyFiles
    {
       FileName = reader["Filename"].ToString(),
       FileLocation = reader["Filelocation"].ToString(),
       FullPath = FileLocation + @"\" + FileName // Error at this line
    };
   files.Add(a);
}

The above is a snippet from my method which uses SqlDataReader to fetch data from the database. Everything works fine expect when I try to assign value to FullPath property I get the following error message:

An object reference is required for the non-static field, method, or property 'MyFiles.FileLocation' An object reference is required for the non-static field, method, or property 'MyFiles.FileName'

I know I can make those properties static or create an instance and access them via eg var b = new MyFiles(); b.FileLocation .. var b = new MyFiles(); b.FileLocation ..

But my question is why can't I use FileLocation and FileName as I am in the above code snippet. Is there something I'm missing?

You cannot access the property of an instance which you are just initializing.

You can use local variables:

int fileNameColumnOrdinal = reader.GetOrdinal("Filename");
int fileLocationColumnOrdinal = reader.GetOrdinal("Filelocation");
while (reader.Read())
{
    string fileName = reader.GetString(fileNameColumnOrdinal);
    string fileLocation = reader.GetString(fileLocationColumnOrdinal);
    var a = new MyFiles
    {
       FileName = fileName,
       FileLocation = fileLocation,
       FullPath = System.IO.Path.Combine(fileLocation, fileName)
    };
   files.Add(a);
}

I also recommend to use System.IO.Path.Combine .

When creating a new object and set some properties using this syntax: var foo = new bar { a = 123 }; You can only set properties, not using them. You have to use some helper variables or setting the properties via: foo.a = 456; .

Solution 1

List<MyFiles> files = new List<MyFiles>();
....
while (reader.Read())
{
    string fileName = reader["Filename"].ToString();
    string fileLocation = reader["Filelocation"].ToString();

    var a = new MyFiles
    {
       FileName = fileName,
       FileLocation = fileLocation,
       FullPath = fileLocation + @"\" + fileName // Error at this line
    };
   files.Add(a);
}

Solution 2

List<MyFiles> files = new List<MyFiles>();
....
while (reader.Read())
{
    var a = new MyFiles()

    a.FileName = reader["Filename"].ToString();
    a.FileLocation = reader["Filelocation"].ToString();
    a.FullPath = a.FileName + @"\" + a.FileLocation // Error at this line
   files.Add(a);
}

Solution 3

You can create a custom getter for FullPath :

public string FullPath
{
    get { return FileName + @"\" + FileLocation; }
}

EDIT

I think there is no specific reason for this. It's by design, so some c# developer thought, we don't need this :) I think it would be possible, but just not implemented. It should be possible, because the created IL-Code is the same as in my solution 2 and it is only some syntactical sugar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM