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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.