Help with array

  • Thread starter Thread starter vbMark
  • Start date Start date
V

vbMark

I'm tyring to extract the file name from a path entered in a text box by
doing this:

string sFile = File1.Value;
string[] sArray = sFile.Split(new char[] {'/'});
sFile = sArray.GetValue(sArray.GetUpperBound(0));

However, I get the following error:
Compiler Error Message: CS0029: Cannot implicitly convert type 'object' to
'string'

I can't figure out how to correct this. Can someone help me here?

Thanks!
 
sFile = (string)sArray.GetValue(sArray.GetUpperBound(0));
GetValue returns an object. Alternative you should use the indexer [] to
access the elements of the array in a type-safe manner.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
vbMark said:
I'm tyring to extract the file name from a path entered in a text box by
doing this:

string sFile = File1.Value;
string[] sArray = sFile.Split(new char[] {'/'});
sFile = sArray.GetValue(sArray.GetUpperBound(0));

However, I get the following error:
Compiler Error Message: CS0029: Cannot implicitly convert type 'object' to
'string'

I can't figure out how to correct this. Can someone help me here?

Thanks!
 
vbMark said:
I'm tyring to extract the file name from a path entered in a text box by
doing this:

string sFile = File1.Value;
string[] sArray = sFile.Split(new char[] {'/'});
sFile = sArray.GetValue(sArray.GetUpperBound(0));

However, I get the following error:
Compiler Error Message: CS0029: Cannot implicitly convert type 'object' to
'string'

I can't figure out how to correct this. Can someone help me here?

The easiest thing to do is avoid using the Array methods, and just use
the "normal" way of accessing arrays:

sFile = sArray[sArray.Length-1];

To correct your existing code, you could just add a cast to string
though.
 
Hi,

Are you on a web app ? cause otherwise you have the incorrect / , it should
be \ .

and if all you need is get the name of the file you can do this ( if a win
app )
string file = System.IO.Path.GetFileName ( sFile );

Don't know if there is a "web" version though.

Cheers,
 
John Wood said:
sFile = (string)sArray.GetValue(sArray.GetUpperBound(0));
GetValue returns an object. Alternative you should use the indexer [] to
access the elements of the array in a type-safe manner.

Adding (string) did the trick. You lost me on the indexer thing.
 
vbMark said:
I'm tyring to extract the file name from a path entered in a text box
by doing this:

string sFile = File1.Value;
string[] sArray = sFile.Split(new char[] {'/'});
sFile = sArray.GetValue(sArray.GetUpperBound(0));

However, I get the following error:
Compiler Error Message: CS0029: Cannot implicitly convert type
'object' to 'string'

I can't figure out how to correct this. Can someone help me here?

The easiest thing to do is avoid using the Array methods, and just use
the "normal" way of accessing arrays:

sFile = sArray[sArray.Length-1];

To correct your existing code, you could just add a cast to string
though.

Nice. Thanks.
 
Hi,

Are you on a web app ? cause otherwise you have the incorrect / , it
should
be \ .

and if all you need is get the name of the file you can do this ( if
a win
app )
string file = System.IO.Path.GetFileName ( sFile );

Don't know if there is a "web" version though.

Cheers,

You're right, the / was incorrect.

I am doing this on an ASP page and GetFileName worked great.

Thanks!
 
Back
Top