0

I have one class where in I am defining 2 variables.

public class attachment_type
{
   string filename;
   int cnt;
}

In the 2nd class, I want to assign string value to filename. In already existing code they have made array of class type.

public class mainApp
{
   attachment_type[] at = new attachment_type[dt.rows.count];
   at[0].filename = "test File" 
}

I am not able to do the above. Error comes at line at[0].filename = "test File";

Object reference not set to an instance of object.

croxy
  • 4,082
  • 9
  • 28
  • 46
Coder157
  • 73
  • 3
  • 15

1 Answers1

3

You have to assign a new instance of your class for every entry in the array:

public class mainApp
{
    attachment_type[] at = new attachment_type[dt.rows.count];
    at[0] = new attachment_type();
    at[0].filename = "test File" 
}

With attachment_type[] at = new attachment_type[dt.rows.count]; you only allocate a new array of a given size, however the array does not have any content so far. You only say that you need some memory, but not for what.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111