question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ushort and short not supported

See original GitHub issue
class Options 
{
    [Option('p', "port", Required = false, HelpText = "port number")]
    public int Port { get;set; }
}

If i change the type of Port to ushort or short I get an error when starting the program:

“ERROR(S): Error setting value to option ‘p, port’: Check if Option or Value attribute values are set properly for the given type.”

Expected behavior:

  1. clear error message that the type is not supported
  2. support the types ushort and short

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

2reactions
moh-hassancommented, Jun 12, 2020

Default is object and need to be casted. Parser fail when trying to cast object value to short because Impossible cast from object to short More cast details

class Options
{
  [Option("port", Default = (ushort)5001)]
  public ushort Port { get; set; }

  [Option(Default=(short)100 )]
  public short ShortVal { get; set; }

  [Option( Default = (bool)true)]
  public bool? Vsible { get; set; }
	
  [Option( Default = (double)5001.4)]
  public double DoubleValue { get; set; }

  [Option( Default = (float)401.3)]
  public float FloatValue { get; set; }
}

For decimal values, it can not be casted like

//this is not allowed
//Compiler Error: CS0182 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type	
[Option( Default = (decimal)401.3)]
public decimal DecimalValue { get; set; }

decimal type is not a valid Attribute Parameter (c# language constraint).

2reactions
danielbisarcommented, May 13, 2020

Ok, I found it:

class Options
{
    [Option("port", Default = 5001)]
    public ushort Port { get; set; }
}

The error seems to be that I set Default to a value not of type string or uint. An explicit cast (ushort)5001 is necessary. It would be helpful if the error message on the terminal would not say:

Error setting value to option 'port': Check if Option or Value attribute values are set properly for the given type.

but more something like:

...: Expected type 'ushort', DefaultValue type 'int'

Which by the way could easily be solved, but I understand that since even dotnet needs a cast, it is more a question of if it should be done like this or not.

Maybe the parser could have an option: strict. If that is set to false the int would be converted (use Convert class from dotnet) otherwise the error message should appear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between a short and ushort in C#? - ...
short can be any value from -32768 to 32767 , whereas ushort can be from 0 to 65535 . They have the same...
Read more >
C# short and ushort Types
Understand the short and ushort number types. These types use 16 bits to represent a number.
Read more >
UShort Data Type - Visual Basic
Because UShort is an unsigned type, it cannot represent a negative number. If you use the unary minus ( - ) operator on...
Read more >
ushorttoint
This function converts short values to integer values by treating the input short as an "unsigned short," i.e. a short in the range...
Read more >
unsigned data types not supported
Im get System.NotSupportedException when i try map an ushort (or uint) column. Short sample: create table x( f number(5) )
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found