Search code examples
c#implicit-conversion

Compiler Error CS0029: Could not implicitly convert to uint


There was bunch of question about implicitly convert but none of them has sheed me some light to figure this out.

So to speak more about this issue I got. I am using VulkanSharp bindings in my project. I was writing Renderer code and I have runned into weird anomally that kind of shocked me.

            uint _queueFamilyUsedIndex;

            var _deviceInfo = new DeviceCreateInfo
            {
                EnabledExtensionNames = new string[] { "VK_KHR_swapchain" },
                QueueCreateInfoCount = new DeviceQueueCreateInfo { QueueFamilyIndex = _queueFamilyUsedIndex }
            };

This part of the code making this difficult:

QueueCreateInfoCount = new DeviceQueueCreateInfo { QueueFamilyIndex = _queueFamilyUsedIndex }

I did checked what types those variables are requiring and it was weird knowing you did good.

Those are the Classes:

public class DeviceCreateInfo : MarshalledObject
    {
        public DeviceCreateInfo();

        public uint Flags { get; set; }
        public uint QueueCreateInfoCount { get; set; }
        public DeviceQueueCreateInfo[] QueueCreateInfos { get; set; }
        public uint EnabledLayerCount { get; set; }
        public string[] EnabledLayerNames { get; set; }
        public uint EnabledExtensionCount { get; set; }
        public string[] EnabledExtensionNames { get; set; }
        public PhysicalDeviceFeatures EnabledFeatures { get; set; }

        public override void Dispose(bool disposing);
    }



public class DeviceQueueCreateInfo : MarshalledObject
    {
        public DeviceQueueCreateInfo();

        public uint Flags { get; set; }
        public uint QueueFamilyIndex { get; set; }
        public uint QueueCount { get; set; }
        public float[] QueuePriorities { get; set; }

        public override void Dispose(bool disposing);
    }

As seen those types are correct.


Solution

  • The problem here is you have defined the field/property QueueCreateInfoCount of datatype uint while in code you are trying to put object of type DeviceQueueCreateInfo which will not work obviously as C# is strongly typed language.

    I think your intention is to write something like:

    QueueCreateInfos = new DeviceQueueCreateInfo[] 
                       {
                         new DeviceQueueCreateInfo 
                        { 
                           QueueFamilyIndex = _queueFamilyUsedIndex 
                        }
                       }
    

    You need to use the other property for holding it in array collection as per my understanding:

    // it looks like you don't need this one there
    public uint QueueCreateInfoCount { get; set; }
    // but may be you need to use this one 
    public DeviceQueueCreateInfo[] QueueCreateInfos { get; set; }