Assign value of enum to some other variable - delphi

I have the following enum in Delphi:
type TChangingDataSetState=(Inserting=1,Editing,Deleting)
......
var
ChangingDSSsate:TChangingDataSetState;
In BeforePost event I check if the dataset in Insert mode then I
ChangingDSState:=Inserting
else
ChagingDSState:=Editing
Let's say the dataset is in edit mode, it means my ChangingDSState var will get evuluated to 2(Editing). Now I want to know how I can then use that number to pass it as an argument to a procedure

I assume you want the ordinal value rather than the enumerated value. You get that with ord().
So, ord(ChagingDSState) is an integer expression with a value of 2 when ChagingDSState equals Editing.

Related

What is the difference between "var" and "List" in Dart?

Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
With var example the type (static and runtime) for example will be inferred from the assigned value ["some","content",11,45,true] which will be List (or actually List<dynamic>)
With List example the type will not be inferred but the explicitely provided type List (or actually List<dynamic> if no generic type is provided) will be used instead.
For var example = ["some","content","11","45","true"]; the inferred type would be List<String>.
As far as I know and as simple as I can be;
List is a data type just like some other built-in types in Dart such as String, int and bool. When you initialize a variable using List, the assigned value must be of List type. i.e. You cannot do this
List example = "sometext";
because you're trying to assign a String value to a List variable or object here.
Whereas, var is a way to declare a variable without specifying its type. For var will accept all kind of data types.
Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
Both the methods of declaration have same effect unless you expect to assign a value to example with type(s) other than List during it's lifetime. i.e If you're looking to assign an int or double or string or whatever value to example in future use the first method else you can use any one of them.

What is the value returned by AsString when FieldByName('fieldname') has null value?

What value does val, which is of string type, get when the column named fieldName is null for the selected row? Here myQry is a database query.
val := myQry.FieldByName('fieldName').AsString
Here column fieldName does exist in the table, but for the selected row/record, the value is null. I have looked here. It was informative, but didn't have the information I needed I am afraid. I also looked at this but that didn't help much either.
You can test this yourself simply by calling
MyDataSet.FieldByName('MyField').Clear
That sets MyField to Null and, after that, calling AsString on it returns an empty (zero-length) string.
The GetAsString method of TField descendants typically contain code like this:
function TIntegerField.GetAsString: string;
var
L: Longint;
begin
if GetValue(L) then Str(L, Result) else Result := '';
end;
Here, GetValue succeeds if it is possible to retrieve a value from the current record buffer. If it fails, the field is taken to contain Null.
Typically, depending on the actual type of field, the empty string is returned. But this does depend on the implementation of the TField descendant, and you have given no indication of what that is.
In order to distinguish between null and an empty string, use the IsNull property of the field.
AsString returns the String value for the field if it has value or the empty string if it does not have the value.
AsString does not use DisplayText or other things.
It is absolutely safe to call AsString on NULL value fields.

Calc field doesn't post

