Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2017, 05:36 PM
Jung Jung is offline
Junior Member
 
Join Date: Oct 2014
Posts: 16
Default Getting the data type of CSV file

Hello,

I am using the JVCsvDataSet component for importing CSV file into a paradox table the is i cannot determinate the data type of each field of the csv file so I use Table.Fields[i].AsVariant for reading data. I don't found an approriate method of JVCsvDataSet for checking the data field or is already exists and don't see it . I made a simple function for checking only boolean data field but not works:
Code:
Function CheckDataType(FldSource,FldDest:TField):Boolean;
var
vl:string;
Bool:Boolean;
Begin
 
If (FldDest.DataType=ftBoolean) Then
Begin
vl:=FldSource.AsVariant;
If (UpperCase(vl)='TRUE') Or (UpperCase(vl)='VRAI') Then  Bool:=True;
If (UpperCase(vl)='FALSE')  OR  (UpperCase(vl)='FAUX') Then Bool:=False;
FldDest.AsBoolean:=Bool;
end;
 
Result:=Bool;
 
end;
The call of the function in a FOR loop:
Code:
TableDest.Edit;
CheckDataType(Source.Fields[IdxSourceField],TableDest.Fields[IdxField]);
TableDest.Post;
Can you help me please ?
Reply With Quote
  #2  
Old 02-27-2017, 09:34 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,180
Default

What does your debugger say?

Does it get into your if-construction (FldDest.DataType=ftBoolean is correct)?

If so, what is the value of vl (and it's a bit strange to put a variant directly into a string, VarToStr is perhaps needed)
Furthermore, in your case if value <> TRUE/VRAI/FALSE/FAUX

And as a sidenote, the UpperCase function is deprecated (use AnsiUpperCase/UTF8UpperCase)
Reply With Quote
  #3  
Old 02-27-2017, 10:22 AM
Jung Jung is offline
Junior Member
 
Join Date: Oct 2014
Posts: 16
Default

Damn ! it works now

I have corrected the code:
Code:
If (FldDest.DataType=ftBoolean) Then
Begin
vl:=FldSource.AsVariant;
vl:=AnsiUpperCase(vl);
If (vl='TRUE') Or (vl='VRAI') Then  Bool:=True;
If (vl='FALSE')  OR  (vl='FAUX') Then Bool:=False;
FldDest.AsBoolean:=Bool;
end;
Yes, before it goes into the if instruction and there was many calls of the UpperCase which I changed with only one call by the AnsiUpperCase function.
Is it comes from the UpperCase function ?

Anyway, thank you Norrit for your help.

BTW : I don't found the SOLVED button

Last edited by Jung; 02-27-2017 at 10:26 AM.
Reply With Quote
  #4  
Old 02-27-2017, 02:07 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 158
Default

Your function is flawed.

A function MUST return a value.

1) if '(FldDest.DataType=ftBoolean)' evaluates FALSE, then your function never sets Bool thus FldDest.AsBoolean:=Bool; is meaningless.
2) if '(FldDest.DataType=ftBoolean)' evaluates TRUE, Bool only gets a value if one of the two if statements evaluates true. If both evaluate FALSE, then Bool doesn't get any value thus FldDest.AsBoolean:=Bool; is meaningless.

A function CANNOT have a path through it that does not return a valid value. Your function obviously has too many paths through it that leaves it returning nothing.

Last edited by rojam; 02-27-2017 at 02:15 PM.
Reply With Quote
  #5  
Old 02-27-2017, 08:57 PM
Jung Jung is offline
Junior Member
 
Join Date: Oct 2014
Posts: 16
Default

Quote:
Originally Posted by rojam
A function MUST return a value.
Yes indeed.
Sorry I not entirely posted the call of the function which was like this:
Code:
If Not CheckDataType(Source.Fields[IdxSourceField],EmployeDest.Fields[IdxField]) Then
Begin
MessageDlg('Data field must be the same',mtError,[mbOk], 0);
 Abort;
end;
Sorry if I did not understand by what you mean by:
Quote:
Originally Posted by rojam
A function CANNOT have a path through it that does not return a valid value. Your function obviously has too many paths through it that leaves it returning nothing.
Anyway, here is the function code which I corrected and completed because it was in build when open the topic.
Code:
Function CheckDataType(FldSource,FldDest:TField):Boolean;
var
vl:string;
Bool:Boolean;
Year, Month, Day:Word;
v2,i:Integer;
dt:TDateTime;
sl:TstringList;
Begin

Result:=True;

If (FldDest.DataType=ftBoolean) Then
Begin
SL := TStringList.create;
SL.Add('TRUE');
SL.Add('FALSE');
SL.Add('VRAI');
SL.Add('FAUX');

vl:=FldSource.AsVariant;
vl:=AnsiUpperCase(vl);

for i := 0 to SL.Count - 1 do
 Begin
   If vl=SL.Strings[i] Then Bool:=True;
 end;

SL.Free;

If Not Bool Then
Begin
Result:=false;
Exit;
end;

Result:=Bool;
end;


If (FldDest.DataType=ftString) Then
Begin
vl:=FldSource.AsVariant;
vl:= VarToStr(vl);
end;

If (FldDest.DataType=ftInteger) Then
Begin
vl:=FldSource.AsVariant;
v2:= VarToInt(vl);
end;

If (FldDest.DataType=ftDate) Then
Begin

 if Not IsDate(FldSource.AsVariant) then
 Begin
 Result:=false;
 Exit;
 end;

decodedate(FldSource.AsDateTime,year,month,day);
If Not IsValidDate(year, month, day) Then
 Begin 
 Result:=false;
 Exit;
 end;

end;
end;
This is the first step and in the second step there is another function which is the same that the function above and when I do the data post.
Thanks and thanks to Norrit for advising me using VarToStr function.

Last edited by Jung; 02-27-2017 at 09:13 PM.
Reply With Quote
  #6  
Old 02-28-2017, 05:39 PM
Jung Jung is offline
Junior Member
 
Join Date: Oct 2014
Posts: 16
Default

The code of your function is very clean and optimized than mine and works fine.
but I didn't see your post I received an email notification
Thank you rojam

Last edited by Jung; 02-28-2017 at 05:42 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.