Trying to use a variable for the name of a file

I have 30 files that I have to perform the same operation on them. Instead of writing every one of them out I want to be able to substitute a variable that contains the name of the file that I want to look at instead of using the exact file name.

Example:

Dim DimGoalMethod As String

DimGoalMethod = CCADPDataSet._Dimension1_Goal1.Rows(RowX).Item(ItemX).ToString()

Want to do something like this instead:

Public FileX As String

FileX = "_Dimension1_Goal1"

Dim DimGoalMethod As String

DimGoalMethod = CCADPDataSet.FileX.Rows(RowX).Item(ItemX).ToString()

Does anyone know if this is possible and if so HOW

Thanks




Answer this question

Trying to use a variable for the name of a file

  • sgmuser

    mmmm....it should be DataSet.TableName.Rows(x).Item(x)

    So you Could do Dim TableName as String = "_Dimension1_Goal1"

    CCADPDataSet.Tables(TableName)...

    So if yiou are iterating through a collection of files...

    Dim di As New DirectoryInfo("TheDirectoryPath")

    Dim TheFiles() As FileInfo = di.GetFiles

    For Each fi As FileInfo In TheFiles

    'trim extension from name---MyFileName.txt becomes MyFileName

    Dim TableName As String = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length - 1)

    CCADPDataSet.Tables(TableName)...

    Next



  • deen

    pretty easy.... I noticed my code from last night helped to, right on. Anyways use a for next loop and put all your file names in an array like so.

    Dim FileList() as string = {"FileName1", "FileName2", "Filename3", "etc"}

    dim count as integer = 0

    For count = 0 to 29 '1 less the total number of array entries because the array starts at 0
    Dim DimGoalMethod As String
    DimGoalMethod = CCADPDataSet.FileList(count).Rows(RowX).Item(ItemX),.ToString()
    ' Now do all the code you need to to the DimGoalMethod variable because that variable will change everytime the loop executes
    Next



    Hope that should help, there really isn't a need for a "FileX" variable just reference directly to the array.


  • Gagandeep Singh

    That would be correct....the member after the dataset name should be the name of the table in the dataset...so for each file name you have you should also have a table named the same

    DataSet.TableName...or DataSet.Tables(TableNameVariable)...

    It is also strange that you would have a table name for every file in a directory...although not impossible



  • grnr_r

    It keeps coming up with 'FileList' is not a member of 'CCADP.CCADPDataSet'. I am not sure how to get it to recognize that the data stored in the variable is the name of the file.

  • xr280xr

    I got it to work.

    Public TableName As String

    TableName = "Dimension1.Goal1"

    DimGoalMethod = CCADPDataSet.Tables(TableName).Rows(RowX).Item(ItemX).ToString()

    This worked perfect. First of all I was trying to use the name that was coming up when as I was typing (_Dimension1_Goal1) and this is not the true name of the table(file). The true name is Dimension1.Goal1. Also, I want to clarify what I have going on. I have a SQL Database (dataset) that contains these 30 Tables (files) and depending on which button the user clicks depends on which table I need to show information from "and" which specific column. With this scenario I could have ended up creating 300 forms to display the information on but because of your guy's answers today I will be able to reduce that down to 1 form. YIPPEE!! I am going to back track in my project and change the way I have some other forms running to reduce the size of this monster. I have a lot of learning to do with Visual Basic. I came from Visual Foxpro and the first project I am on in Visual Basic is HUGE!!!

    Thanks for all of your help and we will be talking to you in the future!!



  • billqu

    Dman's solution is much more flexible than mine, concentrate on that more so than what I posted.


  • Trying to use a variable for the name of a file