CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    37

    Exclamation 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

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Passing Array into Constructor

    I would use a specialized class like:

    Code:
    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jul 2005
    Posts
    37

    Re: Passing Array into Constructor

    Thanks for the reply! But how would I allow them to pass in multiple allowed filetypes as an array?

    Thanks again!

    Chris

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Passing Array into Constructor

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured