Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: [RESOLVED]implicit conversion from integer to string

  1. #1

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Resolved [RESOLVED]implicit conversion from integer to string

    Reviewing some code that says

    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed")
    Being new to programming, can someone explain to me in the most basic format what exactly this means, what does implicit conversion mean and implicit conversion from integer to string mean, what is it trying to tell me here?

    The code is some sample code I am using to be part of a larger app and the code is also from 2009 which shouldnt really make a difference.

    Thank you

    ** Going to Resolve this post, seems to be an open ended debate on the question, I have already sorted the question out, I paid someone to fix the issue instead, I was a tad frustrated by some of the posts, but thanks to all that replied, moving onto other issues.
    Last edited by jokerfool; May 24th, 2013 at 12:59 AM. Reason: Too many people complaining

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    Reviewing some code that says

    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed")
    Being new to programming, can someone explain to me in the most basic format what exactly this means, what does implicit conversion mean and implicit conversion from integer to string mean, what is it trying to tell me here?

    The code is some sample code I am using to be part of a larger app and the code is also from 2009 which shouldnt really make a difference.

    Thank you
    We cant help with the above code as we do not know what GetSettingInt is. None the less its asking for an integer value. Your parsing a string value.

    String "The Quick Brown Fox..."
    Integer 8 etc..

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    An implicit conversion occurs when you assign a value of one data type to a variable of a different, incompatible data type. For instance, this code implicitly converts a String to an Integer:
    Code:
    Dim int As Integer = "100"
    The value "100" is a String and the 'int' variable can only hold and Integer, so the system has to implicitly convert the String to an Integer and then assign the result to the variable. This code performs an explicitly conversion:
    Code:
    Dim int As Integer = CInt("100")
    CInt explicitly converts the String to an Integer and then assign the result to the variable. In that case the code tells the system exactly what to do rather than assuming that the system will work it out for itself.

    In your case, presumably the GetSettingInt method returns an Integer while the Text property of a TextBox (in fact, of any control) is type String. You need to explicitly convert the Integer to a String before assigning it to a String property:
    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    Looking for the GetSettingInt here is the function I think for that

    Code:
    Function GetSettingInt(ByVal name As String) As Integer
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return CInt(File.ReadAllText(subdirName & "s." & name & ".n"))
            Else
                Return Nothing
            End If
        End Function

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    Looking for the GetSettingInt here is the function I think for that

    Code:
    Function GetSettingInt(ByVal name As String) As Integer
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return CInt(File.ReadAllText(subdirName & "s." & name & ".n"))
            Else
                Return Nothing
            End If
        End Function
    ReadAllText returns a string value. Why are you casting this as an integer?


    http://msdn.microsoft.com/en-us/library/ms143368.aspx

    vb Code:
    1. Public Class Form1
    2.  
    3.     Function GetSettingInt(ByVal name As String) As String
    4.         Dim path As String = String.Format("{0}s.{1}.n", subdirname, name)
    5.         If IO.File.Exists(path) Then
    6.             Return IO.File.ReadAllText(path)
    7.         Else
    8.             Return Nothing
    9.         End If
    10.     End Function
    11.  
    12. End Class

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    17

    Re: implicit conversion from integer to string

    Which one is better and more preferred, implicit or explicit conversion.?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: implicit conversion from integer to string

    Explicit conversions... that's why the general recomendation is to set Option Explicit On by default.

    -tg
    * 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??? *

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by techgnome View Post
    Explicit conversions... that's why the general recomendation is to set Option Explicit On by default.

    -tg
    You mean Option Strict. Option Explicit has nothing to do with type conversions. Option Explicit determines whether explicit declaration of variables is required while Option Strict determines whether strict typing is enforced. Strict typing means no late binding and no implicit conversions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by ident View Post
    ReadAllText returns a string value. Why are you casting this as an integer?
    Presumably because the file is a text file but it contains a setting value that represents a number. When you add an Integer to My.Settings it gets stored in the config file. The config file is XML, which is structured text. As such, the value has to be read as text and converted to an Integer at some point. This is no different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    The code I am using is from 2008 to 2009 so the code back then may of served its purpose. The code shown is part of the settings, the main app itself works fine and I have no issues with loading etc, I just got a warning on the code for implicit conversion from integer to string. Here is the settings functions, this is where I am getting the warning.

    Code:
    Imports System.IO
    
    ''' <summary>
    ''' Because My.Setting namespace loose it's data after renaming the assembly file, or moving it, i made this.
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Public Module SettingsManager
    
        ' Dim subdirName As String = "opt\"
        Dim subdirName As String = MYDIR & "\opt\"
    
        'Sub Check()
        '    If FileIO.FileSystem.DirectoryExists("opt") = False Then
        '        FileIO.FileSystem.CreateDirectory("opt")
        '    End If
        'End Sub
    
        Sub Check()
            If FileIO.FileSystem.DirectoryExists(subdirName) = False Then
                FileIO.FileSystem.CreateDirectory(subdirName)
            End If
        End Sub
    
        Function GetSettingInt(ByVal name As String) As Integer
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return CInt(File.ReadAllText(subdirName & "s." & name & ".n"))
            Else
                Return Nothing
            End If
        End Function
    
        Function GetSettingStr(ByVal name As String) As String
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return File.ReadAllText(subdirName & "s." & name & ".n")
            Else
                Return Nothing
            End If
        End Function
    
        ''' <summary>
        ''' Return an empty list if invalid name supplied.
        ''' </summary>
        ''' <param name="name"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetSettingStrList(ByVal name As String) As List(Of String)
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    
                Return File.ReadAllLines(subdirName & "s." & name & ".n").ToList()
    
            Else
                Return New List(Of String)(0)
            End If
        End Function
    
        Function GetSettingBool(ByVal name As String) As Boolean
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    
                If File.ReadAllText(subdirName & "s." & name & ".n") = "True" Then
                    Return True
                Else
                    Return False
                End If
    
            Else
                Return Nothing
            End If
        End Function
    
        Sub SetSettingInt(ByVal name As String, ByVal data As Integer)
            Check()
            File.WriteAllText(subdirName & "s." & name & ".n", data)
        End Sub
    
        Sub SetSettingStr(ByVal name As String, ByVal str As String)
            Check()
            File.WriteAllText(subdirName & "s." & name & ".n", str)
        End Sub
    
        Sub SetSettingStrList(ByVal name As String, ByVal list As List(Of String))
            Check()
            File.WriteAllLines(subdirName & "s." & name & ".n", list.ToArray)
        End Sub
    
    
        Sub SetSettingBool(ByVal name As String, ByVal bool As Boolean)
            Check()
            File.WriteAllText(subdirName & "s." & name & ".n", bool.ToString)
        End Sub
    
    
        Sub SetSettingFont(ByVal name As String, ByVal font As Font)
            Check()
            'Dim d As New Font("font family", Size, FontStyle.Regular, GraphicsUnit.Pixel, font.GdiCharSet)
            Try
                Dim fontfamilyStr As String = font.FontFamily.Name
                Dim sizeStr As String = font.Size
                Dim styleStr As String = font.Style.ToString
                Dim gdiChatSet As String = CInt(font.GdiCharSet)
                Dim text As String = fontfamilyStr & "|" & sizeStr & "|" & styleStr & "|" & gdiChatSet
                File.WriteAllText(subdirName & "s." & name & ".n", text)
            Catch ex As Exception
            End Try
        End Sub
    
        Function getSettingFont(ByVal name As String) As Font
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Dim t As String() = File.ReadAllText(subdirName & "s." & name & ".n").Split(CChar("|"))
                Dim fontfamilyStr As String = t.ElementAt(0)
                Dim sizeStr As Single = CSng(t.ElementAt(1))
                Dim styleStr As FontStyle = getFontStyle(t.ElementAt(2))
                Dim gdiChatSet As Byte = CByte(t.ElementAt(3))
                Return New Font(fontfamilyStr, sizeStr, styleStr, GraphicsUnit.Point, gdiChatSet)
            Else
                Return Nothing
            End If
        End Function
    
        Private Function getFontStyle(ByVal s As String) As FontStyle
            Select Case s
                Case "Bold"
                    Return FontStyle.Bold
                Case "Italic"
                    Return FontStyle.Italic
                Case "Regular"
                    Return FontStyle.Regular
                Case "Strikeout"
                    Return FontStyle.Strikeout
                Case "Underline"
                    Return FontStyle.Underline
                Case Else
                    Return FontStyle.Regular
            End Select
        End Function
    
    End Module

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: implicit conversion from integer to string

    You're getting the warning since GetSettingInt returns an integer and you're assigning that to the Text property of a TextBox which is a String property. If you want to get rid of the warning use either of these two code lines:
    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()
    'or
    TextBox14.Text = CStr(GetSettingInt("TMaxUPSpeed"))
    It's always better to explicitly convert data types because you are in that case in control and the compiler doesn't have to make assumptions of what you're really trying to do. I would recommend to turn Option Strict on.
    Last edited by Joacim Andersson; May 18th, 2013 at 04:13 PM.

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    File.ReadAllText() returns a string though, so I'm assuming that the text you want to read is supposed to be cast to an integer. I might suggest Integer.TryParse() in that case, and return either a null string, or string.Empty on false. (Check that accordingly, and do whatever you need to do based on the returned value.) Or make this function take on the paradigm of the TryParse() method using a ByRef param, and returning a boolean based on TryParse() success or failure...

    Since the value is being used for text though, why cast to an integer in the first place only to convert back to a string? Seems inefficient, and especially if you aren't going to be using this Integer value for anything else. If you want validation Integer.TryParse() is the way to go...

    As mentioned above, unless you've changed your settings, Option Explicit is already on by default anyways, but Option Strict is the one that you want to have turned on. Implicit conversions are bad, and they also don't teach you to recognize what types you are dealing with if you always rely in implicit conversions being done for you. I once read an article that had this as a defense somehow for why VB.net is better than C#... Not sure how that works, but that's another story.

    Cheers
    Last edited by AceInfinity; May 18th, 2013 at 06:57 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by Joacim Andersson View Post
    You're getting the warning since GetSettingInt returns an integer and you're assigning that to the Text property of a TextBox which is a String property. If you want to get rid of the warning use either of these two code lines:
    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()
    'or
    TextBox14.Text = CStr(GetSettingInt("TMaxUPSpeed"))
    It's always better to explicitly convert data types because you are in that case in control and the compiler doesn't have to make assumptions of what you're really trying to do. I would recommend to turn Option Strict on.
    Which is exactly what I said in post #3, so one wonders why the OP is still asking the same question and hasn't acted on the advice provided there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by AceInfinity View Post
    Since the value is being used for text though, why cast to an integer in the first place only to convert back to a string? Seems inefficient, and especially if you aren't going to be using this Integer value for anything else.
    You seem to be missing the point that I made in post #9. It's clear from the method name that this data represents part of the application's preferences/options/settings. The data obviously represents a number so it is completely appropriate that it be returned as a number. When the data gets used it will be used as a number. This is presumably not the only place that this setting value will be used. This particular instance is for display purposes so, as with all data, it must be in String form for that purpose. Elsewhere though, it is likely that the data will be used in a comparison with another number, so it is essential that it be a number. Would you say that My.Settings should return everything as Strings? If not then why should these home-grown settings be returned as Strings?

    The possibility that the file doesn't contain a valid number has to be taken into account somewhere. If your suggestion to use TryParse is not taken up then any code that calls this method really should have it within an exception handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    Quote Originally Posted by jmcilhinney View Post
    You seem to be missing the point that I made in post #9. It's clear from the method name that this data represents part of the application's preferences/options/settings. The data obviously represents a number so it is completely appropriate that it be returned as a number. When the data gets used it will be used as a number. This is presumably not the only place that this setting value will be used. This particular instance is for display purposes so, as with all data, it must be in String form for that purpose. Elsewhere though, it is likely that the data will be used in a comparison with another number, so it is essential that it be a number. Would you say that My.Settings should return everything as Strings? If not then why should these home-grown settings be returned as Strings?

    The possibility that the file doesn't contain a valid number has to be taken into account somewhere. If your suggestion to use TryParse is not taken up then any code that calls this method really should have it within an exception handler.
    Quote Originally Posted by jmcilhinney View Post
    Presumably because the file is a text file but it contains a setting value that represents a number. When you add an Integer to My.Settings it gets stored in the config file. The config file is XML, which is structured text. As such, the value has to be read as text and converted to an Integer at some point. This is no different.
    You are probably right. And if he uses this value lots of times as an Integer, I suppose a simple redundant cast back to a string wouldn't matter too much. He wouldn't have to cast the value to an Integer a whole bunch of times though when he wants to use it if the return value stays as is. I didn't consider the bigger picture and was only looking at the smaller inefficiency here, because I have a habit of only reading the thread and judging based on the given information.

    Regards,
    Ace
    Last edited by AceInfinity; May 18th, 2013 at 09:03 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  16. #16
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: implicit conversion from integer to string

    why is everyone talking about conversions, the question was what does the line of code do

    TextBox14.Text = GetSettingInt("TMaxUPSpeed")
    and

    Function GetSettingInt(ByVal name As String) As Integer
    Check()
    If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    Return CInt(File.ReadAllText(subdirName & "s." & name & ".n"))
    Else
    Return Nothing
    End If
    End Function
    simply checks if a file named "TMaxUpSpeed" exists
    if it does it puts the contents of the file ( an intergeter probably) in a textbox, its wrongly coded and conversions dont need to be applied
    if it does not then the textbox is left empty

    it seems a little pointless(with the way its done) but thats what its doing

    its that simple.

    why are all u guys confusing jokerfool by following up with the conversions stuff?
    Last edited by GBeats; May 18th, 2013 at 10:42 PM.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  17. #17
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: implicit conversion from integer to string

    Function GetSettingInt(ByVal name As String) As Integer
    Check()
    If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    Return CInt(File.ReadAllText(subdirName & "s." & name & ".n"))
    Else
    Return Nothing
    End If
    End Function
    should be

    Function GetSettingInt(ByVal name As String) As string
    Check()
    If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    Return File.ReadAllText(subdirName & "s." & name & ".n")
    Else
    Return Nothing
    End If
    End Function
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: implicit conversion from integer to string

    Why? If the text in the file is an integer why shouldn't the function return an integer? It would be safer to use Integer.TryParse than CInt but the function is still returning an integer, and (unless the function causes an exception) the textbox will never be empty since the function is always returning an integer. Nothing for an integer is equal to 0.

    The warning is because the integer that is returned is added to a textbox and should first be converted as posted earlier.

  19. #19

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    So I just want to clarify that the code I am using is from 2008, please understand this, I didn't write the code, I am just asking for assistance. So thank you to the posts, I will take everything on board and make the changes. Thank you.

  20. #20
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    Quote Originally Posted by GBeats View Post
    should be

    Function GetSettingInt(ByVal name As String) As string
    Check()
    If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
    Return File.ReadAllText(subdirName & "s." & name & ".n")
    Else
    Return Nothing
    End If
    End Function
    If the return value was a string, then GetSettingInt wouldn't make sense would it? And this was part of the discussion if you look through the thread.

    The setting is intended to be an Integer from the very beginning, so why should it be a string? You're going to be using it perhaps more as an Integer than a String, so for this one place where you would use it as a string, why would you make the function fit this type? Then all the other places where you use it as an Integer, you have to manually cast it each time, instead of returning that value directly from the function.

    And no... He was asking what the implicit conversion bit meant on that line, not what the code itself does.

    Being new to programming, can someone explain to me in the most basic format what exactly this means, what does implicit conversion mean and implicit conversion from integer to string mean, what is it trying to tell me here?
    When he said "can someone explain to me in the most basic format what exactly this means," he was referencing the implicit conversion notice.

    My suggestion was Integer.TryParse(), so if you didn't have the return value as an Integer, then imagine implementing TryParse() everywhere you use this string, just to have a usable integer representation of the text in the file? Rethinking this over, it makes much more sense to stay an Integer.

    ~Ace
    Last edited by AceInfinity; May 19th, 2013 at 12:36 AM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  21. #21

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    OMG Integer or String, its all too much for me now, im so confused and it doesnt matter who explains it except for post #2, I think I understand.

    This post was never intended to be a debate, I had the warning when I go to build, it didnt stop the function of the app, I just wanted to know how to remove the warning, it seems I will have to edit the code in the settings file as explained above and in doing so it has removed the error from certain lines and now refers to other lines in the settings file e.g. SetSettingInt, but there are hundreds of lines related to that error, would this mean going in and changing all those lines from int to str? I am sorry if I am not the expert here I am only the beginner and trying to learn as much as I can before the end of the line arrives for me, but thank you too all.

  22. #22
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    Why is it too much? What are you confused about? Let us know and we can help.

    Post #3, is where the implicit conversion phenomena is best explained in my opinion: http://www.vbforums.com/showthread.p...=1#post4417245

    The return value from the function is an Integer, the Text property when set, expects a type of string, so when you assign that Integer to the placeholder of the Text property, a magical conversion is done for you (an implicit conversion from the original Integer, to the String value). To avoid the "magician" doing this magic for you so to speak, you need to cast that Integer value to a string first before you assign it to the Text property. That's how you would fix it.

    So as mentioned above, either CStr(), or the ToString() extension method.

    it seems I will have to edit the code in the settings file as explained above
    No, that function is fine... The only modification you might consider making to it, is adding Integer.TryParse() in there instead of CInt(). Everything else is fine, you shouldn't need to do anything with the function...
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  23. #23

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    Ahhhh lightbulb to head

    The textbox with the error is a text box that requires the user to enter a number

    Which I believe is the Integer.

    What is the meaning of implicit conversion, I dont get.

    So it wants to change from a number to a letter is that what this error is referring too?

  24. #24

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    AceInfinity I was referring too

    String "The Quick Brown Fox..."
    Integer 8 etc..

  25. #25
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    So it wants to change from a number to a letter is that what this error is referring too?
    By "letter", the proper term is String, but yes.

    The Text property is a type of String. Thus it doesn't accept an Integer. Some kind of conversion needs to happen to get the value to a type of String, before the Text property can hold that value. So if you don't have Option Strict On, and you don't do this conversion yourself (explicitly), an implicit conversion happens and this conversion is dealt with for you automatically (when a conversion can be made). This is bad practice though and should be avoided, so that's why you are getting the notification about it.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  26. #26

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    When I change to .ToString() it says:

    Expression does not produce a value

    Hence why I didnt change anything further

    Should I turn on Option Strict On and if so how?

  27. #27
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    When I change to .ToString() it says:

    Expression does not produce a value

    Hence why I didnt change anything further
    Did you do:
    Code:
    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()
    ???

    And to imagine this implicit conversion stuff. Lets say that the GetSettingInt() function returns a value of 9 in this case.

    If you write this in your code:
    Code:
    TextBox14.Text = 9
    You will get the same warning. But the implicit conversion takes that 9 and makes it "9" (a string). This would fix that warning:
    Code:
    TextBox14.Text = "9"
    Or in this case 9 is a placeholder for our function (we can't just put quotes around the function, otherwise it makes the function name our string, and not its return value), so the ToString() function is used:
    Code:
    TextBox14.Text = (9).ToString()
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  28. #28

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    I did add the .tostring but I got that error above. Instead I added this code as mentioned and the error went away

    Code:
    Function GetSettingInt(ByVal name As String) As String
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return File.ReadAllText(subdirName & "s." & name & ".n")
            Else
                Return Nothing
            End If
        End Function

  29. #29
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: implicit conversion from integer to string

    Hi,

    ok yes i read it again, i missed your post.

    and since it looks like its getting some sort of setting that's numerical then it makes sense to have the function like that.
    i wasnt thinking but practically its a good way to get "0" in the string if there is nothing there, since naturally integers start of as 0.

    point taken sry
    the function still is has potential problems with the way it is, and is relying of outside information being correct to prevent a crash.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  30. #30
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: implicit conversion from integer to string

    jokerfool please NOTE

    if the function is used somewhere else while code like my suggestion, you will need to do more conversions, but if its not then you will be fine.

    the error you have might also be caused by the file itself, you should check the data inside the file.

    obviously you cant use Cint("with this") because there are no numbers there, i dont know what it would return but it wouldnt be what you wanted that is sure.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  31. #31
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    I did add the .tostring but I got that error above. Instead I added this code as mentioned and the error went away

    Code:
    Function GetSettingInt(ByVal name As String) As String
            Check()
            If FileIO.FileSystem.FileExists(subdirName & "s." & name & ".n") = True Then
                Return File.ReadAllText(subdirName & "s." & name & ".n")
            Else
                Return Nothing
            End If
        End Function
    No... Please read through the posts again, it was suggested not to do this as a final consensus.

    Does this make sense?

    1. GetSettingInt
    2. Return value: String

    What was the error you were getting with ToString()?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  32. #32

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    When I change to .ToString() it says:

    Expression does not produce a value

  33. #33
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: implicit conversion from integer to string

    I have a hard time believing that you're using the function correctly.. Can you show us your exact code when it gives you that error and are you sure it is with that line?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  34. #34

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    Already did that, its posted above.

  35. #35
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    When I change to .ToString() it says:

    Expression does not produce a value
    Then you did it wrong. How about you show us what you did and then we can tell you what's wrong with it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  36. #36

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    Okay I guess I must of made a mistake somewhere, so is this correct? Cos the line is no longer highlighted saying that message, but will adding that .ToString() to the end of the line chance the functionality of the application?

    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()

  37. #37
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: implicit conversion from integer to string

    you should double check the file that you getting the info for, your applying a text extraction, maybe the file contains an object, or nothing at all.

    you said you already changed the function to return string, what did the textbox show when you did that?
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  38. #38

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    When I made that change you mentioned the error for that section disappeared, the error moved onto another section within the settings that I mentioned above

  39. #39
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: implicit conversion from integer to string

    Quote Originally Posted by jokerfool View Post
    Okay I guess I must of made a mistake somewhere, so is this correct? Cos the line is no longer highlighted saying that message, but will adding that .ToString() to the end of the line chance the functionality of the application?

    TextBox14.Text = GetSettingInt("TMaxUPSpeed").ToString()
    No it won't. Either way, the number is being retrieved from the file and displayed in the TextBox. The difference is that the ToString means that the Integer is converted to a String explicitly while previously it was being converted implicitly. It's being converted either way though. Do you actually know what implicit and explicit mean? That would probably be a good place to start.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  40. #40

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: implicit conversion from integer to string

    Yes after reading this page earlier

    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

Page 1 of 2 12 LastLast

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