|
I don't think you can update the dictionary.
You must create a new one. Something like this:
Dictionary<int, string> original = new Dictionary<int, string>();
original.Add(2, "absolutely");
original.Add(1, "undoubtely");
var sortedKeys = (from rec in original
orderby rec.Key
select rec.Key).ToList();
Dictionary<int, string> second = new Dictionary<int, string>();
for (int i = 0; i < sortedKeys.Count; i++)
{
second.Add(sortedKeys[i], original[sortedKeys[i]]);
}
[Edit]A different option is to use the SortedDictionary [/Edit]
|
|
|
|
|
A dictionary has a Clear method.
Using this method, the Count property is set to 0, and references to other objects from elements of the collection are also released.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
Yeah but then how will he know wich value/object to insert at a given key?
|
|
|
|
|
and if i wont use the Clear method ?
there is no way to add new keyValuePair set to the dictionary ?
|
|
|
|
|
My bad. I thought you wanted to sort just on the key and select just the key.
Here's a working solution using the Clear() method.
var sortedKeys = (from rec in original
orderby rec.Key ascending, rec.Value ascending
select rec).ToList();
original.Clear();
for (int i = 0; i < sortedKeys.Count;i++ )
{
original.Add(sortedKeys[i].Key, sortedKeys[i].Value);
}
|
|
|
|
|
guess what,
when original.Clear() is executed, all the sorted set is gone...
|
|
|
|
|
Yes and NO.
If you don't use the .ToList() after the Linq then yes.
Else no.
|
|
|
|
|
you right, i forgot the .toList()
tanks...
|
|
|
|
|
Your question does not make sense. A dictionary does not have any defined order.
Yes you can get the pairs out of a dictionary and sort them. But if you insert them in a new/cleared dictionary, there's no guarantee they will keep that order.
The dictionary class is allowed to reorder elements as it sees fit (to make loopup faster). Often the order of insertion is preserved, but it's not guaranteed.
If you need your pairs to stay in sorted order, don't use a dictionary. Either use a List<KeyValuePair<...>>, or (if sorting by key) use a SortedDictionary<...>.
|
|
|
|
|
|
namespace MYConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int x = 3;
int y = 2;
x= (x==y++)?x+y:x-y;
A ObjA = new A();
ObjA.M1();
}
}
interface Inf1
{
void M1();
void M2();
}
interface Inf2
{
void M1();
}
class A: Inf1,Inf2
{
public void M1()
{
Console.Write("test");
}
public void M2()
{
Console.Write("test2");
}
}
}
From the above code ..how can i know from which interface the method M1() is overriding in Class A. Please let me know..if this is the case where tow interfaces contains same method..
G. Satish
|
|
|
|
|
See "Avoiding Name Ambiguity" section of this[^] article.
|
|
|
|
|
It is not overriding M1, it is implementing it for BOTH interfaces. If you wanted, you could also explicitly implement it for each interface (so that casting it as Inf1 and calling M1 will do something different than casting it as Inf2 and calling M1).
|
|
|
|
|
I want to write a use control named Notebook just as TableControl.
When desigining, user draws one user control, Notebook to one form and can use the property of the Notebook to edit the Pages in this Notebook. How do I to write this property design?
I write a class PageEditor inherited from System.Drawing.Design.UITypeEditor,
But I do not know which method needed to override? and what needed to do in the override method?
Can anyone help me?
|
|
|
|
|
<br />
public class Notebook : UserControl<br />
{<br />
#region Notebook<br />
public Notebook() : base()<br />
{<br />
InitializeComponent();<br />
<br />
this.SuspendLayout();<br />
Page newPage = new Page();<br />
newPage.Name = "newPage";<br />
this.Controls.Add(newPage);<br />
newPage.Dock = DockStyle.Fill;<br />
newPage.Visible = true;<br />
this.ResumeLayout(false);<br />
<br />
this.Dock = DockStyle.Fill;<br />
this.BackColor = Color.Yellow;<br />
}<br />
<br />
private System.ComponentModel.IContainer components = null;<br />
<br />
protected override void Dispose(bool disposing)<br />
{<br />
if (disposing && (components != null))<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}<br />
<br />
#region 组件设计器生成的代码<br />
<br />
private void InitializeComponent()<br />
{<br />
this.SuspendLayout();<br />
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />
this.Name = "Notebook";<br />
this.Size = new System.Drawing.Size(549, 217);<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
#endregion<br />
<br />
private int _pageIndex;<br />
[Category("Appearance")]<br />
[Description("当前页面索引号")]<br />
[DefaultValue(0)]<br />
public int PageIndex<br />
{<br />
get { return this._pageIndex; }<br />
set { this._pageIndex = value; }<br />
}<br />
<br />
private String _pages;<br />
[Category("Appearance")]<br />
[Description("所有页面")]<br />
[Editor("PageEditor", typeof(PageEditor))]<br />
public String Pages<br />
{<br />
get { return this._pages; }<br />
set { this._pages = value; }<br />
}<br />
<br />
}<br />
<br />
<br />
public class PageEditor : System.Drawing.Design.UITypeEditor<br />
{<br />
public PageEditor()<br />
: base()<br />
{<br />
<br />
}<br />
<br />
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)<br />
{<br />
return base.EditValue(context, provider, value);<br />
}<br />
}<br />
<br />
<br />
public class Page : System.Windows.Forms.Panel<br />
{<br />
public Page() :base()<br />
{<br />
InitializeComponent();<br />
<br />
<br />
this.BorderStyle = BorderStyle.Fixed3D;<br />
Label label1 = new Label();<br />
label1.Text = "fadfadfaef";<br />
this.Controls.Add(label1);<br />
}<br />
<br />
private System.ComponentModel.IContainer components = null;<br />
<br />
protected override void Dispose(bool disposing)<br />
{<br />
if (disposing && (components != null))<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}<br />
<br />
#region 组件设计器生成的代码<br />
<br />
private void InitializeComponent()<br />
{<br />
this.SuspendLayout();<br />
this.Name = "Page";<br />
this.Size = new System.Drawing.Size(553, 277);<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
}<br />
<br />
<br />
I want to write the Editor of Notebook's property, Pages. How can I make it? :( <br />
|
|
|
|
|
A couple of things:
1) If you paste code fragments, make them as small as possible - no one want to wade through 3 pages of someone else's code for the fun of it.
2) If you paste code fragments, enclose then in <pre>...</pre> blocks with the "Code Block" widget - it preserves the formatting and makes it easier to read. Thus it is more likely that someone will read it. It is the difference between:
{<br />
if (disposing && (components != null))<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}
and:
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
(That does not mean just put a code block around your existing post, use the "Preview" button to see what I mean.)
3) Ask a specific question, on where you are stuck, not "how can I make it?". That just shows that you have done no research, to tried in any way to help yourself.
4) Have a little patience, particularly when posting on a weekend. Since today is not a business day in Europe, America or Australia, most people are at home enjoying themselves, rather than hanging around here. Tomorrow, more people will be at work, and may see your question.
5) Trying to push an answer out of people just gets their backs up. No-one here gets paid to do this, so if you annoy them, they will not answer, or will become abusive.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Hi all,
In my application I'd like to write the command that was executed against a SQL database to a log file.
I can't just write the SqlCommand.CommandText property as that might look something like:
"INSERT INTO Users (UserName, Address, PhoneNo) VALUES (@UserName, @Address, @PhoneNo)"
which obviously is of no use to log. The values of the parameters has to be logged as well. I'd prefer not to have to make additional log entries for every parameter so what I've done now is something like this:
string CmdTxt = mySqlCommand.CommandText;
foreach (SqlParameter parm in mySqlCommand.Parameters)
{
CmdTxt = CmdTxt.Replace(parm.ParameterName, "'" + parm.Value.ToString().Replace("'", "''") + "'");
}
And then write CmdTxt to the log.
Does anyone know of a more elegant way of doing this? Is there not perhaps a different member of SqlCommand that I could use?
|
|
|
|
|
Hy,
I've done something quite similar, exept I wrapped it in a Class.
It has some overloads:
Utils.Logs.Write(string message);
Utils.Logs.Write(string message, Exception ex);
Utils.Logs.Write(string message, SqlCommand cmd);
Utils.Logs.Write(string message, Exception ex, SqlCommand cmd);
Utils.Logs.Write(Exception ex, SqlCommand cmd);
Utils.Logs.Write(SqlCommand cmd);
Utils.Logs.Write(Exception ex);
[Edit]Actually it doesn't take a SQLCommand, nor a OleDbCommand as a param, but
a DBCommand wich can be found in System.Data.Common . Both SqlCommand and OleDbCommand
inherit from DbCommand so in order to avoid many oveloades for each type
of Command I used the DbCommand . In my original post I made a mistake and typed
SqlCommand in the params list for the Logs.Write() method. I'll live it like that.
Here's the code without the try...catch...finally for the DbCommand:
public static void Write(System.Data.Common.DbCommand cmd)
{
StreamWriter sw = GetLogFile();
sw.WriteLine("SqlCommand: " + cmd.CommandText);
if ((cmd.Parameters == null) || (cmd.Parameters.Count == 0))
{
sw.WriteLine("Parameters: No Params supplied;");
sw.Flush();
sw.Close();
}
else
{
sw.WriteLine("Parameters:");
foreach (DbParameter pr in cmd.Parameters)
{
sw.WriteLine(String.Format("ParamName: {0} => Value: {1}",
pr.ParameterName, pr.Value.ToString());
}
sw.Flush();
sw.Close();
}
}
[/Edit]
And the method takes the Message, InnerException(if any), StackTrace from the Excpetion,
and the CommandText and Params values from the Command object.
And here's a possible ouput for the overload that takes all three params using Jet Engine for example:
***************START*****************
Date: 27.03.2010::12:30
DeveloperMessage: Error executing the 'procGetStations' query.
SqlCommand:
CommandText: "EXEC procGetStations";
Parameters: No Params supplied;
Exception: The Microsoft Jet database engine cannot find the input table or query 'procGetStations'. Make sure it exists and that its name is spelled correctly.
Sourse: Microsoft JET Database Engine
InnerException: None available
StackTrace: at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at DummyTest.StoredProcedures.StationProcedures.GetStations() in C:\DummyTest\StoredProcedures\Station.cs:line 27
***************END******************
modified on Saturday, March 27, 2010 7:16 AM
|
|
|
|
|
Well, you could base the log entry on what CommandType is specified (stored procedure, text, etc). If it's just text, you could simply save the command text. If it's a storedproc, you would then have to iterate through the parameters. If there's a way to intercept the actual command sent to the dastabase, you could extract the command and log that.
.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
|
|
|
|
|
I just cobble up a string with the commmand text, the parameter names, and their values -- with linefeeds included.
|
|
|
|
|
i am using a compress a file using System.IO.Compress.GZipStream in c# . But i needs compress a folder using is this same class .. how can compress folder . because GZipStream only accept a file Stream
|
|
|
|
|
Well a quick search of MSDN would have shown you exactly the code you need here[^].
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
The OP knows how to use GZipStream to compress a file. What the OP is asking is how to make an archive (or, "compress a folder") out of the compressed files. See the duplicate question just below for other answers relating to this.
|
|
|
|
|
Not quite, both his questions are asking for the procedure to compress a folder rather than a single file. The link I gave hime shows exactly that; the example's description states: "The following example shows how to use the GZipStream class to compress and decompress a directory of files.".
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
A matter of semantics, really. Though I imagine the OP wants all those files to be in the same archive. What the link you provided does is compress each file in a given folder (the output is a bunch of files with ".gz" extensions)... it does not compress the folder (which I take as meaning archive all the files in that folder into a single compressed file, which would likely contain information such as the original names of the uncompressed files).
|
|
|
|
|