|
If you don't know the type until runtime, why not make it IEnumerable<dynamic>. When you run this, it will appear that you have an IEnumerable of objects (as you have now), but with the proviso that because it's dynamic, it will honour any method calls you care to make on that object. I haven't tested the performance of this, but that might be worth a try for you.
|
|
|
|
|
Hmm... I need to return a strongly typed IEnumerable. If I return an IEnumerable<dynamic> off of the object[], it compiles, but I get a runtime cast error. I don't have control over the calling code. I'm just the library that serves it up.
|
|
|
|
|
SledgeHammer01 wrote: will actually be "much" slower
That may be, but unless you try it (and/or the Enumerator you show) you'll never know for sure -- you're only acting on hearsay or prejudice. Get a baseline reading, then try something else and get another reading, etc. You're assuming that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do?
Why are you accepting an object[] of them; can you correct this upstream?
And what actual types are involved? Value types? Structs? Small simple classes? Large complex classes?
SledgeHammer01 wrote: its lazy
Laziness may not help much unless the consumer doesn't need all the members.
And that cast to object looks redundant.
|
|
|
|
|
PIEBALDconsult wrote: That may be, but unless you try it (and/or the Enumerator you show) you'll never
know for sure -- you're only acting on hearsay or prejudice. Get a baseline
reading, then try something else and get another reading, etc. You're assuming
that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do? Why are you accepting an object[] of
them; can you correct this upstream? And what actual types are involved?
Value types? Structs? Small simple classes? Large complex classes?
Ok ... well, for the sake of the thread, I did... its not "much, much slower" as I thought it would be... but it is quite a bit slower (about 15%). 3900ms vs 4500ms
What I'm trying to do is given an array of some unknown (at compile time) object (which is why I store them as object[]), I need to convert said object[] into an IEnumerable T where T is known at compile time by the caller of my class.
I could potentially store a T[], but unfortunately, my class has non-T overloads. So its kind of like I need to support:
T GetTheObjects<T>();
IEnumerable<SomeClass> enum = GetTheObjects<SomeClass>();
object GetTheObjects(Type type);
IEnumerable<SomeClass> enum = (IEnumerable<SomeClass>)GetTheObjects(typeof(SomeClass));
If I only had to support the T overloads, I could avoid boxing / unboxing...
PIEBALDconsult wrote: And that cast to object looks redundant.
Agreed. That's how Reflector shows it. I tried c&p'ing that static method into my dll and removing the extra cast, but it didn't affect performance.
|
|
|
|
|
I'm new to C# and am working on first project.
I have a WINFORM that displays a treenode and a datagridview. I want selected items from the treenode to go to the datagridview. If I select the parent node of treenode all children go to datagridview but if I just expand the parent and select one of the children, nothing goes to datagrid. (selection of a treenode fires a method to get attributes of the item from sql table and it is the attributes that go to the grid + node value)
When parent is selected 'if (e.Node.Nodes.Count > 0)' is true. When parent isn't selected but child is 'if (e.Node.Nodes.Count > 0)' is false.
So my question is what code do I need to find child nodes that are checked/unchecked?
Once I get correct code to find child node check, what code moves it to datagrid or delete from datagrid if child is unchecked?
private void getChildNodesToGrid()
{
DataTable dt = getFieldsTable();
dgvColumns.DataSource = dt;
getAttributeSIDs();
}
private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
{
getFileAndColumns();
if (e.Node.Nodes.Count > 0)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
e.Node.Expand();
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Nodes.Count.Equals(0))
tn.Checked = e.Node.Checked;
}
getChildNodesToGrid();
}
|
|
|
|
|
RESOLVED:
Added an 'else' to the AfterCheck so it simply calls getChildNodesToGrid()
I then updated private DataTable getFieldsTable() to add a counter and condition 'if (fileNode.Nodes[cnt].Checked)'.
Don't know if this is proper programming but seems to work.
private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
{
getFileAndColumns();
if (e.Node.Nodes.Count > 0)
{
e.Node.Expand();
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Nodes.Count.Equals(0))
tn.Checked = e.Node.Checked;
}
getChildNodesToGrid();
}
else
{
e.Node.Expand();
if (e.Node.Checked)
{
getChildNodesToGrid();
}
}
private DataTable getFieldsTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ColumnName");
dt.Columns.Add("FMFieldName");
dt.Columns.Add("FMFieldNumber");
dt.Columns.Add("FMFileNumber");
dt.Columns.Add("FMFieldType");
dt.Columns.Add("ResolvedValue");
dt.Columns.Add("PointsToFileNumber");
TreeNode fileNode = tvFileMan.SelectedNode;
int cnt = 0;
foreach (TreeNode tn in fileNode.Nodes)
{
if (tn.Nodes.Count == 0)
{
if (fileNode.Nodes[cnt].Checked)
{
DataRow dr = dt.NewRow();
dr["FMFieldName"] = tn.Text.Substring(tn.Text.IndexOf(" - ") + 4);
dr["FMFieldNumber"] = tn.Tag.ToString();
dr["FMFileNumber"] = tn.Parent.Tag.ToString();
dr["ColumnName"] = suggestName(tn.Text.Substring(tn.Text.IndexOf(" - ") + 4));
if (dr["PointsToFileNumber"].ToString().Length > 0)
{
dr["ColumnName"] = suggestName(tn.Text.Substring(tn.Text.IndexOf(" - ") + 4) + "txt");
}
dt.Rows.Add(dr);
}
cnt++;
}
}
return dt;
}
private void getFileAndColumns()
{
label4.Visible = false;
label5.Visible = false;
btAllFields.Visible = false;
cbComputed.Checked = false;
TreeNode node = tvFileMan.SelectedNode;
if (node == null) return;
if (node.Index == 0) return;
if (node.Nodes.Count > 0)
{
tbFileNumber.Text = node.Tag.ToString();
tbFileName.Text = node.Text.Substring(node.Text.IndexOf(" - ") + 4);
tbTableName.Text = "xxxx." + (suggestName(tbFileName.Text) + "F" + tbFileNumber.Text.Replace('.', 'x'));
label4.Text = "To select all fields push button to the right. \nOtherwise double-click fields to add them one at a time.";
label4.Visible = true;
btAllFields.Visible = true;
dgvColumns.DataSource = null;
dgvPKIENS.DataSource = getPKIENSTable();
getPKIENSIDs();
}
else
{
}
}
|
|
|
|
|
I want to create a simple chat app. I would like to host it on my server and allow one-to-many people to chat.
I'd like to do it myself as a learning experience. Can anyone point me in the right direction?
Thank you
If it's not broken, fix it until it is
|
|
|
|
|
Lots of examples here[^].
/ravi
|
|
|
|
|
LOL - Never thought to look here
DOH!
If it's not broken, fix it until it is
|
|
|
|
|
What kind of app are you looking to build - web or desktop? I favor desktop, which relieves one of having to deal with the intricacies of building an async web UI, but your needs may vary.
/ravi
|
|
|
|
|
I'd build the UI with WPF and host the server on my server.
If it's not broken, fix it until it is
|
|
|
|
|
You know, I'm fairly good with MVC 4 Web Services. Any reason I shouldn't use that?
If it's not broken, fix it until it is
|
|
|
|
|
No, I think that's a good idea. If your server talks JSON, you could also use it with native mobile clients.
/ravi
|
|
|
|
|
Agreed. And it should be fairly simple.
If it's not broken, fix it until it is
|
|
|
|
|
Have you considered using a Jabber server instead of writing one from scratch?
/ravi
|
|
|
|
|
Kevin Marois wrote: I'm fairly good with MVC 4 Web Services
Then it might be a good opportunity to teach rather than learn.
|
|
|
|
|
The part I'm not sure about is getting notifications in the client from the Server.
Ever done this?
If it's not broken, fix it until it is
|
|
|
|
|
Details. Details. Don't bother me with details.
(I don't even know what MVC 4 is.)
|
|
|
|
|
Several years ago I wrote one that was just WinForms-to-SQL-Server, then I started working on a WCF service for it (again, it was quite a while ago), but since then it has stagnated. It's something I'd like to finish for an article.
Calling it a "chat app" isn't terribly accurate, as it is something between that and a simple e-mail system. It supports message threads (like forum posts here on CP) and delayed delivery for recipient(s) who are offline.
Kevin Marois wrote: as a learning experience
Should be a good way to learn the communication framework du jour and allow clients to be created for a number of devices.
|
|
|
|
|
I'm thinking I could create a Web API that maintains a list of Conversations. Each Conversation would have a unique Id and a list of Participants.
The only problem I see is the API notifying clients that they have a message. I'm guessing a callback of some sort would do the trick.
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: Each Conversation would have a unique Id and a list of Participants.
I put it in a database. Each message has its own ID, the ID of its parent, and the ID of the first message in the thread. Each message also has its own set of recipients (many-to-many relationship between Message and User). The client could then display each message as it arrives, or show the entire thread in a TreeView.
Kevin Marois wrote: API notifying clients
"Push" sucks; always has, always will. I always use polling; it allows the client/user to control frequency or even disable polling altogether. I also put in a "get messages now" feature. The server should only handle requests as they are made and not try to keep track of all the possible connections or it won't scale well. You probably also don't want to waste cycles re-trying broadcasts to clients that have lost connectivity. If no one is asking for data, don't try to send it; just wait.
|
|
|
|
|
Ok but with polling the client would have to make queries every 2 or 3 seconds to get close to real time
If it's not broken, fix it until it is
|
|
|
|
|
Yes, only as frequently as the user wants.
|
|
|
|
|
SignalR will be a better component for a chat application. I done a real-time time application with SignalR.. Its very interesting
|
|
|
|
|
Except is seems to be designed around ASP.Net. My usage will NOT be asp.Net
If it's not broken, fix it until it is
|
|
|
|