Results 1 to 6 of 6

Thread: Integer to string conversion

  1. #1

    Thread Starter
    Hyperactive Member divined's Avatar
    Join Date
    Aug 2004
    Location
    Thessaloniki, Greece
    Posts
    447

    Integer to string conversion

    Hello everybody

    What`s the easiest way to convert an Integer to a String. I was thinking of creating a variant variable and using it like shown below :

    Dim int1 As Variant
    Dim strText As String

    int1 = 2
    strText = "Some text..." & int1.

    But this does not work. It crashes with a type validation error. Can such code work or do I need to use some integerTostring conversion function?

    thx, in advance

    George Papadopoulos

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    VB Code:
    1. Dim int1 As Variant
    2. Dim strText As String
    3.  
    4. strText = CStr(int1)

  3. #3
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    Lose the Variant if it's an integer declare it as such.

  4. #4
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    if you are are converting a Variant type to a string the use the

    Cstr$ method that Deepak Sakpal shows but if you know that its a numeric you can use the str$() function.

    Point of note if you miss the trailing $ from a function it will not convert to a string but to a varaiant and then VB will make the conversion from variant to sting for you thus is less effienct so always use the $ there are a few functions to use the $

    mid$
    left$
    Right$
    ucase$
    LCase$

    Just remeber if you want to return a string from a built-in function alwasy place $ at the end.

    Hope this helped!!!!

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Dim int1 As Variant
    Dim strText As String

    int1 = 2
    strText = "Some text..." & int1
    This code will not produce an error. Are you using .NET?

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Well, if divined is using .NET, then Variant would throw an error right off the bat as it doesn't exist in .NET.

    I'm going to guess that the code sample is a simplified representation of the real problem and that it isn't the actual code. Moreover, the variable is probably getting set to some value other than a numerical 2 and it's that value that is causing the error. Prime example why variants should be outlawed.

    TG

    PS: Bombdrop - CStr$ doesn't exist as a function in VB. Since by it's very nature, CStr returns a string, if you put $ on the end of it VB takes it right off.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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