|
I have a custom collection which wraps a List<T> . I want to provide a conversion operator for the consumers convenience to List<T> but would you recommend:
1. Just returning the innerList
public static explicit operator List<T>(MyList<T> myList)
{
return myList.innerList
}
2. Cloning by creating a new List<T> from the innerList
public static explicit operator List<T>(MyList<T> myList)
{
return new List<T>(myList);
} ?
Obviously the first is way faster, but then any changes after an explicit cast would also be reflected in the collection :undecided:
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
modified on Thursday, August 26, 2010 7:03 PM
|
|
|
|
|
You can just return a readonly version of the list, which is just as fast as returning the list unaltered (with the added benefit that the caller cannot modify the contents of the underlying list):
public static explicit operator System.Collections.ObjectModel.ReadOnlyCollection<int>(MyList<T> myList)
{
return myList.innerList.AsReadOnly();
}
|
|
|
|
|
Thanks, but I'm already implementing that in a method AsReadOnly() to provide consistant operation with the standard List<T> plus that would be an explicit cast to ReadOnlyCollection<T> and not List<T> .
I'm not concerned about keeping it read only, but more concerned about what a consumer of my collection would expect the behaviour to be. Maybe I just shouldn't provide the operator so there can't be any confusion?
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Yeah, if you're adding the operator just because you can, then I'd say there's probably not much value in doing so. If you want to provide some shorthand method so the user can get a list, you might want to provide a method, "CopyToList" or "GetList". If you are not worried about the list being readonly (i.e., the user can modify the list and it won't cause problems), then you could provide a public readonly property that returns the underlying list (perhaps call that property "InnerList").
If you do decide to go with an implicit conversion operator, I'd say it would be really unexpected for an operation as massive as a list copy to occur for a simple assignment to another reference type.
|
|
|
|
|
aspdotnetdev wrote: "CopyToList" or "GetList"
I might go with that - thanks
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
If I got a copy of the collection, I'd expect it to be consistent. I wouldn't expect it to change on me unexpectedly as a result of something manipulating the original list, so I'd clone the original list.
|
|
|
|
|
I was leaning that way too - thanks Pete
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi Dave,
I assume you had good reasons when you decided not to derive from List<T>.
I would not offer access to internal data, just like List<T> does not provide direct access to its internal array.
What I would consider, is adding a method ToList() which creates a List, probably by just cloning the internal List, and similar to List<T>.ToArray() .
Cloning a List isn't really expensive, it basically is an Array.Copy(); it also offers the advantage of independent enumeration, i.e. no foreach-cannot-modify-this problem.
|
|
|
|
|
Luc Pattyn wrote: I assume you had good reasons when you decided not to derive from List<t>.
Yeah, not overwhelmingly great but good enough.
Good points - thanks as always
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
You're welcome, as always.
|
|
|
|
|
Thanks guys, just what I was looking for
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi All,
I am using the standard Listview of Microsoft. Now i want the first column to be hidden. This column contains my key from which i access details. It is a technical id which the users do not care about and do not want to see. I can hide de column but not the subitem of the listview for it. I googled until my fingers burned, but cannot find a suitable solution. Any ideas??
Below is a part of the code from which i use.
With initialisation i run the ListViewAddColumns void. Then in code i loop trough my object and add each record to the listview. This all works fine!! but not i want to hide ListLines[0]. How can/should i do that.
if (objAll != null)
{
this.progressBar1.Maximum = objAll.Count;
foreach (statblCompany tblCompany in objAll)
{
progressBar1.Increment(1);
{
string[] ListLines = new string[10];
ListLines[0] = tblCompany.RE_ID.ToString();
ListLines[1] = tblCompany.RE_NUMBER.ToString();
ListLines[2] = tblCompany.RE_NAME;
ListLines[3] = tblCompany.RE_PHONE;
ListLines[4] = tblCompany.RE_VISITCITY;
ListLines[5] = tblCompany.RE_EMAIL;
ListLines[6] = xxGetIDDescription(tblCompany.RE_TYPE1);
ListViewAddItem(ListLines, 200);
}
}
listView1.Columns[0].Width = 0;
}
private void ListViewAddColumns(String ColumnHeaders, int ColumnWidth)
{
ColumnHeader glColumn = new ColumnHeader();
glColumn.Width = ColumnWidth;
glColumn.Text = ColumnHeaders;
glColumn.Width = ColumnWidth;
listView1.Columns.Add(glColumn);
}
private void ListViewAddItem(string[] ListLines, double ColumnWidth)
{
try
{
ListViewItem olItem = new ListViewItem();
ListViewItem.ListViewSubItem olSubItem0 = new ListViewItem.ListViewSubItem(olItem, ListLines[0]);
ListViewItem.ListViewSubItem olSubItem1 = new ListViewItem.ListViewSubItem(olItem, ListLines[1]);
ListViewItem.ListViewSubItem olSubItem2 = new ListViewItem.ListViewSubItem(olItem, ListLines[2]);
ListViewItem.ListViewSubItem olSubItem3 = new ListViewItem.ListViewSubItem(olItem, ListLines[3]);
ListViewItem.ListViewSubItem olSubItem4 = new ListViewItem.ListViewSubItem(olItem, ListLines[4]);
ListViewItem.ListViewSubItem olSubItem5 = new ListViewItem.ListViewSubItem(olItem, ListLines[5]);
ListViewItem.ListViewSubItem olSubItem6 = new ListViewItem.ListViewSubItem(olItem, ListLines[6]);
ListViewItem.ListViewSubItem olSubItem7 = new ListViewItem.ListViewSubItem(olItem, ListLines[7]);
ListViewItem.ListViewSubItem olSubItem8 = new ListViewItem.ListViewSubItem(olItem, ListLines[8]);
ListViewItem.ListViewSubItem olSubItem9 = new ListViewItem.ListViewSubItem(olItem, ListLines[9]);
olItem.SubItems.AddRange(new ListViewItem.ListViewSubItem[] {olSubItem0, olSubItem1, olSubItem2, olSubItem3, olSubItem4, olSubItem5, olSubItem6, olSubItem7, olSubItem8, olSubItem9});
listView1.Items.Add(olItem);
olItem = null;
olSubItem0 = null;
olSubItem1 = null;
olSubItem2 = null;
olSubItem3 = null;
olSubItem4 = null;
olSubItem5 = null;
olSubItem6 = null;
olSubItem7 = null;
olSubItem8 = null;
olSubItem9 = null;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}
Kind regards,
Marcel Vreuls
MarcelVreuls.com
<<A good idea can change your life>>
modified on Friday, August 27, 2010 6:19 AM
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:00am.
|
|
|
|
|
Hi,
That is exactly the solution i tried and worked. Thanks!!
Kind regards,
Marcel Vreuls
MarcelVreuls.com
<<A good idea can change your life>>
|
|
|
|
|
Hi friends
I'm looking for way of getting url from browser like Chrome or Opera etc.
To get this from IE I use [DllImport(user32.dll)] and methods FindWindow, FindWindowEx and SendMessage.
In case of Firefox very helpful is NDde.dll and DdeClient class.
But I don't know how to get this from other browsers.
I thought about checking cache files, but it doesn't work e.g. in portable version. It must by other way...a library or sth...
Any ideas?
|
|
|
|
|
A web browser is an app like any other, it can organize its GUI and its data structures any way the designer sees fit, and it may or may not offer an API. As a result there is no universal way to do what you want.
However I expect there is good probability you can for each of them identify a window and basically execute a GetWindowText; how you identify the right window and how you walk from the browser's main window to it, is for you to discover, and might even differ from one version to the next.
|
|
|
|
|
Yes, you're right, but not in the all. Because e.g. in Chrome is a problem: not every tab has "hidden" url. Please look to this image to have better understanding
part of Spy++ tree
So I think there must be another way to get this..but how?
|
|
|
|
|
The address bar in a browser is a window. Get the handle to the window and call GetWindowText (as pointed out by Luc). The window handle will always be the same for a given window in a given application. Each browser's address bar will have a different handle.
Looks like you're gonna have to do a little more legwork here.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Yes, I've the handle, but not this what I need
In IE analogical way works. I don't know why it catch a window where it's not a child with className Chrome_AutocompleteEditView...so window where is hidden url from active tab
public string GetChromeUrl()
{
IntPtr mainWindow = FindWindow("Chrome_WidgetWin_0", null);
IntPtr edit = FindWindowEx(mainWindow, IntPtr.Zero, "Chrome_AutocompleteEditView", null);
int length = SendMessage(edit, WM_GETTEXTLENGTH, 0, 0);
StringBuilder sb = new StringBuilder(length + 1);
SendMessage(edit, WM_GETTEXT, length + 1, sb);
return sb.ToString();
}
Any idea?
ok, it's easy, GetWindow() with second par = GW_HWNDNEXT
this what I'm looking for
modified on Wednesday, September 1, 2010 8:24 AM
|
|
|
|
|
Hello everybody,
I'm trying to develop a WebSpider that retrieves data related to "Sports" from twitter, Facebook and other sites/blogs, to display it all in my page.
I just need to retrieve the information displayed on that page, but the problem I’m facing is that when I’m reading the twitter page, there is only a JavaScript code that display these data and not the data itself
Is there any possibility to extract this information so I can log it in a Database for example?
Thank you for your usual help
|
|
|
|
|
Ajax, the bane of content thieves everywhere.
It is actually easier to get the data from these types of sites you do not have to filter the HTML to get to the data. What you need to do is find the underlying js method that returns the data and call it directly. If the given site allows you to do so. The presence of an RSS feed is a de-facto indication that aggregation is expected. In fact, you could even get the data from the RSS feed as well.
|
|
|
|
|
I handle situations like this by opening the page in an off-screen WebBrowser and reading the control's document's OuterHtml property after the document has finished loading. Ennis' solution will work (and is the preferred one) if an underlying AJAX call exists.
/ravi
|
|
|
|
|
Twitter has a well defined API for retrieving information in a variety of formats. Don't waste time trying to screen scrape when you can get the data easily using the RESTful API.
|
|
|
|
|
I'm building a little puzzle game that can have many (Trillions) of puzzles. Each puzzle can be simplified to a grid that is 6 by 6 where each grid can be one of 8 possible values. Right now I have each puzzle stored as a 36 char string (each char can be one of {"L", "R", "T", "B", "E", "P", "H", "V"} does anyone know a cool trick to take this type of string and compress it down so that it takes less space? Even shaving a few bytes off of it would be a huge help. (It'll make searching for it in the database a lot faster)
|
|
|
|
|
This is NOT string or text compression, it is a very dedicated problem.
8 possible values for a cell means a cell only needs 3 bits, hence a 6*6 board could be stored in 108 bits, i.e. 14 bytes (with 4 unused bits).
|
|
|
|
|