C
C#12mo ago
SCOLANATOR

Parsing Error

Trying to take a string then parse it to the correct format for decimal and dateonly for a CSV file yet the first record works fine but the subsequent records are showing an error? I'm very new to C# so having a bit of trouble figuring this one out CS1503 Argument 1: cannot convert from 'string' to 'System.IFormatProvider?'
No description
34 Replies
ZacharyPatten
ZacharyPatten12mo ago
you seem seem a bit confused why would you think you need to convert the value to a string and then parse it right back into a non-string data type? at this point in the code are you trying to read in the file or export to the file?
SCOLANATOR
SCOLANATOROP12mo ago
I was not originally doing that but without ToString it was causing other errors. This is for the export part of the file after having made the changes.
Angius
Angius12mo ago
You're trying to set record.Amount to record.Amount But with extra steps Same goes for other properties You're setting them to... themselves
SCOLANATOR
SCOLANATOROP12mo ago
So record amount is a string with say '100' and I want it to be a decimal with value '100.00' for example
Angius
Angius12mo ago
Is the type of record.Amount a string or a decimal?
SCOLANATOR
SCOLANATOROP12mo ago
And similar situation for date, taking '5/11/23' to '05/11/23' Its a string
Angius
Angius12mo ago
Then it is a string Period
SCOLANATOR
SCOLANATOROP12mo ago
and I want it to be a decimal with two decimal places
Angius
Angius12mo ago
You cannot assign a double or a boolean or a Person instance to a property of type string You can only ever assign another string
SCOLANATOR
SCOLANATOROP12mo ago
OK How would i take values that could be 100 or 87.12 and always make sure they come out with two decimal places, I don't mind if its a string, it just needs to have two decimal places Same for the date, it needs to be zero padded
Angius
Angius12mo ago
Store the values as their actual types Integers stored as int, dates stored as DateOnly, and so on The, when displaying them, decide how should they be displayed And only then
SCOLANATOR
SCOLANATOROP12mo ago
But they are being brought in as strings from an existing CSV file that does not contain wholly dates or integers
Angius
Angius12mo ago
A CSV file can be parsed into whatever you need it to be parsed
SCOLANATOR
SCOLANATOROP12mo ago
For example Amount comes in as " 100 | 98.16 | 55.1 " for example and I am trying to seperate it out into multiple records
Angius
Angius12mo ago
Regardless, store in appropriate types
SCOLANATOR
SCOLANATOROP12mo ago
I dont think I can parse it as an integer if the cell contains a pipe and double quotations
Angius
Angius12mo ago
Only .ToString() it with the desired format on display
SCOLANATOR
SCOLANATOROP12mo ago
*sorry store not parse as an integer
Angius
Angius12mo ago
Well, no, the format you have is garbage
ZacharyPatten
ZacharyPatten12mo ago
Is this a school assignment, work assignment, personal project? Can you share the source code?
Angius
Angius12mo ago
But you can handle it manually
SCOLANATOR
SCOLANATOROP12mo ago
It's a personal project that will probably evolve into a work project. I'm practicing my C# on some simple work problems. I can share my source code.
Angius
Angius12mo ago
I'd start by having non-garbage data to work with
ZacharyPatten
ZacharyPatten12mo ago
that would help us help you if you do share it (preferably via github repo link)
Angius
Angius12mo ago
If at all possible
SCOLANATOR
SCOLANATOROP12mo ago
OK, I can't change the data coming in, it's from an external database The whole point of the program is to make the data usable
Angius
Angius12mo ago
You'll have to parse it manually. And if you have, as you say, 69 | 4.20 | 111.222 store that data in, say, double[] type property
SCOLANATOR
SCOLANATOROP12mo ago
I'll upload the CSV too so you can see what I mean Appreciate the help, I spend most of my time with Powershell and JS so still learning the C# ropes
Angius
Angius12mo ago
Lesson 1: C# has types, use them
Angius
Angius12mo ago
404 The repo is private, probably
SCOLANATOR
SCOLANATOROP12mo ago
Whoops, now its public
Angius
Angius12mo ago
Well, if you know which columns can store arrays... you can just parse them to arrays
SCOLANATOR
SCOLANATOROP12mo ago
I guess that makes sense Just a bit frustrated with this project to be honest, I had it working great then trying to convert a single line item into multiple threw up all sorts of problems
Want results from more Discord servers?
Add your server