|
Hi,
Are there any difference in performance between these codes:
<br />
MyClass _myClass = new MyClass();<br />
string _myString = "";<br />
_myString = _myClass.GetMyString();
and
<br />
MyClass _myClass = new MyClass();<br />
string _myString = _myClass.GetMyString();
In the first case, the string variable has been explicitly initialized.
While in the second case, it has been implicitly initialized.
Well, I understand that the difference in performance cannot be noticed.
But I'm just curious to learn about it.
Thank you very much.
-- modified at 22:15 Wednesday 20th June, 2007
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
|
|
|
|
|
If you replace "" with string.Empty, then I think performance will be equal.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
So you mean in this scenario, the second case (implicitly initialization) yields better performance?
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
|
|
|
|
|
yes, because the first creates an empty string, which is then discarded. string.Empty maps to an empty string that already exists, so doesn't cost anything.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I see. Thank you very much.
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
|
|
|
|
|
KiTsuNeKo wrote: Never wait for a chance to come, Believe in your own potential and go get it!
You can use Reflector[^] to investigate MSIL code...
|
|
|
|
|
I want to multiply 2 XML nodes and want to append the result in another XML node in C#.What will be the code.If there is already exist one XML file there.I want to do the coding in a Class File.
|
|
|
|
|
Select the nodes, grab the values, multiply them and stuff the result in.
What specific part are you stuck on ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
This is the code:
This code is actually calculating one specific node with a user defined rate value.But this code I made for one sample windows application.
But now I need only one class file where no user defined rate is there.
rate is also available in the XML file.
And these code is for one specific node.I need to do the calculations for each and every nodes where Attribute is mention as coverage.
public void CalculateCommissionAmount(string policyXML, string rate)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(policyXML);
XmlElement root = xmldoc.DocumentElement;
if (Convert.ToDouble(rate) < -5)
{
MessageBox.Show("Invalid Rate - Rate cannot be less than -5%.");
}
else if (Convert.ToDouble(rate) > 100)
{
MessageBox.Show("Invalid Rate - Rate cannot be more than 100%.");
}
else
{
try
{
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("PersonalAutoVehicle");
for(int i=0; i
|
|
|
|
|
saymajum wrote: .I need to do the calculations for each and every nodes where Attribute is mention as coverage.
OK, so you need to use SelectNodes where the attribute exists. XPath allows you to do this.
Use SelectNodes, not GetElementsByTagName. XPath is a lot more flexibile than that.
use double.TryParse, not convert.ToDouble, that will tell you without throwing an exception if the conversion is not possible.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thanks a lot I am trying in that way
|
|
|
|
|
How to loop in every nodes of a XML file through C# codes
|
|
|
|
|
Assuming it's in an xml document, you can use the SelectNodes method to get through a list of nodes, then you'd need to recurse from there.
However, I believe that an XmlReader just steps through them, that may be better/easier.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thanks,
But my scenario is:
There is a already existing Xml file.I have to take to nodes from there multiply it and result will come in the 3rd node that will be created dynamically.Coing in C#.
|
|
|
|
|
OK, so you don't want to iterate over every node, at all. You have specific paths you can pass into an XPath to get the values you want, you can use int.TryParse to get the numbers out, and then you need to add a node. XmlDocument is what you need. And probably a good read of the MSDN, it doesn't sound like you know much about using XML, yet.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Yes it's true,
After that I need loop also to itarate for each an every nodes.
|
|
|
|
|
OK, well, I answered that already.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
how do you attach a serial command to a button in c#?
i have a lutron rs232 controller for my lights, and i want to know how you attach an on/off command to it.
this is what i have
private void button1_Click(object sender, EventArgs e)
{
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
port.Write("SDL,1,0") //just an example, "Set Dimmer Level, zone 1, 0 for off"
port.Close();
}
now the (SDL,1,0) i've put a "/r/n" as well, but i get no response.
|
|
|
|
|
dirtylarry wrote: how do you attach a serial command to a button in c#?
I wouldn't. I'd put all the serial communications in a class (or classes) of its own. I'd then call that class from the user interface.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
Does it work when you send the same string using a terminal emulator
(such as Hyperterminal) with the same port settings ?
If not, anything is possible.
If so, maybe you need some delay between port.Open/Write/Close so the
control lines can settle (actually the lutron peripheral can react to their new
state).
|
|
|
|
|
in hyperterminal, it works fine, i need to set ascii characters, but it works fine in ht.
|
|
|
|
|
OK - I believe you're sending Unicode characters in this case, you'd need to build a byte [] or something to send ASCII ( or there are conversion classes in there somewhere )
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Although MSDN states "By default, SerialPort uses ASCIIEncoding ..."
|
|
|
|
|
I am trying to setup an array of forms so that each uses the same original form yet will get different data.
I used the following command
List<form1> forms = new List<form1>();
Is this correct and if so how does this get assigned a different number to forms.
Each time I instantiate a new form it shows as "forms = 0".
How do I solve this problem.
Thanks,
Michael
|
|
|
|
|
MAW30 wrote: Each time I instantiate a new form it shows as "forms = 0".
Sounds to me like you expected creating forms to magically add them to this list. forms.Add(new Form1()) is how to add a form to the list, and create it.
I don't like your idea, you'd do better to have an array of data sources.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|