See return value in C# - c#

Consider the following piece of code:
As you can see we are on line 28. Is there any way to see the return value of the function at this point, without letting the code return to the caller function?
Foo.Bar() is a function call which generates a unique path (for example). So it's NOT constant.
Entering ?Foo.Bar() in the immidiate window doesn't work either, since that reevaluates the code:
?Foo.Bar()
"80857466"
?Foo.Bar()
"2146375101"
?Foo.Bar()
"1106609407"
?Foo.Bar()
"792759112"
In VB.NET it's possible by entering the function's name in the Watch, which will then threat it as a variable.
But in C# this is not possible, any other tips?
PS: rewriting is not an option.

Answer found here: VS get returned value in C# code?
It is actually visible. Debug + Other
Windows + Registers. Look at the value
of EAX (RAX in x64). The value of
simple integral types are returned in
the EAX register. Long in EDX:EAX.
Floating point in STx (XMM00 in x64).

Assuming you are using visual studio, you could use the Immediate window. If you type Foo.Bar(); in the Immediate window you will get the result you are after, if you don't want it to re-evaluate stick a break point in the appropriate spot IE either before it evaluates the first time or in Foo.Bar() itself.

Related

Setting CheckBox value to checked

Some context, I'm still rather new at c# but I've been updating c# code that someone else wrote that is incomplete. I've come across this code that seems to be giving me an error.
gblnEnableSSL = Convert.ToBoolean(Convert.ToInt32(Utils.GetSetting("Program", "Configuration", "SSL", "0")));
Utils.GetSetting("Program", "Configuration", "SSL", "0") returns a string "false" currently.
I've found that removing Convert.ToInt32() solves my problem. But, I was unconvinced that someone simply added Convert.ToInt32() that broke the code. I checked in the repo and was surprised to find that the previous code of the same line use to be:
gblnEnableSSL = Utils.GetSetting("Program", "Configuration", "SSL", "0").ToUpper() == "TRUE";
Which seemed to work as well. The checkbox checked value is set by using chkEnableSSL.Value = gblnEnableSSL;
My question is whether or not there is a difference between setting the value using a bool value or a string value. Also, is a difference between chkEnableSSL.value vs chkEnableSSL.Checked?
Edit: I'm not getting a compiler or run-time error with the previous code. It does not perform the code afterwards. For example, i have txtDataSource.Text = strDataSource; immediately after and it does not set the text unless i comment out gblnEnableSSL = ... or remove the Convert.ToInt32().
Edit:Convert.ToInt32() throws an exception but it did not break in VS because i didn't have "Common Language Runtime Exceptions" checked off.
It looks like GetSetting is returning a string. The first code example you gave is converting this string to an integer and then converting it to a boolean. The only way this would work is if the string values stored for that key are either 0 or 1. On your second code block it looks as if the values being used were not string values of 0 or 1 but actual string values of true or false. If the values stored in your config file are still in the format of true or false then this would be why your first line is breaking. You can't convert false or true strings to an int. It does look like someone just added something to break the code.
Also you don't mention what technology you are using. You could be using winforms, wpf, webforms, or MVC. I'm going to assume this is a web technology you are using because checbox.value is something I usually see in javascript. General consensus in setting checkbox values in javascript is by using the .prop() property which takes in a string as the property you want to change and a value as the second parameter. E.G.
$( "input" ).prop( "checked", true );
http://api.jquery.com/prop/
Moral of the story is remove the conversion to the int32 and use .prop() to set that checkbox :)

ReadInt32 vs. ReadUInt32

I was tinkering with IP packet 'parsers' when I noticed something odd.
When it came to parsing the IP addresses, in C#
private uint srcAddress;
// stuff
srcAddress = (uint)(binaryReader.ReadInt32());
does the trick, so you'd think this VB.Net equivallent
Private srcAddress As UInteger
'' stuff
srcAddress = CUInt(binaryReader.ReadInt32())
would do the trick too. It doesn't. This :
srcAddress = reader.ReadUInt32()
however will.
Took some time to discover, but what have I dicovered -- if anything ? Why is this ?
VB.NET, by default, does something that C# doesn't do by default. It always check for numerical overflow. And that will trigger in your code, IP addresses whose last bit is 1 instead of 0 will produce a negative number and that cannot be converted to UInteger. A data type that can only store positive 32-bit numbers.
C# has this option too, you'd have to explicitly use the checked keyword in your code. Or use the same option that VB.NET projects have turned on by default: Project + Properties, Build tab, Advanced, tick the "Check for arithmetic overflow/underflow" checkbox. The same option in VB.NET project is named "Remove integer overflow checks", off by default.
Do note how these defaults affected the syntax of the languages as well. In C# you have to write a cast to convert a value to an incompatible value type. Not necessary in VB.NET, the runtime check keeps you out of trouble. It is very bad kind of trouble to have, overflow can produce drastically bad results. Not in your case, that happens, an IP address really is an unsigned number.
Do keep the other quirk about IP-addresses in mind, sockets were first invented on Unix machines that were powered by LSD and big-endian processors. You must generally use IPAddress.NetworkToHostOrder() to get the address in the proper order. Which only has overloads that take a signed integer type as the argument. So using ReadInt32() is actually correct, assuming it is an IPv4 address, you pass that directly to NetworkToHostOrder(). No fear of overflow.

WebMatrix C# AppState (variable ?) always evaluating to false in if condition with string variables [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there a global variable (across web pages) I can use in C# WebMatrix, that actually works?
I have a rather odd (to me) problem. I have tested and tried many things and traced the problem down to one simple fact.
When I try to compare an AppState["uniqueName"] to a string value, it always evaluates to false. Here are a couple of snippets so you can see what I have:
On the first page:
AppState["gAdmitsMembership"] = AdmitsMembership;
On the second page:
if(AppState["gAdmitsMembership"]=="true"){checkBoxes[0]="checked='checked'";}else{checkBoxes[0]="";}
As you can probably tell the point is to keep a checkbox checked after submitting a form (and subsequently bringing the data back up in another form for possible editing)
Now, as I stated, I have tested many things and this is what I have done.
I plotted the value of AppState["gAdmitsMembership"] directly to a text input field (in the second page), in order to see what the actual value being tested was. It was indeed "true", however it still always equates to false (which I determined with another test by manipulating what happens with the "else".
Why is this failing the if condition?
It is important to note that nowhere in the code is this value "true" a boolean value. It is always the string "true". I have to do it this way because when I try:
if(AppState["gAdmitsMembership"]==true){checkBoxes[0]="checked='checked'";}else{checkBoxes[0]="";}
(same thing only with boolean true)
I get an error that says '== cannot be compared to objects or boolean' or something to that effect.
For this reason the compared string value must remain as "true" so that when it is added to the database it will be converted to the database as the necessary boolean value, true.
Anyway, any help that gets me through this will be quickly accepted and definitely appreciated. Thanks!
The syntax you use to assign a value to the AppState variable on the first page is not the right one.
Try with
App.gAdmitsMembership = AdmitsMembership;
as stated in this previous thread: Webmatrix 2: Storing static values.

C# int.TryParse("3",out tst) AND Convert.ToInt32("3") returning 0x00000003 instead of 3 what can it be?

All my code works fine until i did some changes that i cant track right now.
when i debugg my code i can see that the converting returning hex instead of number.
I have no idea why its happening or what can cause this.
any ideas please?
Thanks.
It's a VS feature, You can disable it:
You're looking at the value in a debugger. Integer values are not stored "in hex" or even "in decimal". 3 is stored in binary, but viewed based on your debugger settings.
You can disable hexadecimal viewing by right clicking in the Watch or Immediate view and deselecting Hexadecimal Display.

String type check variable not saving actual string

here are 2 screen shots when i try to debug my code in visual studio 2005
i want to save string value in variable check in variable a but it saves -1 not the actual string which is something like that "<username>admin</username>"
If you want to save the value of check in a, then your assignment is the wrong way round. Currently it's converting the value of a to a string, and storing the result in check.
Of course, you haven't specified the type of a - it may be converted to a string one way in the debugger, but the actual ToString method may be overridden to do something different.
If you actually meant to describe the question the other way round, you need to provide a lot more information - a short but complete program to demonstrate the problem would be ideal.
String assignment very definitely works in C# - so the chances are incredibly high that you're doing something strange in the code that you haven't shown us.

Categories

Resources