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"?
Re: Just some clarification...
Because "5" is a string and 5 is an integer.
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?
Re: Just some clarification...
Check1.Value = CInt(GetSetting("Some Program", "Section 1", "5", "1"))
Now it returns an Integer.
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 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.
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.
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.