Click to See Complete Forum and Search --> : Passing Array into Constructor


tryston02
August 3rd, 2005, 09:29 AM
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

boudino
August 3rd, 2005, 09:56 AM
I would use a specialized class like:

pulic class FileTypes
{
private string _msWord;
private string _msExcel;
private string _textFile;
private string _csvFile;

// Imagine apropriate properties here, but I am lazy to write them.
}

...
private FileTypes fiileTypes;

public UploadFile_strict(FileTypes fileType)
{
this.fileTypes = fileTypes;
}

In fact, I would do it more complex, but I hope this is enoght as an example.

tryston02
August 3rd, 2005, 10:57 AM
Thanks for the reply! But how would I allow them to pass in multiple allowed filetypes as an array?

Thanks again!

Chris

MadHatter
August 3rd, 2005, 11:18 AM
foreach(string s in strFileType) {
if(s.EndsWith(".doc")) _wordFile = s;
if(s.EndsWith(".xsl")) _excelFile = s;
if(s.EndsWith(".whatever")) _whateverFile = s;
}