Passing array into Constructor


Hey guys,
I'm using C# 2.0 and thought I would take advantage of the new params keyword (I might not even need it if someone can show me another way).
I'm making a custom class that handles files that our customers upload via the upload control. I want to only allow certain types of files to be uploaded, and was going to leave this up to the consumer of this object to specify in the contructor what file types they would like to allow from a predifined list.
i.e. UploadFile_strict(msWord, msExcel) or just UploadFile_strict(msWord)
I want to allow:
msWord doc
msExcel doc
text file
csv file
I've made them private variables like so:
private string _msWord;
private string _msExcel;
private string _textFile;
private string _csvFile;
I thought about creating a constuctor like so:
public UploadFile_strict(params string[] strFileType)
{
//how would a for-each work here? How would it assign these values to the private variables?
}
Should I get rid of the private variables and make it an array? How would you guys acheive this?
Thanks for any help,
Chris
[1181 byte] By [Tryston02] at [2007-12-16]
# 1
Hi,

In this situation I would use the Hashtable. Makes it much easier to check if a filetype is allowed.

First import the System.Collections namespace with the using clause.

Then, in the calling code, populate the Hashtable object and call the method:


Hashtable htAllowedFileTypes = new Hashtable();

htFileTypes.Add("msWord doc", "");

htFileTypes.Add("msExcel doc", "");

htFileTypes.Add("text file", "");

htFileTypes.Add("csv file", "");

UploadFile_strict(htAllowedFileTypes);




The method itself would look like this:


public void UploadFile_strict(Hashtable htAllowedFileTypes)

{

htFileTypes = htAllowedFileTypes;

if (htFileTypes.ContainsKey("csv file"))

{

//Upload of doc allowed

}

}




Notice that using the contains key, you can easily find out what filetypes are allowed.

Regards,
Vikram

Vikram at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Instead of Hashtable, I would use Dictionary<string, string> for type-safety. Just my 2 cents.
DanielRieck at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Hashtable works, but you could also use an enum. Then you wouldn't have to do any check, the compiler will do it for you. If you want to make the list configurable (i.e. store the list in a file that's read at runtime) the Hashtable (or Dictionary) is the better way to go. If not, the enum list is probably the better choice since it catches invalid choices at compile time.



public enum AllowedFileTypes
{
MS_WORD, MS_EXCEL, TEXT, CSV
}

public UploadFile_strict(params AllowedFileTypes [] fileTypes)
{
}

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
Thanks guys for all your posts. I'm still deciding on which method to use.
dkocur2 in the example you gave, could you show me how I would access those values and check to see it's value?
Thanks again guys!

Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5



public class UploadFile_strict
{
public enum AllowedFileTypes
{
MS_WORD, MS_EXCEL, TEXT, CSV
}

public UploadFile_strict(params AllowedFileTypes [] fileTypes)
{
allowedFileTypes.AddRange( fileTypes);
}

public void UploadFile( string file, AllowedFileTypes type)
{
if( !allowedFileTypes.Contains( type))
throw new Exception( "That file type is not allowed.");

switch( fileType)
{
case AllowedFileTypes.MS_WORD:
...
break;
case AllowedFileTypes.MS_EXCEL:
...
break;
case AllowedFileTypes.TEXT:
...
break;
case AllowedFileTypes.CSV:
...
break;
}
}

private List<AllowedFileTypes> allowedTypes = new List<AllowedFileTypes);
}



dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
Thanks man. A few questions though:
1) What exactly does AddRange do?
2) I don't see this being used anywhere:
private List<AllowedFileTypes> allowedTypes = new List<AllowedFileTypes);

Thanks again for your help man. :)

Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 7
1. AddRange adds each item in the Array to the List<>.

2. Hmm, what can I say? I do this all the time. Although, it should be...

private List<AllowedFileTypes> allowedTypes = new List<AllowedFileTypes>();

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
Is it being used somewhere?
Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 9
Ok, I understand now. It works perfect, however I was speaking with my super and he wants the user of the object to be able to specify what file extension they want (be it whatever, instead of hardcoding a set range of allowed types like in the enum) in the constructor and then allow only the ones they specify in the constructor.
Does this make sense. I really appreciate all the help you are giving me.
Thanks,
Chris
Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 10
I assume your referring to the member variable declaration / initialization?

Yes it's being used. Take the following class that has three constructors. You could write it like this...



public class A
{
private int foo;

public A() { foo = 10; ... }
public A( int i) { foo = 10; ... }
public A( double d) { foo = 10; ... }
}

or, you could write it like this...


public class A
{
private int foo = 10;

public A() {...}
public A( int i) {...}
public A( double d) {...}

Note: The elipses (...) are not literal, they represent whatever code would go in that space.

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 11
Yes, it makes sense. I kinda suspected that was the case, but you did state "from a predefined list", so that's the way I wrote it. Sounds like the Dictionary is the way to go, although I do wish Microsoft would create a Set class like the one in Java. This is one of those instances where a Set class would make more sense.
dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 12
No I mean being able to pass any extension in the contructor without knowing ahead of time what it might be. So in this case overloading the constructor won't work. I think your previous code would work for this (because I need an array of possible values passed in) but I need to remove the enum since I won't know what extensions they may want to use in the constructor.
UploadFile_strict up = new UploadFile_strict(".doc", ".xls", ".ini", ".asp",) etc... as the maker of the class I won't know how many or what extensions they might want to pass in, but to only allow those extensions that are passed in to be submitted.
Any ideas?
Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 13


public class UploadFile_strict
{
private Dictionary<string,string> allowedTypes = new Dictionary<string,string>();

public UploadFile_strict(params AllowedFileTypes [] fileTypes)
{
foreach( string fileType in fileTypes)
allowedTypes.Add( fileType, null);
}

public void UploadFile( string file)
{
if( !allowedFileTypes.ContainsKey( GetFileExtension(file)))
throw new Exception( "That file type is not allowed.");

...
}

}



dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 14
Ok, I feel we are almost there. I've added the changes you suggested, however there is one part the compiler doesn't like:
public UploadFile_strict(params AllowedFileTypes [] fileTypes)
It doesn't like the 'AllowedFileTypes'.
That was the name of the enum in your previous code listing, however since we are not using it anymore what should go there instead.
Thanks again for your help. Hopefully the next post will nail it.

Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...