I was just looking for some code on the internet for sorting and I found this, but I am having trouble understanding it >__> as usual...Code:procedure TForm1.DBGrid1TitleClick(Column: TColumn); {$J+} const PreviousColumnIndex : integer = -1; {$J-} begin if DBGrid1.DataSource.DataSet is TCustomADODataSet then with TCustomADODataSet(DBGrid1.DataSource.DataSet) do begin try DBGrid1.Columns[PreviousColumnIndex].title.Font.Style := DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold]; except end; Column.title.Font.Style := Column.title.Font.Style + [fsBold]; PreviousColumnIndex := Column.Index; if (Pos(Column.Field.FieldName, Sort) = 1) and (Pos(' DESC', Sort)= 0) then Sort := Column.Field.FieldName + ' DESC' else Sort := Column.Field.FieldName + ' ASC'; end; end;
This code, as is stated, sorts the column selected (when you click on the column), if it has already been sorted, it will sort it the other way, it will also turn the title bolded to show that it is currently being sorted.
When clicking on another title, to sort another column, the sorting is deleted from the previous column, so that the new one can be sorted properly.
1) "if ... is ... ", is this the same as "if... = ..." ?
2) The stuff I don't get is in Bold. Like, I really dont understand what the Tcustom thing is.
With those answered, I think Ill be able to get the jist of things.
Thanks
The "if ... is ..." tests if an object is of a given class type, which is different to an '=' which tests if an object ptr is the same as another object ptr. ie: The same object stored in two different ptr's.
The "pos" code is just checking to see if the Sort property contains the selected column field and is assending, in which case it will "flip" and change it to decending.
Otherwise, it will just set the Sort property to sort the selected field assending.
BTW, This code only works with TCustomADODataSet's, hence the type checking at the start.
Other similar code generates SQL or IndexFieldName depending on the database components you are using.
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
pos->searches a sring in another sting and returns the position or 0 for not found
with->the next statement (or compound statment) assumes that if a field, property or method of a record, object or class is used, it pertains to this object/record/class
TCustomADODataSet(DBGrid1.DataSource.DataSet)->Makes the compiler believe that "DBGrid1.DataSource.DataSet" is a "TCustomADODataSet" (even if it isn't, but it's already proven by ising is)
Please be advised that this code uses a DBGrid and a dataset (for databases).
The sorting isn't done here. The database engine does the sorting. For sorting code, look for quicksort. Or check your classes.pas file and search for QSort.
"Is"
As mentioned by alienkinetics the IS command compares class types , since the TADODataSet class is derived from the TCustomADODataSet class, comparing any TADODataSet component using " component is TCustomADODataSet" will return true
"AS"
the 'as' command is used to typecast classes, (make a component look like a different class of component),
these 2 commands are most commonly used in events like OnClick when lots of components are assigned to use the same procedure and the component is passed as a TObject
e.g.
create a new form called MyForm, throw some buttons , edit boxes and panels onto it
set all the panels edit boxes and buttons Onclick events to ClickFunction
set the edit boxes onChange event to ClickFunction as well
have fun clicking on themCode:procedure MyForm.ClickFunction(Sender:TObject); Begin if Sender is TEdit then Myform.caption:=(Sender as TEdit).text; if Sender is TButton then Myform.caption:=(Sender as TButton).caption; if Sender is TPanel then Myform.color:=(Sender as Tpanel).color; end;
"With"
the with command is actually better to be thought of as a compiler directive, it directs all ambiguously coded elements to a specified record, or class
e.g
is the same asCode:with MyForm do // dirrect all ambiguously coded elements to MyForm begin caption:="Caption"; left:=0; Top:=0; end;
Code:begin MyForm.caption:="Caption"; MyForm.left:=0; MyForm.Top:=0; end;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks