|
Welcome
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
<br />
int x = 1;<br />
int z = ++x + x;<br />
Console.WriteLine (x.ToString () + " " + z.ToString ());<br />
<br />
int x = 1;<br />
int z1 = x + ++x;<br />
Console.WriteLine (x.ToString () + " " + z1.ToString ());<br />
Why z is 4, but z1 is 3?
In accordance with
Operators (C# Programming Guide)
The operator ++ has a higher precedence than the operator +. In both cases, ++ must execute at first.
There's not this problem in C++!
Oleg
-- modified at 10:04 Wednesday 21st March, 2007
|
|
|
|
|
++x is a prefix increment operation and its result is the value of x after it has been incremented. Therefore, the first calculation is 2 + 2 whereas the second is 1 + 2 .
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
It's all to do with prefix and postfix operators. If you break it down, you get the following:
++x is a prefix operator, so it performs the operation first - so it turns x from 1 into 2. Next, you add x again which is 2 by this stage.
In the case of z1, x is equal to 1 and then you add x back on, but increment it before you add it so the maths becomes 1 + 2.
Here's a one for you, what do you get with this:
int x2 = 1;
int z2 = x++ + x;
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
For ++x + x , because of the location of the pre-increment x is pre-incremented before it's value is substituted in the in the equation - so the expression effectively evaluates to z = 2 + 2 .
For x + ++x the value of x is substituted as the first operand in the equation before it is modified, then it is pre-incremented and substituted as the second operand in the expression. This makes the expression evaluate to z1 = 1 + 2 . At least that appears to be what's going on . . .
-- modified at 10:39 Wednesday 21st March, 2007
The compiler/run time environment/whatever has to substitute the value of x into both expressions twice. The first expression is evaluated as such:
z = ++x + x;
z = (x = x+1) + x;
z = 2 + x;
z = 2 + 2;
z = 4;
Whereas the second expression is evaluated like this:
z1 = x + ++x;
z1 = 1 + ++x;
z1 = 1 + (x = x+1);
z1 = 1 + 2;
z1 = 3;
In both expressions the pre-increment is performed before the addition, keeping the order of operation correct, but in the first expression the pre-increment happens before the value of x is substituted for the 2nd operand in the addition.
In C++ the compiler will examine the entire expression and evaluate any pre-increments before it does any value substitutions (apparently - this probably depends on what optimizations/type modifiers you specify), so it isn't that C++ doesn't have this problem, it's just that the code is being interpreted by different compilers and therefore will behave differently.
|
|
|
|
|
Varibrus wrote: Why z is 4, but z1 is 3?
+ is the arithmatic operator which works left to right
When int z = ++x + x; expression executes it increments the value of x and put it into stack(generally all the operations are done with the help of stack ). so stack will be 2 + value of x(i.e. 2) so result is 4.
When int z = x + ++x; expression executes stack will be 1 + incremented value of x(i.e. 2) so result is 3.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
Hello!
I got a main form, which can set to be always-on-top (this is achieved using the TopMost property of the main form).
The problem now is that when opening a modal dialog box, the dialog appears behind the main window.
The brute-force solution would be to disable the always-on-top mode while displaying the dialog. A better solution would be to tell the dialog somehow that its parent is the main form and therefore to appear in front of it. I tried setting the Owner , Parent and ParentForm properties of the dialog, but nothing worked (Owner: doesn't do anything, Parent: not allowed for top-level controls, ParentForm: read-only property).
Any solution? How can the dialog displayed in front of the main window?
Thanks and best regards
Dominik
|
|
|
|
|
Did you try also enabling the TopMost property of the dialog.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
No, didn't try that
Works great, thank you!
|
|
|
|
|
Glad to hear my guess was right
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi !
I would like to implement the Progressive Disclosure Controls in windows forms using c# . The Progressive Disclosure Control is the same when you use the add/remove programs from control panel the particular row gets expand when its clicked .
Can anyone give a ref. or sample code for this kind of implementation.
Thankyou
/S.Kumar
|
|
|
|
|
I was watching to see if anyone would reply. So I'll make an attempt. I've done some reading on this, and it's a bit confusing. Progressive Disclosure is a UI pattern, and on MSDN[^] they talk about Prog. Disc. Controls like they're just arrows.
Start with the MSDN article, if you haven't already. Other than that, I myself am interested in whether there really are standard/custom controls like f.e. the one used in Add/Remove programs.
Visual Studio can't evaluate this, can you?
public object moo<br />
{<br />
__get { return moo; }<br />
__set { moo = value; }<br />
}
|
|
|
|
|
ConstructorInfo ctor = LocatedType.GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance, System.Type.DefaultBinder, new Type[] { }, new ParameterModifier[] { });
I've just been creating some code to let me unit test private methods on classes. I created the code and then thought that maybe i might want to test a private constructor in the future so i started playing with the get constructor method.
The thing that surprised me was that i need to use BindingFlags.Instance rather than BindingFlags.Static to get the constructor. Does this strike anyone else as strange?
Russell
|
|
|
|
|
No. When you think about it, the constructor is not a static method.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
You're right. There after all specific static constructors. It just seemed that calling a ctor an instance method was going to start a chicken / egg problem.
Russ
|
|
|
|
|
Russell Jones wrote: It just seemed that calling a ctor an instance method was going to start a chicken / egg problem
I can answer that one as well. The egg came first. If you follow evolutionary theory here, then the egg must have come first because the chicken was the result of genetic adaptation/mutation, but the egg was there before.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
That's always been my argument.
This site has now been blacklisted in 5 states of the USA.
Russell
|
|
|
|
|
|
i have just started working on .net remoting . well, an event is raised on some method call in the remote object. But while i assign a handler to the remote object's event property a SecurityException is raised stating :
"Type System.DelegateSerializationHolder and the types derived from it (such as System.DelegateSerializationHolder) are not permitted to be deserialized at this security level."
|
|
|
|
|
Hi,
Just happened to see that same error yesterday...
check out this link http://www.codeproject.com/useritems/RemotingEvent.asp
Section solution 2.
When you implement a wrapper class which can be accessed from both client and server side you app should work
Greetings
Kaine
|
|
|
|
|
Hi.
This is because of the remoting security. Normally types that are in GAC-registered assemblies are allowed to be serialized and deserialized, unless they're derived from:
- System.Runtime.Remoting.ObjRef
- System.DelegateSerializationHolder
- System.Runtime.Remoting.IEnvoyInfo
- System.Runtime.Remoting.Lifetime.ISponsor
you have to set the typefilterlevel to "Full" in your channel-configuration.
for more information take a look at this: http://msdn2.microsoft.com/en-us/library/5dxse167(vs.80).aspx[^]
greets
m@u
|
|
|
|
|
Dear all,
Probably is the answer pretty obvious, but I have been struggling with this for the past 2 days already, and it's making me frustrated.
I got following XML file
<br />
<?xml version="1.0" encoding="ISO-8859-1"?><br />
<things><br />
<people><br />
<person id="1"><br />
<name>Bob</name><br />
<age>25</age><br />
<projects><br />
<project x="123" y="123">ABC</project> <br />
<project x="53" y="149">DEF</project> <br />
<project x="13" y="111">FOO</project><br />
<project x="463" y="435">BAR</project><br />
</projects><br />
</person><br />
<person id="2"><br />
<name>Steve</name><br />
<age>100</age><br />
</person><br />
<person id="3"><br />
<name>Maria</name><br />
<age>23</age><br />
<projects><br />
<project x="1" y="1">HOT</project> <br />
</projects><br />
</person><br />
</people><br />
<file><br />
<name>filename.ext</name><br />
</file><br />
</things><br />
How can I add in C# a new person, and acess the "people" node.. I even cut away so much code, that I ended up with following, just to print out what's in there, and it even fails:
<br />
doc = new XmlDocument();<br />
doc.Load(this.filename);<br />
XmlElement rootPeople = doc.GetElementById("people");<br />
XmlNodeList node = rootPeople.GetElementsByTagName("person");<br />
for (int i = 0; i < node.Count; i++)<br />
{<br />
Console.WriteLine(node[i].InnerXml);<br />
}<br />
I have been messing with Xmlnodelists, xmlelements, xmlwhatevers, but even after reading the first 2 pages of google results, it still doesn't seem to work..
Can somebody point me in the good direction?
Thanks,
mike
|
|
|
|
|
First of all, I suggest using the SelectNodes and SelectSingleNode methods instead of GetElementsByTagName since both use XPath expressions and therefore allow specifying/restricting the selection in more detail. You can for example select the (first) people node by writing
XmlNode peopleNode = doc.SelectSingleNode("//things/people");
To create a new person or more general new nodes in a XML document take a look at this Create New Nodes in the DOM[^].
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Thank you Stefan, works like a charm!!
Seems that, after reading your signature.. the universe is building improved versions of me
Thanks again.
|
|
|
|
|
I have a problem like Yanshof.
I have a class writed in C++ (I don't want to translate it), it is used to store and processing te data...but the interface of the program must be writed in C# (or VB).
It is possible to do this in a simply way (using Visual Studio 2003 .NET?) ?
How to create this solution that contains this classes in different lenguages.
Y/N ?
If yes, does anyone can show me the way, I need only a simple example (the classic "hello word" in mixed lenguage is enough)
Thanks a lot
Cheers,
Russell
|
|
|
|