dotnet / dotnet/command-line-api

Inconsitency with error CS0030: Cannot convert type

Open
#2,251 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Hello Folks,

I am having trouble understanding why my code fails to compile with the error:

`Usage.cs(127,32): error CS0029: Cannot implicitly convert type 'System.CommandLine.Option' to 'int?'`

The thing is I have another variable of the same type defined just above the line that produces the error, and it does not error out. Here's the code in question edited for brevity:

I Define the options:

`var dpiOption = new Option (new[] { "--dpi" }, "Set image dots per inch.");
var resizeOption = new Option (new[] { "--resize" }, "Resize Image.");`

I want to save the values in the struct:

`public struct CmdArgs
{
public int? dpi;
public int? resize;
}
public CmdArgs() {
dpi = 0;
resize = 0;
}`

When I complie:

line 126: `cmdArgs.dpi = dpiOption; // This line does not give an error`
line 127: `cmdArgs.resize = resizeOption; // This line does give the error`

I have tried casting without any luck:

`cmdArgs.resize = (int)resizeOption;
Usage.cs(127,32): error CS0030: Cannot convert type 'System.CommandLine.Option' to 'int'`

`cmdArgs.resize = (int?)resizeOption;
Usage.cs(127,32): error CS0030: Cannot convert type 'System.CommandLine.Option' to 'int?'`

Any help I can get to help me understand this issue would be greatly appreciated. I am working in a net6.0 environment if that makes any difference. If I can provide more detail please let me know.

Thanks,

Daryl

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.