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

Passing array into Constructor
Stevejh
2. Hmm, what can I say I do this all the time. Although, it should be...
private List<AllowedFileTypes> allowedTypes = new List<AllowedFileTypes>();
pcoelho
chillah
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.
MichelG
SoccerDude
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!
Klaus Staehle
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.");
...
}
}
Darren Harvey
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);
}
Zycl0ne
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
linuxfreak
public enum AllowedFileTypes
{
MS_WORD, MS_EXCEL, TEXT, CSV
}
public UploadFile_strict(params AllowedFileTypes [] fileTypes)
{
}
sidno
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.
Epilif
nikonek
Does this make sense. I really appreciate all the help you are giving me.
Thanks,
Chris
allen herring
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. :)
Maher K. Al-Jendasi
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