int float check

Is there a way to check if a data type is what it is suppose to be.

(eg) If the user typed in some string characters in a textField that was parsing to int down the line, throws a incorrect format error.

So is there a way to do something like this

var myString : String;

if(parseInt(myString)==int) {
  // Let me know
}

In C# it would be like this

string inputText = "12345";

int value;
if (int.TryParse(inputText, out value)) // or float.TryParse, or double.TryParse etc
{
    Debug.Log("The value is " + value);
}
else
{
    Debug.Log("Not a valid integer");
}

But since UnityScript doesn’t support “ref” and “out” parameters, I guess, you have to use exceptions to control the code flow:

function IsValidInteger(var inputText: String) : bool
{
    try
    {
        int.Parse(inputText);
        return true;
    }
    catch(error)
    {
        return false;
    }
}

If they can enter a float… then why would you not use a float?

Shouldn’t you be deciding what data you need?

Thanks for the replies, and examples. The user in this case would be the dev. I have create a local database to .txt and a user interface to play with where you would be able to see all data types in runtime. So even though the dev would know ahead of time not to enter a string in the parsed float or int text fields. I wanted to learn something new and eliminate the error as a final polishing maneuver lol. In the game, the db will be altered by script and there wont be the concern for a mix-up in data exchange. Since I have a few customers lined up already I’d like to leave little room for error for those devs that never read the documentation. :wink:

Oops,

So, you can use int.TryParse(…) in UnityScript too.

Thanks for the help.

Here is my working example

// Needed vars
var info : String;
var outFloat : float;
var dataSetFlo : String;
// Example
if(float.TryParse(dataSetFlo, outFloat)) { 
  info = " Float Was Excepted";
}
else { 
  info = " That Was Not a Float Value";
}