Visual Studio 2022: Enum Values Not Recognized in C# Project Wrapping yt-dlp

Will Pittenger 311 Reputation points
2025-03-17T04:59:58.2166667+00:00

I have a C# project targetting .NET 9.0 in which I'm wrapping yt-dlp (CLI video downloader) so you can use it directly in .NET or PowerShell. Unfortunately, I have a switch statement that needs to select between enum values. You can get the project from Chasing-many-issues. On lines 88, 93, and 98 of Goodies/PowerShell/WillPittenger.Goodies.PowerShell.YtDlpWrapper/GetInfoFromYtDlp.cs, Visual Studio 2022 Community reports it can't find an enum value, CS0246. But when I go to the declaration of the type, ItemTypes (describes what an JSON object returned from yt-dlp is), it declares all three values that the IDE complains about. The IDE did see the enum type itself along with other types in the enum's namespace. So why doesn't it find entries in the enum type? I tried recompiling the entire solution and restarting Visual Studio. Neither helped.

Any suggestions?

The declaration of ItemTypes:

namespace WillPittenger.Goodies.YtDlpWrapper;
public class YtDlpWrapper
{
	public enum ItemTypes
	{
		chan,
		playList,
		vid,
	}
}

The switch statement

namespace WillPittenger.Goodies.PowerShell.YtDlpWrapper;

[System.Management.Automation.OutputType(typeof(Goodies.YtDlpWrapper.Chan), typeof(Goodies.YtDlpWrapper.Vid), typeof(Goodies.YtDlpWrapper.PlayList))]
[System.Management.Automation.Alias("gvi")]
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "InfoFromYtDlp")]
public class GetInfoFromYtDlp : BaseCmdLet
{
⋮
	switch(joDataToWrite.Values[Goodies.YtDlpWrapper.YtDlpWrapper.KnownYtDlpFields.field_ItemType.strName].val.objVal)
			{
				case Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes.chan.ToString():
					WriteObject(new Goodies.YtDlpWrapper.Chan(joDataToWrite));

					break;

				case Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes.playList.ToString():
					WriteObject(new Goodies.YtDlpWrapper.PlayList(joDataToWrite));

					break;

				case Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes.vid.ToString():
					WriteObject(new Goodies.YtDlpWrapper.Vid(joDataToWrite));

					break;

				default:
					throw new System.InvalidOperationException($"Unable to interpret entry sent back from yt-dlp.");
			}
⋮
}

Community Center Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2025-03-17T08:47:53.4166667+00:00

    Try something like this:

    string s = joDataToWrite.Values[Goodies.YtDlpWrapper.YtDlpWrapper.KnownYtDlpFields.field_ItemType.strName].val.objVal;
    
    if( ! Enum.TryParse( s, out Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes val ) )
    {
        throw new System.InvalidOperationException( $"Unable to interpret entry sent back from yt-dlp: '{s}'." );
    }
    
    switch( val )
    {
    case Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes.chan:
        // . . .
        break;
    case Goodies.YtDlpWrapper.YtDlpWrapper.ItemTypes.playList:
        // . . .
        break;
    default:
        throw new System.NotSupportedException( $"Not supported: '{s}'." );
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.