Click to See Complete Forum and Search --> : Convert.ToByte(MyString) concatination problems, is it converted then converted back?


Zirus Blackheart
July 22nd, 2008, 06:41 AM
Hi :-)

I'm having a very strange problem with Convert.ToByte(MyString)
Is it converted to the byte then converted back? as i'm getting concatination when I attempt to add to it again, and multiplycation ect is saying its still a string, even though I have done the convert and done some maths with it b4.

Here is some code to demostrate this weirdness...

This throws out the number 8, which is correct.
string myString = "10";

Console.WriteLine(Convert.ToByte(myString) - 2);

This just concatenates (as I understand it, C# has overloaded operators so this is correct but that still means there is no conversion happening)
string myString = "10";

Convert.ToByte(myString);
myString += 2;
Console.WriteLine(myString);

This says its still a string.
string myString = "10";

Convert.ToByte(myString);
myString -= 2;

As you can tell this is confusing the hell out of me, I want to convert my string to a byte so I can do some maths on it, I can do that, but it doesn't stay converted.... why?

Zirus Blackheart
July 22nd, 2008, 06:44 AM
The other annoying thing is MSDN doesn't saying anything at all about it not staying converted, that sites useless, it never helps heh

cjard
July 22nd, 2008, 07:01 AM
You seem to be under the impression that applying an operation to a variable changes the variable itself. THis is rarely the case, and it the case of strings (which are immutable) it is impossible

Convert.ToByte RETURNS A BYTE from the input string

at no point does it convert the actual input string itself into a byte. "string myString" declares a string variable that will always be a string variable

byte b = Convert.ToByte(myString)

byte b contains the byte conversion result of myString

-
an example of a routine that "changes" an input is:

string num = "2.0";
double d;
bool success = Double.TryParse(num, out d);

upon completion the variable "d" will contain the value 2.0. Note that d didnt "change" exactly; it was assigned a value where none existed before. This gets into the difference of pass-by-reference and pass-by-value but that is a topic better discussed when your coding knowledge is further along.

For now, just remember that methods such as Convert.ToByte do not work on or change the original variable and in particular, strings are immutable meaning once created they cannot be changed. Concatenating a new string onto the end of an old one causes an entirely new string to be created, and the contents of the old and the concat to be copied in. thus two strings a megabyte long each require 4 megs of memory to concatenate. 2 megs of memory are reserved for the strings, the first meg is copied in and then the second is copied in. Total memory 4 meg (1+1+2)

cjard
July 22nd, 2008, 07:05 AM
The other annoying thing is MSDN doesn't saying anything at all about it not staying converted, that sites useless, it never helps heh

Having read http://msdn.microsoft.com/en-us/library/y57wwkzk.aspx please tell me exactly how much more help you want microsoft to give you? The page describes what the method does, provides signatures code in 6 languages (including a usage guide in VB.NET-are VBN programmers being babysat or something??), describes the parameters and return values in detail, what could possibly go wrong and then to cap it all off provides 4 code samples, one for every language in .NET that you can copy and paste and use.

I accept that youre new, and you might not have wroked out how to use MSDN yet, but you will come to wish that all your API references were as authoritative, complete and comprehensive as MSDN.. It doesnt deserve the bashing you give it

Zirus Blackheart
July 22nd, 2008, 07:28 AM
Ahhh yeah of course, the orrigianl variable doesn't change, it's mearly converted in memory for the sake of that statement.
To store the conversion you need to create a variable of that type then initialise that varable with the return from the convert.

Thx :-)

On the msdn note, it still didn't tell me what i said above.
Hence why msdn suxx for learners :-)

cjard
July 22nd, 2008, 07:36 AM
It was a little confusing for me at the start, because it's so darn big. However, here's how i'd go about it now:

google for "convert.tobyte"

because MSDN is linked often, it's among the top searches

clicking the google link takes you to the overload-page for convert.tobyte, this page in itself isnt particularly useful but it isnt supposed to be. here you search for the overload youre using
specifically, you find Convert.ToByte(string) which is what youre looking for in this case

click the link for it and have a read, the code samples and stuff will help out

you can also, from the comfort of VS (if youre using it) on the VIOEW menu choose OBJECT BROWSER (Ctrl+W then J) and search for COnvert.ToByte there. it's a cut down version with no code samples, but contains enough esential info like return types to work out how it's used.

in code, you can position the cursor inside the ToByte text and then choose EDIT menu >> INTELLISENSE >>
PARAMETER INFO
or
QUICK INFO

for tooltip based help

foamy
July 22nd, 2008, 07:50 AM
also, you can simply position the curser on a method or other piece of code and hit F1 ;)

cjard
July 22nd, 2008, 07:32 PM
yeah, but that loads Online Help in my case, which is a) slow and b) full of seemingly irrelevant garbage

foamy
July 23rd, 2008, 01:02 AM
I find it quite helpful.. From what I've seen, it loads the same page as you would get by googling whatever your curser is positioned at...

Arjay
July 23rd, 2008, 01:09 AM
On the msdn note, it still didn't tell me what i said above.
Hence why msdn suxx for learners :-)You need to understand how to read a method prototype, but other than that Msdn gives you what you need:

public static byte ToByte(
string value
)

Notice how the method ToByte( ) converts the string and returns a byte?