I'm trying to store a set inside the object property (and read it) of a TStringList (I will also use it to store text associated to the set) but I get a invalid typecast for the set.
What's the best way to store a set inside a StringList object? Also, will this object need to be freed when destroying the StringList?
Here's some example code:
type
TDummy = (dOne, dTwo, dThree);
TDummySet = set of TDummy;
var
DummySet: TDummySet;
SL: TStringList;
begin
SL := TStringList.Create;
Try
DummySet := [dOne, dThree];
SL.AddObject('some string', TObject(DummySet)); // Doesn't work. Invalid typecast
Finally
SL.Free;
End;
end;
First read the other answers - probably you'll find a less hacky solution.
But FTR: You can write
SL.AddObject('some string', TObject(Byte(DummySet)));
and
DummySet := TDummySet(Byte(SL.Objects[0]));
if you really want.
Note: You'll have to change the keyword Byte if you add enough elements to the TDummySet type. For example, if you add six more elements (so that there is a total of nine) you need to cast to Word.
I can't add non objects on that case.
What you can do, is create an object that have TDummySet as Field.
Something like
TExemple = class
DummySet = TDummySet;
end;
Or you can use a different approach:
Declarations:
TDummy = (dOne, dTwo, dThree);
TDummySet = set of TDummy;
PDummySet = ^TDummySet;
How to use:
var
DummySet: PDummySet;
begin
New(DummySet);
DummySet^ := [dOne, dThree];
You should not store a set via TStringList.Objects because what Objects use (TObject) is a 32 bit value type and sets can be represented up to 256 bits depending on the size of the set. That's probably why the compiler doesn't even allow casting.
A better way to serialize sets is using RTTI. I am not sure where VCL exposes its builtin set serialization mechanism but JCL has a JclRTTI unit with JclSetToStr and JclStrToSet functions.
var
fs: TFontStyles;
begin
JclStrToSet(TypeInfo(TFontStyles), fs, 'fsBold, fsItalic'); // from string
Showessage(JclSetToStr(TypeInfo(TFontStyles), fs)); // to string
end;
I don't think a stringlist is the way to go. Why not an array of TDummySet? And no, there is no need to free it because the set is not an object.
var
Test: Array of TDummySet;
SetLength(Test, 2);
Test[0] := [dOne, dThree];
Test[1] := [dTwo];
When you're done:
SetLength(Test, 0);
You cannot make a typecast from your set to a TObject, because your variable is not a pointer.
You have to store a pointer to your variable in the TStringList. In that case, you'll have to allocate and deallocate it manually too.
Try something like this:
type
TEnum = (one, two, three);
TSet = set of TEnum;
PSet = ^TSet;
var s: TStringList;
p: PSet;
begin
s := TStringList.Create;
p := AllocMem(SizeOf(TSet));
p^ := [two, three];
S.AddObject('a', TObject(p));
// bla bla bla
// Here you read the set in the string list
if (two in PSet(S.Objects[0])^)) then begin
// your checks here
end
...
Related
In pascal, the only way I dared cleaning my array was to simply iterate through it and clear it, but it is extremely inefficient. Can't I simply reinitialize it by assigning an empty array to it?
program arrays;
var
working, empty : array [1..10] of integer;
begin
working[3] := 5;
working:= empty;
end.
Is is ok to do this, can this backfire?
If you want to clear the array, writing:
working:= empty;
will in fact do the clearing, by copying the empty array content into working... in your case empty is void, since it is a global variable, so initialized with 0.
IMHO it is not a good practice to define such global variables. Global variables are evil in most cases (unless you know what you are doing), and in case of declaring them to be initialized with 0 does not make sense.
In fact, if empty is initialized on the stack (i.e. a var within a method), it is filled with whatever is on the stack at this time, i.e. some random data.
If you want to fast initialize an array which does not contain any reference counted types (like string), you can write:
fillchar(working,sizeof(working),0);
And if your array contains managed types, you can write:
finalize(workingWithStringInside); // to safely release internal managed types
fillchar(workingWithStringInside,sizeof(workingWithStringInside),0);
This is the faster code possible (faster than a variable copy), and sounds a better option.
This is absolutely fine. The semantics of the code are exactly what you need. Certainly the Delphi compiler will emit code to perform a simple and efficient memory copy. The compiler is able to do that because you have a fixed length array whose elements are simple value types. I'd be surprised if FPC did not produce very similar code.
Even if your array contained managed types (it doesn't), the assignment operator would result in code that respected those managed types.
As a final comment, the array full of zeros should be a constant.
An easy way is not to set your length of the variable in the type...and use SetLength to initialize the array for you... from Delphi help: When S is a dynamic array of types that must be initialized, newly allocated space is set to 0 or nil.
type
TIntArray = Array of Integer;
procedure WorkArrays(var aWorking: array of integer);
begin
if High(aWorking) >= 0 then
aWorking[0] := 1;
if High(aWorking) >= 3 then
aWorking[3] := 5;
end;
procedure WorkArrays2(var aWorking: array of integer);
begin
if High(aWorking) >= 1 then
aWorking[1] := 4;
if High(aWorking) >= 9 then
aWorking[9] := 7;
end;
procedure WorkArrays3(var aWorking: TIntArray);
begin
SetLength(aWorking, 0);
SetLength(aWorking, 4);
aWorking[0] := 1;
aWorking[3] := 5;
end;
procedure WorkArrays4(var aWorking: TIntArray);
begin
SetLength(aWorking, 0);
SetLength(aWorking, 10);
aWorking[1] := 4;
aWorking[9] := 7;
end;
procedure TForm58.ShowArrays(aWorking: array of integer);
var
a_Index: integer;
begin
for a_Index := Low(aWorking) to High(aWorking) do
Memo1.Lines.Add(IntToStr(aWorking[a_Index]));
end;
procedure TForm58.ShowArrays2(aWorking: TIntArray);
var
a_Index: integer;
begin
for a_Index := Low(aWorking) to High(aWorking) do
Memo1.Lines.Add(IntToStr(aWorking[a_Index]));
end;
procedure TForm58.Button1Click(Sender: TObject);
var
a_MyArray: array of integer;
a_MyArray1: TIntArray;
begin
//SetLength(aWorking, 0);
SetLength(a_MyArray, 3);//note this is a Zero based Array...0 to 2
WorkArrays(a_MyArray);//note aWorking[3] will not show...because High is 2...
ShowArrays(a_MyArray);
SetLength(aWorking, 0);
SetLength(a_MyArray, 10);//note this is a Zero based Array...0 to 9
WorkArrays2(a_MyArray);
ShowArrays(a_MyArray);
WorkArrays3(a_MyArray1);
ShowArrays2(a_MyArray1);
WorkArrays4(a_MyArray1);
ShowArrays2(a_MyArray1);
end;
I need to be able to compare two different connection strings together and identify whether or not they are the same info. I cannot do a simple string comparison, because the properties could be laid out differently, but still represent the same connection.
Before I go and write my own comparison for this, is there already something that can do this?
I've searched for a way and I can't find anything out there about this.
You might use the IDataInitialize::GetDataSource method, which returns an uninitialized data source object from a given connection string. Because of this method returns pointer to a data source object of a IUnknown type, you cannot directly compare the objects obtained for your two compared connection strings. However, you can query the IDBProperties interface on those uninitialized data source objects what gives you a possibility to access all properties supported by a given provider.
To get a property set, you need to use IDBProperties::GetProperties method. This will return a DBPROPSET structure, which contains an array of DBPROP elements (properties). You will then simply iterate this array and compare the properties of those two data source objects in a way you need.
The following IsSameConnStr function returns True if the connection strings equal, False otherwise. Note, that the used property value comparison is case insensitive except the DBPROP_AUTH_PASSWORD property, which is compared with case sensitivity:
uses
ActiveX, ComObj, OleDB;
function IsSameVarWideStr(const AValue1, AValue2: OleVariant;
ACaseSensitive: Boolean = False): Boolean;
begin
Result := VarType(AValue1) = VarType(AValue2);
if Result then
begin
if ACaseSensitive then
Result := WideCompareStr(VarToWideStr(AValue1),
VarToWideStr(AValue2)) = 0
else
Result := WideCompareText(VarToWideStr(AValue1),
VarToWideStr(AValue2)) = 0;
end;
end;
function IsSameConnStr(const AConnStr1, AConnStr2: WideString): Boolean;
var
I: Integer;
DataSrc1: IUnknown;
DataSrc2: IUnknown;
DataInit: IDataInitialize;
PropSet1: PDBPropSet;
PropSet2: PDBPropSet;
PropSetCnt1: ULONG;
PropSetCnt2: ULONG;
Properties1: IDBProperties;
Properties2: IDBProperties;
const
DBPROP_AUTH_PASSWORD = $00000009;
begin
// first check if the input connection strings aren't exactly the same
Result := CompareStr(AConnStr1, AConnStr2) = 0;
// if they are not same, then...
if not Result then
begin
// create IDataInitialize object instance
OleCheck(CoCreateInstance(CLSID_DataLinks, nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IID_IDataInitialize, DataInit));
// get data source objects for both input connection strings
OleCheck(DataInit.GetDataSource(nil, CLSCTX_INPROC_SERVER,
PWideChar(AConnStr1), IUnknown, DataSrc1));
OleCheck(DataInit.GetDataSource(nil, CLSCTX_INPROC_SERVER,
PWideChar(AConnStr2), IUnknown, DataSrc2));
// query for IDBProperties objects of the data source objects
if Succeeded(DataSrc1.QueryInterface(IID_IDBProperties, Properties1)) and
Succeeded(DataSrc2.QueryInterface(IID_IDBProperties, Properties2)) then
begin
// get properties of data source objects
OleCheck(Properties1.GetProperties(0, nil, PropSetCnt1, PropSet1));
OleCheck(Properties2.GetProperties(0, nil, PropSetCnt2, PropSet2));
try
// same DB provider will have the same set of initialization properties,
// so the first check might be the property count, if that differs, then
// at least DB provider is different, so if this equals, then...
if PropSetCnt1 = PropSetCnt2 then
begin
// initialize positive result
Result := True;
// iterate all the properties
for I := 0 to PropSet1.cProperties - 1 do
begin
// check if we're comparing the same property and if so, compare the
// property values; for password property compare the value with case
// sensitivity, for all the others case insensitively; if any of this
// doesn't match, we're done with False result and we can exit
if (PropSet1.rgProperties[I].dwPropertyID <>
PropSet2.rgProperties[I].dwPropertyID) or
not IsSameVarWideStr(PropSet1.rgProperties[I].vValue,
PropSet2.rgProperties[I].vValue,
PropSet1.rgProperties[I].dwPropertyID = DBPROP_AUTH_PASSWORD) then
begin
Result := False;
Break;
end;
end;
end;
finally
// release the property sets; note that you should avoid this common
// try..finally block and that you should free also each property array
// element by using IMalloc::Free; why I've used CoTaskMemFree see this
// question http://stackoverflow.com/q/3079508/960757
CoTaskMemFree(PropSet1);
CoTaskMemFree(PropSet2);
end;
end;
end;
end;
The usage is clear I think, so I'll rather mention results for some connection strings:
IsSameConnStr = True
AConnStr1: Provider=MSDASQL.1;Persist Security Info=True;Data Source=datasource
AConnStr2: Provider=MSDASQL.1;Persist Security Info=True;Data Source=DATASOURCE
IsSameConnStr = True
AConnStr1: Provider=MSDASQL.1;Data Source=datasource;Persist Security Info=True
AConnStr2: Provider=MSDASQL.1;Persist Security Info=True;Data Source=DATASOURCE
IsSameConnStr = True
AConnStr1: Provider=MSDASQL.1;Password=PASSWORD;Data Source=datasource;Persist Security Info=True
AConnStr2: Provider=MSDASQL.1;Data Source=DATASOURCE;Password=PASSWORD;Persist Security Info=True
IsSameConnStr = False - password differs in case sensitivity
AConnStr1: Provider=MSDASQL.1;Password=PASSWORd;Data Source=datasource;Persist Security Info=True
AConnStr2: Provider=MSDASQL.1;Data Source=DATASOURCE;Password=PASSWORD;Persist Security Info=True
To get a collection of the ConnectionString properties, you can assign the ConnectionString to TADOConnection (without actually connecting to the DB) and use TADOConnection.Properties collection (collection item is ADOInt.Property_) e.g.:
ADOConnection.Properties.Get_Item('Data Source')
You should probably compare specific properties to determine if the connection is set to a specific data store via a specific provider. e.g.:
Provider, Data Source, Initial Catalog, User ID \ Password (optional).
There are many properties that you might want to ignore depending on the provider e.g:
Workstation ID, Persist Security Info, Use Procedure for Prepare, Auto Translate, etc...
Here is an example how to iterate the TADOConnection properties collection:
var
ADOConnection: TADOConnection;
PropName, PropValue: WideString;
I: Integer;
ADOConnection := TADOConnection.Create(nil);
try
ADOConnection.ConnectionString := 'Provider=MSDASQL.1;Password=secret;Data Source=127.0.0.1;User ID=user;Initial Catalog=mycatalog';
for I := 0 to ADOConnection.Properties.Count - 1 do
begin
// Properties.Item[I] is ADOInt.Property_
PropName := ADOConnection.Properties.Item[I].Name;
PropValue := VarToWideStr(ADOConnection.Properties.Item[I].Value);
ShowMessage(Format('%s=%s', [PropName, PropValue]));
end;
finally
ADOConnection.Free;
end;
There might be much more properties that are added to/changed in the ConnectionString after TADOConnection was connected to the DB, so you need to take this into account.
I want to pass multiple objects as one parameter with the smallest effort.
I've got some type
TOpenMode = [omNew, omEdit, omBrowse]
And a procedure
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: TUniDataSet; TableOpenMode: TOpenMode);
I want to pass more than one dataset. Can I do that without arrays or creating new objects? How can I make them to be passed in pairs [UniTable1, TOpenMode], [UniTable2, TOpenMode]?
The simplest way to combine multiple objects in a single compound type is a record:
type
TDataSetAndOpenMode = record
DataSet: TUniDataSet;
OpenMode: TOpenMode;
end;
For convenience provide a function to initialise one of these records:
function DataSetAndOpenMode(DataSet: TUniDataSet;
OpenMode: TOpenMode): TDataSetAndOpenMode;
begin
Result.DataSet := DataSet;
Result.OpenMode := OpenMode;
end;
Then your OpenForm function can receive an open array of such records:
procedure OpenForm(Form: TForm; const Datasets: array of TDataSetAndOpenMode;
NewWindow: Boolean=False);
Note that I have put the NewWindow parameter at the end. Since it has a default value, that default value is only useful when it appears at the end of the list.
Now, to call the function you can write code like this:
OpenForm(Form, [DataSetAndOpenMode(DataSet1, OpenMode1),
DataSetAndOpenMode(DataSet2, OpenMode2)]);
If you want to pass multiple pairs as one parameter, I don't see how you can avoid declaring at least a record to define the pair and at least an open array parameter to pass multiple instances of those records as one parameter.
type
TDatasetModePair = record
DS: TUniDataSet;
Mode: TOpenMode;
end;
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: array of TDatasetModePair);
But you'll probably find that it will be much easier to declare your own array type:
type
TDatasetModePairArray: array of TDatasetModePair;
the procedure declaration then becomes:
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: TDatasetModePairArray);
Regardless of that though, there is no way around having to create the array before you can pass it to your function:
var
MyArray: TDatasetModePairArray;
begin
SetLength(MyArray, 2);
MyArray[0].DS := SomeDataSet;
MyArray[0].Mode := omEdit;
MyArray[1].DS := SomeOtherDataSet;
MyArray[1].Mode := omBrowse;
const
states : array [0..49,0..1] of string =
(
('Alabama','AL'),
('Montana','MT'),
('Alaska','AK'),
('Nebraska','NE'),
('Arizona','AZ'),
('Nevada','NV'),
('Arkansas','AR'),
('New Hampshire','NH'),
('California','CA'),
('New Jersey','NJ'),
('Colorado','CO'),
('New Mexico','NM'),
('Connecticut','CT'),
('New York','NY'),
('Delaware','DE'),
('North Carolina','NC'),
('Florida','FL'),
('North Dakota','ND'),
('Georgia','GA'),
('Ohio','OH'),
('Hawaii','HI'),
('Oklahoma','OK'),
('Idaho','ID'),
('Oregon','OR'),
('Illinois','IL'),
('Pennsylvania','PA'),
('Indiana','IN'),
('Rhode Island','RI'),
('Iowa','IA'),
('South Carolin','SC'),
('Kansas','KS'),
('South Dakota','SD'),
('Kentucky','KY'),
('Tennessee','TN'),
('Louisiana','LA'),
('Texas','TX'),
('Maine','ME'),
('Utah','UT'),
('Maryland','MD'),
('Vermont','VT'),
('Massachusetts','MA'),
('Virginia','VA'),
('Michigan','MI'),
('Washington','WA'),
('Minnesota','MN'),
('West Virginia','WV'),
('Mississippi','MS'),
('Wisconsin','WI'),
('Missouri','MO'),
('Wyoming','WY')
);
function getabb(state:string):string;
var
I:integer;
begin
for I := 0 to length(states) -1 do
if lowercase(state) = lowercase(states[I,0]) then
begin
result:= states[I,1];
end;
end;
function getstate(state:string):string;
var
I:integer;
begin
for I := 0 to length(states) -1 do
if lowercase(state) = lowercase(states[I,1]) then
begin
result:= states[I,0];
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
edit1.Text:=getabb(edit1.Text);
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
edit1.Text:=getstate(edit1.Text);
end;
end.
Is there a bette way to do this?
Should this kind of data be hard coded?
Wouldn't it be better to use something like a XML file or even just a CSV.
Or Name Value Pairs, i.e. IA=Iowa
then loaded into a TStringList to get
States.Values['IA'] = 'Iowa';
Then you just need to write something to search the Values to work backwards like
//***Untested***
//Use: NameOfValue(States, 'Iowa') = 'IA'
function NameOfValue(const strings: TStrings; const Value: string): string;
var
i : integer;
P: Integer;
S: string;
begin
for i := 0 to strings.count - 1 do
begin
S := strings.ValueFromIndex[i];
P := AnsiPos(strings.NameValueSeparator, S);
if (P <> 0) and (AnsiCompareText(Copy(S, 1, P - 1), Value) = 0) then
begin
Result := strings.Names[i];
Exit;
end;
end;
Result := '';
end;
I'm fairly sure its case insensitive too
If you're on D2009 or D2010, use a TDictionary<string, string> from Generics.Collections. Declare the array of constants like you have it, then set up your dictionary by putting each pair in to the dictionary. Then just use the dictionary's default property to do your lookups.
Notice that lowercase(a) = lowercase(b) is slower than sameText(a, b).
In addition, you can speed up the procedure further by storing the strings in the array as lower-case only, and then in the look-up routine start with converting the input to lower-case as well. Then you can use the even faster function sameStr(a, b). But of course, when a match is found, you then need to format it by capitalizing the initial letters. This speed-up approach is probably not very important for such a small list of strings. After all, there are not too many states in the US.
Also, you should declare the functions using const arguments, i.e. write
function getabb(const state:string):string;
instead of
function getabb(state:string):string;
(unless you want to change state in the routine).
Finally, you could make the code more compact and readable by omitting the begin and end of the for loops.
I would have your lists sorted. That way you can use a binary search to cut the lookup times down. It all depends on the number of iterations you will be exercising. Around 50 items doesn't seem like much, until your iterating over the list a few thousand times looking for the last item in the list.
Also you should ALWAYS bail from your loops as soon as you get get a match if you know the rest of the list will not match.
Arrays are fine, and depending on how your using the data, you might need to add some of the "territories" that also have abbreviations (PR = PUERTO RICO, GU = GUAM, etc.).
First, apologies for my English, I hope it makes sense what I`ve written here. Now to my problem.
How can I get the string representation of the content type of a Variant using TypInfo.GetEnumName(). I have tried the following, without luck, I get a numeric representation.
myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).VType );
Thank you.
Just use the build-in Delphi function for getting the string representation of a Variant type.
var
MyVariantType: string;
MyVariant: Variant;
begin
MyVariant := 'Hello World';
MyVariantType := VarTypeAsText(VarType(MyVariant));
ShowMessage(MyVariantType); //displays: String
MyVariant := 2;
MyVariantType := VarTypeAsText(VarType(MyVariant));
ShowMessage(MyVariantType); //displays: Byte
end;
Quoting from the Delphi 2007 help:
Use GetEnumName to convert a Delphi enumerated value into the symbolic name that represents it in code.
That means that you can't use it for that purpose, as TVarData.VType is not an enumerated value, but an integer which is set to one of the constants in System.pas that are taken from the Windows SDK wtypes.h file. Look at the source of GetEnumName(), it does immediately return a string containing the value of the integer.
Edit:
is there any other way to get the string representation of TVarData.VType
You can determine the string representation manually. First you need to be aware of that there are several bits of information encoded in that integer, so a simple case statement or array lookup will not work. The lower 12 bits are the type mask, and the upper bits encode information about whether it is a vector or array type and whether it is given by reference or not. The important parts are:
const
varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;
So you could do something like:
function VariantTypeName(const AValue: TVarData): string;
begin
case AValue.VType and varTypeMask of
vtInteger: Result := 'integer';
// ...
end;
if AValue.VType and varArray <> 0 then
Result := 'array of ' + Result;
if AValue.VType and varByRef <> 0 then
Result := Result + ' by ref';
end;
Since it's not an enum, you'll have to do it manually. Write something like this:
function VariantTypeName(const value: TVarData): string;
begin
case value.VType of
vtInteger: result := 'integer';
//and so on
end;
Or, since the values in System.pas are listed in order, you could try declaring a const array of strings and have your VariantTypeName function return the appropriate member of the array.
Here's a thought for Delphi versions that don't support VarTypeAsText: You could define a enumerate type yourself that follows the VType values:
type
{$TYPEINFO ON}
TMyVarType = (
varEmpty = System.varEmpty,
varNull = System.varNull,
// etc...
);
(Fill the unused enum slots too - see Why do I get "type has no typeinfo" error with an enum type for the reasoning behind this).
Next, use these functions to read the Variants' type as your own enumerate type :
function MyVarType(VType: TVarType): TMyVarType; overload;
begin
Result := TMyVarType(VType);
end;
function MyVarType(V: Variant): TMyVarType; overload;
begin
Result := TMyVarType(TVarData(V).VType);
end;
And then you can convert it to a string like this :
function VarTypeToString(aValue: TMyVarType): string;
begin
Result := GetEnumName(TypeInfo(TMyVarType), Ord(aValue));
end;