how to convert a string to a ushort

14.4k Views Asked by At

I have researched all of the questions that match my query and tried their solutions however none appear to work for me so I am asking how I convert a string to a ushort I have this set up all ready

public static string vid
{
    get { return "<vehicleid>"; }
}

I've tried to use this : short[] result = vid.Select(c => (ushort)c).ToArray(); but when I go to put the vid ushort into this bit

[RocketCommand("vehicle", "This command spawns you a vehicle!", "<vehicleid>", AllowedCaller.Player)]
public void ExecuteCommandVehicle(IRocketPlayer caller, string[] command)
{
    UnturnedPlayer spawner = (UnturnedPlayer)caller;
    spawner.GiveVehicle(vid);
}

I get the following error :

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'string' to 'ushort' arena v by FridgeBlaster C:\Users\Jonathan\Documents\Visual Studio 2015\Projects\arena v by FridgeBlaster\arena v by FridgeBlaster\vehicle.cs 71 Active

4

There are 4 best solutions below

2
On

Either you want to convert the string to a short:

Int16.Parse(...)

or

Int16.TryParse(...)

or you want to convert char to short by first converting string to array of chars:

vid.ToCharArray[..]
4
On

What you're looking for is ushort.TryParse or ushort.Parse methods.
I would suggest using this piece of code :

ushort[] result = vid.Where(i => ushort.TryParse(i, out short s)).Select(ushort.Parse);

Or if you do not use latest C# version :

ushort[] result = vid.Where(i => { ushort r = 0; return ushort.TryParse(i, out r); }).Select(ushort.Parse);

Okay so the problem ( as what your error says ) is that your GiveVehicle method accepts ushort as an argument and you're putting string type in it. Try doing something like this :

ushort val = 0;
if(ushort.TryParse(vid, out val))
{
    UnturnedPlayer spawner = (UnturnedPlayer)caller;
    spawner.GiveVehicle(val);
}

Based on this source which is responsible for calling method marked with RocketCommand attribute. Your <"vehicleid"> is/should be stored in a command array on first place. So to get this out and convert use something like this :

if(command.Length > 0)
{
    ushort val = 0;
    if(!string.IsNullOrWhiteSpace(command[0]) && ushort.TryParse(command[0], out val))
    {
        UnturnedPlayer spawner = (UnturnedPlayer)caller;
        spawner.GiveVehicle(val);
    }
}
5
On

Based on the line short[] result = vid.Select(c => (ushort)c).ToArray(); it looks like there might be multiple IDs in a single string (each character, I assume).

If that's the case, try something like this:

ushort[] result = vid.ToCharArray().Select(c => (ushort) c).ToArray();

If you've got several actual numbers in the string separated by a character (e.g. "13,15,18"), give this a shot:

ushort[] result = vid.Split(separator).Select(str => ushort.Parse(str)).ToArray();

For either of these options, make sure you include using directives for the System and System.Linq namespaces.


With discussion on this and other answers, it looks like command is the result of tokenizing "/vehicle <id>". In this case, the body of the ExecuteCommandVehicle(IRocketPlayer, string[]) method should be similar to this:

ushort result = 0;
if (ushort.TryParse(command[1], out result)) {
  UnturnedPlayer spawner = caller as UnturnedPlayer;
  caller?.GiveVehicle(result);
}
0
On

For a simple string to ushort conversion: UInt16.Parse(string)