|
Preliminary results confirm my theory:
as works well with a homogenous collection
is works well with a heterogenous collection
But cast/catch/ignore also works well with a homogenous collection, at times even better than as ! (Not that I recommend it... much.) It is horrible with a heterogenous collection.
On the other hand, as doesn't work with value types, but I've had success testing it with nullable types.
-- modified at 23:52 Tuesday 17th July, 2007
Whoops, need to add that the if ( o.GetType() == typeof(sometype) ) method is faster than both is and as , but at the cost of not checking the inheritance tree.
|
|
|
|
|
Hello,
Thanks for the test results!
PIEBALDconsult wrote: that the if ( o.GetType() == typeof(sometype) ) method is faster than both is and as
Wow, I have not seen this comming!
PIEBALDconsult wrote: But cast/catch/ignore also works well with a homogenous collection, at times even better than as ! (Not that I recommend it... much.) It is horrible with a heterogenous collection.
Yep, it is the Exception calls, which makes it so expensive I think!
Again thanks for taking time to test it.
All the best,
Martin
|
|
|
|
|
PIEBALDconsult wrote: I still haven't found a worthwhile use for as.
Generally if I need to know whether a variable is of a particular type, I need to use some method of that type that isn't in the base type. So for example, if I wanted to select all the text in the textboxes on a form, where the form has other types of control as well, I'd write:
TextBox tb;
foreach( Control ctl in Controls )
{
if ( null != ( tb = ctl as TextBox ) )
{
tb.SelectAll();
}
} Using as saves performing the type check twice: once to get the result of is , and again to perform the cast.
|
|
|
|
|
I understand that, I just wonder if trading a test for an assignment is worthwhile, in some cases it may well be, but perhaps not in such a simple example. In your example, if you perform a number of operations on tb, then you may well see better performance by having the one assignment rather than a bunch of casts.
|
|
|
|
|
if (null != Something) {} looks really strange to me for some reason.
|
|
|
|
|
Oh, well then try the cast and catch/ignore the exception.
|
|
|
|
|
Far, far, far slower than using as . Exceptions take milliseconds to handle because they require a pass through the kernel.
|
|
|
|
|
(I meant to put a smiley on that post.)
My preliminary results show cast/catch/ignore to actually be faster... sometimes... on a small enough homogenous collection... where no exceptions are actually thrown...
If I understand the original poster's design, he has TabPages that contain some (small) number of instances of his custom control and nothing else; that would qualify as a small homogenous collection, and if in this case:
A) Speed truly outweighs good design (doubtful)
and
B) The presence of some other type of Control in the collection should trigger an Exception
then why not simply perform the cast and catch the exception as you would anyway?
|
|
|
|
|
Every control, including Form has a property called Controls . And of course in cases like TabControl , controls are nested. So the form will have the TabControl in it's Controls collection, and each TabPage in the TabControl will have whatever is on the tab in it's controls collection. If the IDs you give each control is enough information for you to tell what you're looking at, you should be able to do what you need that way.
foreach (Control tab in this.Controls) {
TabControl t = tab as TabControl;
if (t == null) { continue; }
foreach (TabPage p in t.TabPages) {
foreach (Control cp in p.Controls) {
if (!(cp is MyControl)) { continue; }
MyControl mc = cp as MyControl;
}
}
}
|
|
|
|
|
I need to create an application where a user can select some records from a DataGridView and create a Crystal Report based on those records. I basically created most of the necessary functionality using the DataGridView.SelectedRows property to get the selected rows and then importing those rows into a data table that I use to create the report, like so:
foreach(DataGridViewRow r in this.gridView_Hydro_Results.SelectedRows)
{
int selectIndex = r.Index;
row = resultsTable.Rows[selectIndex];
dtSelectedRecords.ImportRow(row);
}
Unfortunately, when I create the report, the records are not in the order of selection. Instead, the records are in the reverse order. It seems like the rows are getting added in a last in, first out order. While that may be easy to counter, my customers actually probably don't care what order the y select the records in, but want the report ordered according to some field in the database, like time or date. I suppose I could build a method to sort the Data Table (or the collection gridview rows) after I create it, but I was wondering if there is a more elegant solution.
In the same vein, is it possible to use a generic list collection in a SQL statement. I was thinking of adding the value of the primary key for each selected row to a List<> object and then using a SELECT * FROM Table WHERE KEY IN (List<>), but am not sure if that will work.
Anyway, any advice would be appreciated. Thanks.
|
|
|
|
|
Have you tried
dtSelectedRecords.Rows.Add(row)
instead? I've always gone this route, and not sure about how ImportRow behaves when inserting rows.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
Thanks for the advice.
I actually did try the dtSelectedRecords.Add(row) command first, but kept getting the error that the row already belongs to another table. I didn't want to delete the row from the original table (because the original table is the one that's bound to the grid view), so I had to find some other method that imports a copy of the rows. ImportRow fits that need and it seems to simply insert the records into the table at the last position in the table. What seems to have caused the trouble is the way the rows are added to the DataGridViewRowCollection.SelectedRows collection. I think the rows are added in a last in-first out structure.
What I wound up doing-something that works for now-is import all the rows into the datatable that I need in there and then sort the datatable using the DataTable.Select function to assign the sorting criteria. I import the rows from the DataRow [] into a target table from which I create the report. It's a bit more convoluted than I want and it might not be the most efficient method, especially if the user is selecting quite a few records, but it seems to do the job.
|
|
|
|
|
Hi All,
i have a list view with two column header , one is for ItemName and second for ItemDescription , now i have to implement a search in the list view ,
and i have to make my scrollbar to be at the correct item and i have to make the selected text colored .
Can anyone has any idea how to do it , please?
Praveen Sharma
|
|
|
|
|
foreach (ListViewItem lvi in listView1.Items) {
if (lvi.IsTheOneIWant) {
lvi.ForeColor = Color.Red;
}
}
If you need help with determining if any given item is the one you're looking for, you'll need to be more specific. I personally like to put the object I'm representing in the ListViewItem 's Tag property so I can pull it out and inspect it. Gives you a lot more options when doing something like this.
|
|
|
|
|
hi ,
what is IsTheOneIWant in the code it's not listviewItem property
Praveen Sharma
|
|
|
|
|
Umm, that's where you need to determine if that's the item you're interested in. I thought that was pretty clear.
|
|
|
|
|
Adding to the previous reply, once you find your item, be sure to call its EnsureVisible method (in ListViewItem ). That will scroll the list view to show that item.
|
|
|
|
|
thanx for response ,
actually i want to search a particular text in the list view like if there is text "Cancel The Item" and i want to search search item then i want to make red only Item not the whole , it's like the search we have in wordpad or notepad .
Praveen Sharma
|
|
|
|
|
You mean only the word and not the full row? That's imposible with the built-in functionality. The most you can do is set all the text in one column to a different color, but not one word within a column.
You need to draw the text yourself (which can be some work). If you are interested, look into list view custom draw.
|
|
|
|
|
can someone explane how to create cookie and which file or class to put it?
thank you
|
|
|
|
|
daku1 wrote: can someone explane how to create cookie and which file or class to put it?
thank you
Did you try to google "c# cookie"?
In case you never heard of the google.
Webster's New Millennium Dictionary of English
Main Entry: google
Part of Speech: verb
Definition: to search for information about a specific person through the Google search engine
Example: She googled her high school boyfriends.
Etymology: trademark Google
Usage: googling n
|
|
|
|
|
Let's see. 2 sticks butter (softened), flour, baking soda, a couple eggs, pinch of salt, sugar, brown suger, a bit of vanilla extract, chocolate chips...
Seriously, your question is a bit vague. Is this an ASP.NET application? Are you trying to set a cookie on the client? Or, are you trying to create your own cookie in a Windows Forms app and sending that to a web server?
|
|
|
|
|
This[^] article may be a good place to start.
/ravi
|
|
|
|
|
yes, this is a ASP.NET and try to create a cookie on the project and it is on live within two weeks. thank you for ur answer cokkie wil be cookie to client.
thank you
|
|
|
|
|
The answer you seek is here[^].
|
|
|
|