Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've written a program that seems to be growing in use, but currently the way it's setup I have to create a new sub routine for each different type of CSV file I may read.

Our template CSV will never change.

What I'm trying to find out is can you have a text file that defines column information, rather than having to create a new sub routine with the different format.

Basically it's a CSV reader, that reads through a file and places the columns of information into a format we require. Here is a test sample

VB
currentrow = csvreader.ReadFields()
                        account(0) = (currentrow(0))   'Account number
                        account(1) = (currentrow(29))  'Card Number
                        account(2) = (currentrow(4))   'Firstname
                        account(3) = (currentrow(6))   'Last Name
                        account(4) = (currentrow(2))   'Title
                        account(6) = (currentrow(15))   'Gender
                        account(7) = (currentrow(22))  'Expirydate
                        account(9) = (currentrow(14))  'Birthdate
                        account(12) = (currentrow(11)) 'Postcode
                        account(15) = (currentrow(16)) 'phone number
                        account(20) = (currentrow(20)) 'Email
                        account(26) = (currentrow(7))  'street
                        account(27) = (currentrow(9))  'suburb
                        account(29) = (currentrow(10))  'state
                        account(40) = (currentrow(25))  'accountgroup


the currentrow is what's being read. Can the row numbers be replaced with a variable which is read from a text file, and could someone point me in the right direction on how to do this :)

Iain Stewart
Posted
Comments
Ralf Meier 18-Aug-15 0:16am    
I'm not sure if I understood right what you meant. Perhaps you try once more to explain what you are trying to achieve.
Do you want to store/get the Information too in the file to where the readed entries should assigned ?

1 solution

If I understand the question correctly, why not store the mapping information in a configuration file and read it from there.

Basically you would have two pieces of information for each element:
- target column number for an element
- source column number for an element

For example first name would have 2 as target and 4 as source

To use a configuration file you can use ConfigurationManager.AppSettings [^].

The example line could look something like
VB
account(CInt(appSettings("FirstName_Target"))) = (currentrow(CInt(appSettings("FirstName_Source")))) 

taken that the appSetting is defined properly as in the example in MSDN.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900