0

excelFilesNames is a array of string and there is more than 1 value in excelFilesNames. While printing oVariableName[2] value it prints "system.string". i want to print all values of excelFilesNames by assigning it to oVariableName[2].

the code is below

Object[] oVariableName = new object[3]; 
oVariableName[2] = excelFilesNames;     
MessageBox.Show(oVariableName[2].ToString());
1
  • Why not just MessageBox.Show(excelFilesNames[2]);? Commented May 4, 2015 at 12:20

4 Answers 4

1
Object[] oVariableName = new object[3]; 
oVariableName[2] = string.Join(",", excelFilesNames);     
MessageBox.Show(oVariableName[2]);

You don't need to use a Object[] though:

var fileNames = string.Join(",", excelFilesNames);
MessageBox.Show(fileNames);
Sign up to request clarification or add additional context in comments.

Comments

1

If excelFilesNames is an array of strings, iterate it with a foreach:

Object[] oVariableName = new object[3]; 
oVariableName[2] = excelFilesNames;  

foreach (string s in oVariableName[2])
{
   MessageBox.Show(s);
}

Note I'm not sure why you assign the string[] to an object field inside an object[], but I'll assume for your question that it's whats needed to be done.

Comments

0

Since the contents of oVariableName[2] is known to be a string[], you can cast it to string[] and make use of string.Join() to create a multiline string with one line per filename, like so:

MessageBox.Show(string.Join("\n", (string[])oVariableName[2]));

However this will explode at runtime if oVariableName[2] is not an array of strings. You could protect against like that:

var asStringArray = oVariableName[2] as string[];

if (asStringArray != null)
    MessageBox.Show(string.Join("\n", asStringArray));

I don't really understand why you're using an array of objects in this way, but I guess there's some background that you haven't told us about.

Comments

0

excelfileNames is array of string, so when your assigning to the oVariableName[2] you should assign a specific value of it, rather assigning the whole object.

something like below code.

    string [] excelFilesNames = new string[]{ "One","Two"};
    Object[] oVariableName = new object[3]; 
    oVariableName[2] = excelFilesNames[1];     
    MessageBox.Show(oVariableName[2].ToString());

1 Comment

but my need is to assign whole object value and not a specific value. well i got my answer, thank you for your precious time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.