CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2008
    Posts
    26

    Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    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.
    Code:
                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)
    Code:
                string myString = "10";
    
                Convert.ToByte(myString);
                myString += 2;
                Console.WriteLine(myString);
    This says its still a string.
    Code:
                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?

  2. #2
    Join Date
    Mar 2008
    Posts
    26

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    The other annoying thing is MSDN doesn't saying anything at all about it not staying converted, that sites useless, it never helps heh

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    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)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    Quote Originally Posted by Zirus Blackheart
    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Mar 2008
    Posts
    26

    Thumbs up Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    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 :-)

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted b

    also, you can simply position the curser on a method or other piece of code and hit F1
    It's not a bug, it's a feature!

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    yeah, but that loads Online Help in my case, which is a) slow and b) full of seemingly irrelevant garbage
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted b

    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...
    It's not a bug, it's a feature!

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Convert.ToByte(MyString) concatination problems, is it converted then converted back?

    Quote Originally Posted by Zirus Blackheart
    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:

    Code:
    public static byte ToByte(
        string value
    )
    Notice how the method ToByte( ) converts the string and returns a byte?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured