|
No, help them, but don't do it for them.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Any of you fine C# folks care to tackle this problem in an elegant way?
- we have a list of Nodes and we want to group them by parent
- we want to return the Foo attached to each Node, grouped by parent's Foo
Given that:
- each Node knows its parent Node, e.g. someNode.Parent
- the parent Node can be null
- each Node has data attached to it of type Foo, e.g. someNode.Foo
I want a function that takes Nodes and returns their Foos, grouped by parent. (e.g. something that returns Foos grouped by parent Foos)
Any takers?
|
|
|
|
|
Dictionary<ParentNode, List<Foo>> is what you want to return, I'd have thought ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I need it all grouped by Foo. In other words, something like Dictionary<Foo, List<Foo>>
|
|
|
|
|
Something like this:
Dictionary<Foo, List<Foo>> GroupFoos(List<Foo> list);
Dictionary<Foo, List<Foo>> dict = new Dictionary<Foo, List<Foo>();
foreach (Foo foo in list) {
List<Foo> group;
if (!dict.TryGetValue(foo.Parent, out group) {
group = new List<Foo>;
dict.Add(foo.Parent, group);
}
group.Add(foo);
}
return dict;
}
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
I'm afraid the Dictionary class doesn't allow for null keys.
|
|
|
|
|
Right... Then just check if the parent is null, and handle them separately:
Dictionary<foo, List<Foo>> GroupFoos(List<Foo> list) {
Dictionary<Foo, List<Foo>> dict = new Dictionary<Foo, List<Foo>>();
foreach (Foo foo in list) {
if (foo.Parent == null) {
if (!dict.ContainsKey(foo) {
dict.Add(foo, new List<Foo>());
}
} else
List<Foo> group;
if (!dict.TryGetValue(foo.Parent, out group) {
group = new List<Foo>;
dict.Add(foo.Parent, group);
}
group.Add(foo);
}
}
return dict;
}
Now you have the parent elements in the Keys collection of the dictionary.
Experience is the sum of all the mistakes you have done.
modified on Friday, December 21, 2007 1:27:17 AM
|
|
|
|
|
Keeping in mind you already have them in a structure which does this - so I would only do this if you actually have to pass a Dictionary< Foo, List< Foo>> to something. Anything internal can just look at the existing structure.
Sounds like the first thing you are missing is that given a Foo you can't lookup the Node it belongs to. Solve that first. If you can't add a Foo.parentNode member then you might have to keep around a Dictionary <foo, node=""> somewhere while you manipulate this structure.
Then its fairly trivial to get Foo's under a given Foo.
IEnumerable< Foo> FoosThatAreUnderAGivenFooWowBadName(Foo parent)<br />
{<br />
foreach(Node n in parent.parentNode.ChildNodes)<br />
yield return n.Foo;<br />
}
Then I guess if you really really want its trivial to pull that out into a Dictionary< Foo, List< Foo>> kind of structure, but I don't entirely recommend that unless theres a compelling reason to do so.
|
|
|
|
|
Mark Churchill wrote: Keeping in mind you already have them in a structure which does this - so I would only do this if you actually have to pass a Dictionary< Foo, List< Foo>> to something. Anything internal can just look at the existing structure.
I am passing the Foos to a part of the program that doesn't know about Nodes. It only knows about and cares about Foos.
Mark Churchill wrote: its trivial to pull that out into a Dictionary< Foo, List< Foo>> kind of structure
Dictionary<k,v> doesn't permit null values as keys, unfortunately.
|
|
|
|
|
Ok, so the part thats only knowing about Foos - what operations is it going to perform on the grouped Foos? Is this a seperate clump of data that will be passed out, or is it fine if the other section queries the existing structure?
Does it have to be serializable?
null keys make an elegant structure a pain - you could inherit Dictionary and keep a seperate list of unparented Foos... basically adding null keys to a dictionary. Bit of work though.
You could also attempt to provide a singleton "null" Foo - similar to DBNull.Value and use that exclusively for keying a Dictionary< Foo, List< Foo>> structure.
Elegance really is going to depend on what the consuming code wants to do with these Foos. :P
|
|
|
|
|
I have an application that needs to save some translated files.
So I did what any developer would and incorporated a SaveFileDialog into the project.
It is configured for CreatePrompt = true.
When I run the application and give it a filename inside a folder (that I have the rights to write files to) I click save and get a dialog box that basically says:
Hey, that file doesn't exist!
Then I only get a button to say Okay .. but no button to Create.
Has anyone else experienced this? I see examples EVERYWHERE that I should totally be able to provide a new file name and expect to get it created without the Dialog box from actually blocking me from creating a file!
Thanks,
Michael
|
|
|
|
|
theRealCondor wrote: Has anyone else experienced this?
Nope.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I am attempting to synchronize my system clock with an NTP server using SNTP. I have found a number of publicly available NTP servers hosted by NIST and affiliates here[^]. I also found a reference to the type of message I will receive from the server here[^]. However, I don't know how to get this response from the server. Does anyone have experience with something like this that he or she will share with me? Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I found the answer... you send a byte array with 48 empty bytes as a udp client, then listen with the same client for a response. The code is as follows, where m_ep is the IpEndpoint of a NTP server (port 123):
private static readonly IPEndPoint MY_IPEP = new IPEndPoint(IPAddress.Any, 0);
private void GetTime(object clientObj) {
UdpClient client = (UdpClient)clientObj;
byte[] result = client.Receive(ref m_ep);
...
}
private void button1_Click(object sender, EventArgs e) {
UdpClient client = new UdpClient();
client.Client.Bind(MY_IPEP);
new Thread(new ParameterizedThreadStart(GetTime)).Start(client);
byte[] message = new byte[48];
message[0] = 0x1B;
client.Send(message, 48, m_ep);
} Hope this helps someone.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I have an access db. Tables have a YES/NO check box as the "NEW ITEM" column, I want to count how many new items are in the db. and print it.
I am connected to the db, how can i count ?
DataAccess da = new DataAccess();
//Choose the table
string sql = "SELECT * FROM read WHERE New?=true ";
and wanna print it as:
You have" + " " + i.ToString()+ " " + "clients waiting";
thanks
|
|
|
|
|
To get a count using SQL you would do this
SELECT COUNT(*) FROM read WHERE New?=true
This will return a single scalar value
only two letters away from being an asset
|
|
|
|
|
|
Hi,
I hope someone can help me here. We have a 3rd party windows application that is written in .NET 2 and want to prohibit users from being able to use on of the features which seems like a simple print screen from a right click on a list of contacts. The problem is I am a fairly novice C# coder but it has become my problem!
I guess that I would need to somehow capture a win32 API onPrint() event but the problem is I have absoloutly no idea about how one would go abouts doing this. I think it would be a very important step in my learning if I could understand how I could create a custom event handle to a win32 API call.
Does anyone have any ideas on how I could do this or where I could find articles on this? I read one article but it went quite over the top of my head and did not seem to work on XP sp2 or vista machines.
Cheers,
Viv
|
|
|
|
|
You want to prevent printing from a third party app? Unless you have the code for this app and modify it, the only other way is to somehow hook into the app, which isn't easy even for experienced people, let alone a novice.
only two letters away from being an asset
|
|
|
|
|
I have different types of objects stored in a xml file. I am wondering what is the fastest/best way to read this file. I mean, should i go line by line or should i read as an object. If reading as an object reduces extract codes then can somebody provides me a simple example.
thanks for answering
|
|
|
|
|
You can use the XmlDocument object to read elements in an XML file.
<br />
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();<br />
<br />
xmlDocument.Load(filePath);<br />
<br />
|
|
|
|
|
There is no "best" way, but there are at least 3 different ways depending on the circumstance.
1) Use XmlDocument as described above. This allows you read/write access to the Xml file and is therefore generally the slowest.
2) Use an XmlTextReader gives you forward-only read access.
3) use XmlSerializer to turn an object graph into an object. This could well be the "fastest" but it would depend entirely on your object.
|
|
|
|
|
XLINQ? (XDocument )
|
|
|
|
|
If you have LINQ availale in your .net / vs version - use it! It takes a bit of getting used to, but so far I have found it to be far simpler than using XmlDocuments, readers, Xpath etc...
using System.Linq;
using System.Xml.Linq;
will change (for the better) the way you work with xml!
|
|
|
|
|
Hi guys,
I have a datagridview. i can delete all rows, bud not the last row, i mean i can't never delete wen there is only one row remaind.
any idea how to fix this?
thanx.
|
|
|
|