Results 1 to 7 of 7

Thread: Just some clarification...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Just some clarification...

    When using the SaveSetting command the parameters are as follows:
    Code:
    SaveSetting (AppName[String], Section[String], Key[String], Setting[String])
    Since they are all strings (same as GetSetting parameters are), then how come something like this will work:
    Code:
    SaveSetting "Some Program", "Section 1", "5", "1"
    or
    Code:
    Check1.Value = GetSetting("Some Program", "Section 1", "5", "1")
    Without a type mismatch error happening.

    But if i have a Function with the following in it...
    Code:
    Public Function SomethingToDo (FirstParameter As String, SecondParameter As String) As String
    and pass integers into it, it will error with type mismatch.


    How does visual basic get around this? I don't want to use Variants because problems will come when i'm converting back to integers or what ever. So what would i put inside the function parameters to make it "fuzzy"?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Just some clarification...

    Because "5" is a string and 5 is an integer.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Just some clarification...

    Check1.Value = GetSetting("Some Program", "Section 1", "5", "1")

    What about this?, GetSetting returns a string, but Check1.Value is an integer.

    As long as it returns a number it won't error even though the types don't match up =S and how can i make this behavour happen in functions and subs that i create?
    Last edited by Slyke; May 30th, 2007 at 12:27 PM.

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Just some clarification...

    Check1.Value = CInt(GetSetting("Some Program", "Section 1", "5", "1"))

    Now it returns an Integer.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Just some clarification...

    Quote Originally Posted by Slyke
    how can i make this behavour happen in functions and subs that i create?
    If the values you pass can be coerced into the types required, VB does that silently. IOW, if your sub is
    Code:
    Private Sub Test(iTest As Integer)
    And you call it with
    Code:
    Test "5"
    It will work, since "5" will be coerced into 5. Or you could write your sub to expect a variant if that's what you really want to do - then test for what you need - If IsNumeric(vMyVariant), IsArray(aMyVariant), etc.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Just some clarification...

    Oh, ok, but sometimes it comes up with a type mismatch and when i check what is inside the string, it is only a number. This happens alot when i load a string from a file and try to put it into an integer even though it's only a number.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Just some clarification...

    Don't rely on implicit conversion.

    If a type mismatch occurs then explicit conversion is necessary, such as with CInt(). But as I mentioned earlier don't rely on implicit conversion... you should have converted the string to begin with since you already knew it was a string and that you were gonna assign it to a variable of another data type... you then wouldn't be having this error.

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