Exists. Confused about this ...

S

shapper

Hello,

I have a filename and I need to check if it is of a specific type.

I have:

public static Boolean FileType(String filename, String[] types) {
return types.Length == 0 ? false : Array.Exists(types, s =>
String.Compare(s, Path.GetExtension(filename).TrimStart('.'), true) ==
0);
} // FileType

So if filename="document.pdf" and types = new string[] { "gif, pdf,
png, swf" } then it should return true because the document is a pdf
and pdf exists in types.

However, this is returning false.

I am a little bit confused on this. Should I add != 0 ... or maybe do
this in other way?

Thanks,
Miguel
 
M

Matt

Hello,

I have a filename and I need to check if it is of a specific type.

I have:

    public static Boolean FileType(String filename, String[] types) {
      return types.Length == 0 ? false : Array.Exists(types, s =>
String.Compare(s, Path.GetExtension(filename).TrimStart('.'), true) ==
0);
    } // FileType

So if filename="document.pdf" and types = new string[] { "gif, pdf,
png, swf" } then it should return true because the document is a pdf
and pdf exists in types.

However, this is returning false.

As it should. Your 'array' being passed in is a single string of gif,
pdf, png, swf.

What you wanted, I think, was this:

string[] types = new string[] { "gif", "pdf", "png",
"swf" };

which is an array of four different strings.

matt
 
S

shapper

I have a filename and I need to check if it is of a specific type.
    public static Boolean FileType(String filename, String[] types){
      return types.Length == 0 ? false : Array.Exists(types, s =>
String.Compare(s, Path.GetExtension(filename).TrimStart('.'), true) ==
0);
    } // FileType
So if filename="document.pdf" and types = new string[] { "gif, pdf,
png, swf" } then it should return true because the document is a pdf
and pdf exists in types.
However, this is returning false.

As it should. Your 'array' being passed in is a single string of gif,
pdf, png, swf.

What you wanted, I think, was this:

            string[] types = new string[] { "gif", "pdf", "png",
"swf" };

which is an array of four different strings.

matt

Of course,

Thanks.
 
B

Ben Voigt [C++ MVP]

Matt said:
Hello,

I have a filename and I need to check if it is of a specific type.

I have:

public static Boolean FileType(String filename, String[] types) {
return types.Length == 0 ? false : Array.Exists(types, s =>
String.Compare(s, Path.GetExtension(filename).TrimStart('.'), true) ==
0);
} // FileType

So if filename="document.pdf" and types = new string[] { "gif, pdf,
png, swf" } then it should return true because the document is a pdf
and pdf exists in types.

However, this is returning false.

As it should. Your 'array' being passed in is a single string of gif,
pdf, png, swf.

What you wanted, I think, was this:

string[] types = new string[] { "gif", "pdf", "png",
"swf" };

which is an array of four different strings.

<sarcasm>
_Everyone_ knows that commas go inside quotes.
http://www.flounder.com/bricks.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top