|
Thanks a lot Scott,
carbon_golem wrote: Have you timed it yourself?
Nope I havent timed it yet, how do i do that?
I did look at the ildasm. Thanks a lot, it cleared a big stumble in the thought process.
Okay agreed that with a method call, there is an additional overhead for puting the instruction on the stack and the return.
However compare it to the scenario where the class A and class B objects are being created more than once in Class C, would it be more economical to invoke the constructors everytime and initialize the members, even if we dont need them to be initialized; or just call a method once and pass the object references to initialize them once?
The IL clearly showed how a simple initialization like string.Empty is costly to refer to the Empty member of the system.string. Why do it twice? why not do it just once in a method call and avoid the redundancy?
Agreed there is a cost to make the call to the constructor and the method, but what about the cost of redundancy by calling the class ctor but not using that object any further.
Pass the object to a method that initializes/constructs it when it is needed.
What say?
------------------------------------
Vision is the ability to see the invisible
|
|
|
|
|
Spykraft wrote: carbon_golem wrote:
Have you timed it yourself?
Nope I havent timed it yet, how do i do that?
Look at the Stopwatch class, create a bunch of instances in a tight loop and time it. divide by your for loop parameter.
Spykraft wrote: However compare it to the scenario where the class A and class B objects are being created more than once in Class C, would it be more economical to invoke the constructors everytime and initialize the members, even if we dont need them to be initialized; or just call a method once and pass the object references to initialize them once?
That depends... if you're using the class as purely an initializer and not a member, then yes it is more economical. Say you're using class A to initialize class B then throwing A aside, then that's better to use it that way. And that's leaning more toward the Factory Pattern.[^].
If you're keeping A as a member in C then in that situation, then you'll probably want to give C it's own copy. If you make A temp = new A(); and pass temp to every instance of C that is being created, then all C's copies of A point to the same object. Thus any changes to A have a profound effect on your program. If that's the situation, then consider a redesign. But for the sake of argument, that's the fastest solution because there's one initialization and method calls proportional to the number of C's you have.
Hope this helps...
Scott P
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Spykraft wrote: Duz it make sense now?
No, even less so.
|
|
|
|
|
I have a form with a datagridview and the following class to handle the data keypress.
What code is missing to use the class.
I do not want to put this as a control.
I am new at this so anyhelp would be appreciated.
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)e.Control;
dText.KeyPress += new KeyPressEventHandler(/////);
}
}//end form class
//********************************************************************
#region DataGridViewControlClass
public class DataGridViewEnter : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}
public new bool ProcessRightKey(Keys keyData)
{
int DGVfirstcolumn = 0;//First column shown
int DGVlastcolum = 5;//Last Column shown
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
//works on the current line
if ((base.CurrentCell.ColumnIndex == (DGVlastcolum)) && (base.CurrentCell.RowIndex == (base.RowCount - 1)))
{
try
{
((BindingSource)base.DataSource).EndEdit();
((BindingSource)base.DataSource).AddNew();
}
catch
{ } // do nothing
base.CurrentCell = base.Rows[base.RowCount - 1].Cells[DGVfirstcolumn];
return true;
}
//add a new line
if ((base.CurrentCell.ColumnIndex == (DGVlastcolum)) && (base.CurrentCell.RowIndex != base.NewRowIndex))
{
base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[DGVfirstcolumn];
return true;
}
return base.ProcessRightKey(keyData);
}
return base.ProcessRightKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
}
#endregion
}//end namespace
|
|
|
|
|
|
hi
i want to generate typed dataset, for this work, i need an article to explaining how to create typed dataset ?
thanks
|
|
|
|
|
Hello everyone,
I am learning from "Garbage Collector Basics and Performance Hints",
http://msdn2.microsoft.com/en-us/library/ms973837.aspx
Two questions,
1. In the pattern "Too Many Pointers", what means "they merely "slide" together to remove the wasted space"?
2. In the pattern "Too Many Object Writes", I do not quite understand the emunerator and collector pattern, could anyone provide some brief pseudo code please?
--------------------
If you are, for instance, doing simple operations in some kind of enumerator class, you might find that you need to move some of your key pointers from the main collection into the enumerator at every step. This is actually something you might want to avoid, because you effectively double the cost of copying those pointers around due to the write barrier and you might have to do it one or more times per loop on the enumerator.
--------------------
thanks in advance,
George
|
|
|
|
|
George_George wrote: what means "they merely "slide" together to remove the wasted space"?
Defragment
|
|
|
|
|
Thanks Colin,
How about your points of my question item 2?
regards,
George
|
|
|
|
|
George_George wrote: could anyone provide some brief pseudo code please?
The "for instance" is just talking about copying a reference from the collection into an enumerator of the collection as a method of implementing an enumerator. Anytime you copy a reference the GC system has to track it so it comes at an expense. There isn't much you can do in a GC environment that doesn't come at an expense.
These two reasons are of course complemented by the usual reasons for not doing too many writes in any kind of program. All things being equal, it's better to touch less of your memory (read or write, in fact)
led mike
|
|
|
|
|
Thanks led,
Two more comments,
1.
If this practice is bad, because of two many writes, what is the preferred implementation? Could you describe please (without using enumerator)?
2.
I have a stupid question, what means enumerator in C#? Which API? Could you show me some samples please?
(I understand container or collect like List<>, but I am confused about what means enumerator?)
regards,
George
|
|
|
|
|
|
Thanks led mike,
1.
Does the article mean using foreach is better or not according efficiency and effectiveness of GC?
2.
Any pros and cons compared with using foreach and using simple index to loop, like
for (int i = 0; i < count; i++)
{
}
regards,
George
|
|
|
|
|
|
Thanks led mike,
I like both links and I take some time to read through. I do not quite understand this sentence in the 1st link,
"It remains true that only 10% of the code needs optimization, and that algorithmic efficiency is often the driving factor."
What means "algorithmic efficiency" and "driving factor"?
regards,
George
|
|
|
|
|
George_George wrote: What means "algorithmic efficiency"
George, why to you keep asking us question for which there is already a perfectly acceptable and easily found[^] answer?
George_George wrote: What means .... "driving factor"?
That means every project has it's own factors related to optimization needs. They can be very different from project to project. For example the use model of a system typically provides an understanding of many of these factors.
led mike
|
|
|
|
|
Thanks led mike,
Sorry it is my bad English. Now it is clear now. Sorry I have another question about the 1st link,
"On questions such as these, there are two ways to find the answer. The first is to build the code and look at the generated IL (or the resulting assembly, remembering that you get debug code if you run in the debugger. Attach to a running process, and then you get the optimized native code), or to time the code with a stopwatch."
I think either in debug mode in Visual Studio or attach to a running process, we can only debug through either the source code or the native code, any ways to debug through IL code in debug/release mode?
regards,
George
|
|
|
|
|
hy everyone!
sorry for me asking again, but i am not sure how to do this:
i read from a file which is a outputtemplate (i have fields in there which have to be replaced by it's values).
then there are two line which describes the table e.g.
|tag1_01| |tag2_01| |tag3_01| |tag4_01| |tag5_01|
|tag6_01| |tag7_01| |tag8_01|
(the location of the tags and finally the values are important, because otherwise it won't appear to be a table
the _01 is used to help me replace the tags with it's values.
so lets say i do want to duplicate this line ten times to finally have 11 lines.
the replacement of the _01 to _02 etc. isn't the challange for me, but i wonder how to insert the new lines at the correct location. because as far as i know i am only able to insert it at an index position. well, in my case i would try to find the _01 entries by reading line after line in the file. when i find it i copy these two lines to a tempstringvariable. then loop for the desired number, replace _01 by _02 etc.
now the tricky part: i have to find out at which position the two lines end. well i guess this is indexof(tempstring) + tempstring.length + 2 (linefeed). and then insert the lines + linefeeds into there.
but isn't there an easier way like "insert line after this line"?
thanks for any further hint which help me realize this in a more easy way!
stephan.
|
|
|
|
|
I think you may have a knowledge representation issue here. You're representing the template with a single string, right? In that case, you've built a find-replace model where now you need an example-build model. Looks like you're going to either A) devise a very clever solution using your current system by changing the table template to a line template B) change your templating model to have better supporting code or C) get rid of the template system in favor of a custom formatter or custom StreamWriter.
Scott P
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Also, almost forgot, You could look into constructing using XML... XSL...
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Hello all...
when i enter a text in any WindowsForms Textbox with forward slash "/" the textbox reverse the text and make it wrong
e.g. if i enter 990 first then / then 130 the system will display it 130/990 note 990/130...
how can i solve that?
Thanks in advanced...
Thanks
|
|
|
|
|
What are you binding the textbox to?
|
|
|
|
|
Atually I'm developing a documents In-System and this is the document no.
I face this problem when I convert the textbox order to be Right To Left
Thanks
modified on Thursday, May 1, 2008 8:12 AM
|
|
|
|
|
when i am trying to insert i the patient detail in the BINARY SEARCH TREE it give me the following error "The best overload method for IBSTree.Insert(System.Icomparable) has some invalid arguments".
<br />
using System;<br />
using System.Collections;<br />
<br />
<br />
<br />
public interface IPatient<br />
{<br />
string FirstName { get; set; }<br />
<br />
<br />
}<br />
<br />
<br />
using System;<br />
using System.Collections;<br />
<br />
<br />
<br />
public class Patient: IPatient<br />
{<br />
private string fname;<br />
<br />
public string FirstName<br />
{<br />
get<br />
{<br />
return fname;<br />
}<br />
set<br />
{<br />
fname = value;<br />
}<br />
}<br />
public int CompareTo(IPatient obj)<br />
{<br />
Patient p1 = (Patient)obj; <br />
return fname.CompareTo(p1.fname);<br />
} <br />
}<br />
<br />
using System;<br />
using System.Collections;<br />
<br />
public class BTreeNode<br />
{<br />
private IComparable item;
private BTreeNode lchild;
private BTreeNode rchild;
<br />
public BTreeNode(IComparable item)<br />
{<br />
this.item = item;<br />
lchild = null;<br />
rchild = null;<br />
}<br />
<br />
public IComparable Item<br />
{<br />
get { return item; }<br />
set { item = value; }<br />
}<br />
<br />
public BTreeNode LChild<br />
{<br />
get { return lchild; }<br />
set { lchild = value; }<br />
}<br />
<br />
public BTreeNode RChild<br />
{<br />
get { return rchild; }<br />
set { rchild = value; }<br />
}<br />
}<br />
<br />
<br />
public class BSTree : IBSTree<br />
{<br />
private BTreeNode root;<br />
public BSTree()<br />
{<br />
root = null;<br />
}<br />
<br />
public bool IsEmpty()<br />
{<br />
return root == null;<br />
}<br />
<br />
public bool Search(IComparable item)<br />
{<br />
return Search(item, root);<br />
}<br />
<br />
private bool Search(IComparable item, BTreeNode r)<br />
{<br />
if (r != null)<br />
{<br />
if (item.CompareTo(r.Item) == 0)<br />
return true;<br />
else<br />
if (item.CompareTo(r.Item) < 0)<br />
return Search(item, r.LChild);<br />
else<br />
return Search(item, r.RChild);<br />
}<br />
else<br />
return false;<br />
}<br />
<br />
<br />
public IComparable PartialSearch(IComparable item)<br />
{<br />
return PartialSearch(item, root);<br />
}<br />
<br />
private IComparable PartialSearch(IComparable item, BTreeNode r)<br />
{<br />
if (r != null)<br />
{<br />
if (item.CompareTo(r.Item) == 0)<br />
return r.Item;<br />
else<br />
if (item.CompareTo(r.Item) < 0)<br />
return PartialSearch(item, r.LChild);<br />
else<br />
return PartialSearch(item, r.RChild);<br />
}<br />
else<br />
return null;<br />
}<br />
<br />
<br />
public void Insert(IComparable item)<br />
{<br />
if (root == null)<br />
{<br />
root = new BTreeNode(item);<br />
<br />
}<br />
else<br />
Insert(item, root);<br />
}<br />
<br />
private void Insert(IComparable item, BTreeNode ptr)<br />
{<br />
if (item.CompareTo(ptr.Item) < 0)<br />
{<br />
if (ptr.LChild == null)<br />
{<br />
ptr.LChild = new BTreeNode(item);<br />
<br />
}<br />
else<br />
Insert(item, ptr.LChild);<br />
}<br />
else<br />
{<br />
if (ptr.RChild == null)<br />
{<br />
ptr.RChild = new BTreeNode(item);<br />
<br />
}<br />
else<br />
Insert(item, ptr.RChild);<br />
}<br />
}<br />
<br />
public void Delete(IComparable item)<br />
{<br />
BTreeNode ptr = root;
BTreeNode parent = null;
while ((ptr != null) && (item.CompareTo(ptr.Item) != 0))<br />
{<br />
parent = ptr;<br />
if (item.CompareTo(ptr.Item) < 0)
ptr = ptr.LChild;<br />
else<br />
ptr = ptr.RChild;<br />
}<br />
<br />
if (ptr != null)
{<br />
if ((ptr.LChild != null) && (ptr.RChild != null))<br />
{<br />
if (ptr.LChild.RChild == null)
{<br />
ptr.Item = ptr.LChild.Item;<br />
ptr.LChild = ptr.LChild.LChild;<br />
}<br />
else<br />
{<br />
BTreeNode p = ptr.LChild;<br />
BTreeNode pp = ptr;
while (p.RChild != null)<br />
{<br />
pp = p;<br />
p = p.RChild;<br />
}<br />
ptr.Item = p.Item;<br />
pp.RChild = p.LChild;<br />
}<br />
}<br />
else
{<br />
BTreeNode c;<br />
if (ptr.LChild != null)<br />
c = ptr.LChild;<br />
else<br />
c = ptr.RChild;<br />
<br />
if (ptr == root)
root = c;<br />
else<br />
{<br />
if (ptr == parent.LChild)<br />
parent.LChild = c;<br />
else<br />
parent.RChild = c;<br />
}<br />
}<br />
<br />
}<br />
}<br />
<br />
public void PreOrderTraverse()<br />
{<br />
PreOrderTraverse(root);<br />
Console.WriteLine();<br />
}<br />
<br />
private void PreOrderTraverse(BTreeNode root)<br />
{<br />
if (root != null)<br />
{<br />
Console.Write(root.Item);<br />
PreOrderTraverse(root.LChild);<br />
PreOrderTraverse(root.RChild);<br />
}<br />
}<br />
<br />
public void InOrderTraverse()<br />
{<br />
InOrderTraverse(root);<br />
Console.WriteLine();<br />
}<br />
<br />
private void InOrderTraverse(BTreeNode root)<br />
{<br />
if (root != null)<br />
{<br />
InOrderTraverse(root.LChild);<br />
Console.WriteLine(root.Item);<br />
InOrderTraverse(root.RChild);<br />
}<br />
}<br />
<br />
public void PostOrderTraverse()<br />
{<br />
PostOrderTraverse(root);<br />
Console.WriteLine();<br />
}<br />
<br />
private void PostOrderTraverse(BTreeNode root)<br />
{<br />
if (root != null)<br />
{<br />
PostOrderTraverse(root.LChild);<br />
PostOrderTraverse(root.RChild);<br />
Console.Write(root.Item);<br />
}<br />
}<br />
<br />
public void Clear()<br />
{<br />
root = null;<br />
}<br />
<br />
public ArrayList AllItems()<br />
{<br />
ArrayList items = new ArrayList();<br />
AllItems(root, items);<br />
return items;<br />
}<br />
<br />
private void AllItems(BTreeNode root, ArrayList keys)<br />
{<br />
if (root != null)<br />
{<br />
AllItems(root.LChild, keys);<br />
keys.Add(root.Item);<br />
AllItems(root.RChild, keys);<br />
}<br />
}<br />
}<br />
<br />
using System;<br />
using System.Collections;<br />
<br />
namespace PatientName<br />
{<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
int choice;<br />
IPatient p1 = new Patient();<br />
IBSTree container = new BSTree();<br />
Console.WriteLine("Enter the first Name of the patient:");<br />
p1.FirstName = Console.ReadLine();<br />
container.Insert(p1);<br />
<br />
}<br />
}<br />
}<br />
<br />
<br />
|
|
|
|
|
Tip: Use the pre tag when posting blocks of code. It makes it readable.
As far as I can see you only have overloads of the Insert method that takes an IComparable parameter. The Patient class doesn't implement the IComparable interface. Even if it would, the variable p1 is an IPatient reference, so it can never be used as an IComparable reference even if the actual ojbect would implement the IComparable interface.
Make the Patint class implement the IComparable interface, and change the variable p1 from an IPatient reference to a Patient reference.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|