Hi I want to post calc field(cemi) to table (sql). when I calc all field the last field doesn't post on sql table. because last field (cemi) type fkcalc how can I post fkcalc type field to sql table Thanks in advance!
procedure TForm1.ADOQuery1CalcFields(DataSet: TDataSet);
begin
ADOQuery1.FieldValues['cemi']:=
((ADOQuery1.FieldValues['boyuk1'] + ADOQuery1.FieldValues['boyuk2'] +
ADOQuery1.FieldValues['boyuk3'])*0.35)+((ADOQuery1.FieldValues['kicik1'] +
ADOQuery1.FieldValues['kicik2'])*0.25) +(ADOQuery1.FieldValues['qara1']*0.30);
end;
I'm not quite sure what you mean by
the last field doesn't post on sql table
If the "last field" you are referring to is your "Cemi" one and that is a column which is in the table on your SQL Server, it will not get posted back there if you have defined it as a calculated field in your AdoQuery1 in the Object Inspector. Fields with a FieldKind of fkCalculated are local to the AdoQuery.
Just assigning a value to the calculated field is sufficient to "post" it locally to the AdoQuery, as I imagine you know. What you want to do to debug your problem (because readers like me cannot debug it fr you) is to more easily see what value, if any, is being assigned to it.
From that point of view, your code is suffering from "premature optimisation" which will make it difficult for you to see what is going wrong. Try this instead:
In your ADOQuery1CalcFields, declare a local variable for each of the fields you are accessing, including the calculated one. Choose the variable types to suit the fields:
var
Boyuk1 : Double; // or Integer, etc
[...]
Cemi : Double;
Assign values to the local variables, using the AsXXXX (type) of the fields:
Cemi := 0;
if not AdoQuery1.FieldByName('Boyuk1').IsNull then
Cemi := Cemi + AdoQuery1.FieldByName('Boyuk1').AsFloat;
[etc]
That way, at least you'll be able to see the point at which the calculation goes wrong (if it does).
I've used FieldByName().AsFloat, rather than FieldValues[], because FieldValues[] is a Variant, which can be Null, and you don't want that when you are assigning values to it which mat themselves be variants.
Also
Check that AutoCalcFields is set to True for AdoQuery1.
Put a debugger breakpoint on the first line of ADOQuery1CalcFields. Compile and run and check that the breakpoint hits - if it doesn't, there's your answer. Single-step the debugger through each line of the procedure, and, after the final line, use Ctrl-F7 to evaluate the value of AdoQuery1.FieldByName('Cemi').AsFloat.

What is the difference between AsInteger and Value in Delphi?

I want to know the difference between following two statements related to datasets in delphi.
dsMyDataSet.ParamByName('ID').AsInteger := 1122; //If ID is integer
dsMyDataSet.ParamByName('ID').AsString := '1122'; //If ID is string
and
dsMyDataSet.ParamByName('ID').Value := 1122; //ID is string or integer
Do these statements carry same meaning? Does "value" implicitly converts integer into string?
The TParam.AsInteger property, for instance, set the value and the data type of the parameter. TParam.Value does the same, but TParam will decide which type will be mapped to the value inside the Variant and not always it´s the data type you would like.
I advise you to set values by using the AsXXX properties only, since you will be in control of the parameter's data type, what can save you from having parameter binding errors.
So, answering your final question: no, the values won´t be converted to the right data type, you have to set the data type by selecting the right property to assign the value.

cxGrid calculate as you type

Simplified I have a cxGrid where I type a start time and a end time. What I need is a function that when I change one of these values calculates the difference and stores this in a third column
I am having trouble finding the correct way of doing this.
I guess you have assigned some sort of Properties editor (probably a DateEdit) to the column.
Given that, you could try to use following code in the OnValidate event of the Properties editor:
var
ValueThirdCol : variant;
RecordIndex : integer;
begin
RecordIndex := myView.DataController.FocusedRecordIndex;
ValueThirdCol := myView.DataController.GetValue(RecordIndex, MyEndDateCol.Index) - myView.DataController.GetValue(RecordIndex, MyStartDateCol.Index);
myView.DataController.SetValue(RecordIndex, myDifCol.Index, ValueThirdCol);
end;
Please note that you might have to tweak this code a bit depending on if you have set GridMode or DataModeController.SyncMode to true, or not, and to use DisplayValue where necessary, but the basic idea should work.
EDIT: the OnValidate event of the Properties editor occurs before converting the display value to the edit value. That is the reason why this code I provided had to be tweaked.
In order for the code to work, you need to use (for the column being modified) the DisplayValue argument provided by the event instead of the value returned by GetValue.
For example, if the EndDateCol would be the column that triggered the OnValidate, then the code should be
ValueThirdCol := DisplayValue - myView.DataController.GetValue(RecordIndex, MyStartDateCol.Index);
HTH

Resources