Dear Friends,
I should First thank you all for taking time reading and kindly answering my question. It was suggested that I should set the Array Size at the time of definition.
I understand it but the fact is, at the time of definition, I am not aware of its probable size needed. As it was pointed I have to define the ‘FileNames’
which is an Array of string outside of button1_Click event (due to some reasons). But within this event the size of it will become clear. So I don’t know it in advance.
In other words, what should I do when I need to define an array with unknown size at the time of definition so that it won’t raise such error?
In bellow my question is repeated for convenience
*************
Hello,
In a program the user may choose some files to have some operation on. In ‘OpenFileDialog’, Full path of files, both path of directory and File names could be returned. What I require is to separate File path from its following File name. To do so, I needed to set value to Array of string one by one. I used the code below but there is a error as follows:
Null Reference Exception was unhandled
Object reference not set to an instance of an object.
Could you please help me?
Thanks a lot
public Form1()
{
InitializeComponent();
}
string[] FilePathNames;
string[] FileNames;
private void button1_Click(object sender, EventArgs e)
{
...
if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
{
FilePathNames = myOpenFileDialog.FileNames;
int counter = 0;
foreach (string fname in FilePathNames)
{
int idx = fname.LastIndexOf('\\');
FileNames[counter] = fname.Substring(idx + 1);
counter += 1;
}
}
}