Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the same error in multiple places. I am converting some c# to vb .net when I try to define Public structures or classes that implements some Iinterface I get "class can only inherit from other classes". Many thanks to
A Simple - Yet Quite Powerful - Palette Quantizer in C#
by Smart K8. If there is any interest I'll post the VB version when it is working.

Here is an example of the problem:

The Interface:

Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Runtime.InteropServices

Namespace ColorQuantizationVB.ColorCaches

    Public Interface IColorCache
        Sub Prepare()
        Sub CachePalette(ByVal palette As IList(Of Color))
        Sub GetColorPaletteIndex(ByVal color As Color, <Out> ByRef paletteIndex As Integer)
    End Interface

End Namespace


Here is the Class:

Namespace ColorQuantizationVB.ColorCaches
    Public MustInherit Class BaseColorCache
        Inherits IColorCache

        Private ReadOnly cache As ConcurrentDictionary(Of Integer, Integer)
        Protected Property ColorModel As ColorModel
        Public MustOverride ReadOnly Property IsColorModelSupported As Boolean

        Protected Sub New()
            cache = New ConcurrentDictionary(Of Integer, Integer)
        End Sub

        Public Sub ChangeColorModel(ByVal colorModel As ColorModel)
            colorModel = colorModel
        End Sub

        Protected MustOverride Sub OnCachePalette(ByVal palette As IList(Of Color))
        Protected MustOverride Sub OnGetColorPaletteIndex(ByVal color As Color, <Out> ByRef paletteIndex As Integer)

        Public Overridable Sub Prepare()
            cache.Clear()
        End Sub

        Public Sub CachePalette(ByVal palette As IList(Of Color))
            OnCachePalette(palette)
        End Sub

        Public Sub GetColorPaletteIndex(ByVal color As Color, <Out> ByRef paletteIndex As Integer)
            Dim key As Integer = color.R << 16 Or color.G << 8 Or color.B
            paletteIndex = cache.AddOrUpdate(key, Function(colorKey)
                                                      Dim paletteIndexInside As Integer
                                                      OnGetColorPaletteIndex(color, paletteIndexInside)
                                                      Return paletteIndexInside
                                                  End Function, Function(colorKey, inputIndex) inputIndex)
        End Sub
    End Class


Here is the original C# code for the Iinterface

using System;
using System.Collections.Generic;
using System.Drawing;

namespace SimplePaletteQuantizer.ColorCaches
{
    public interface IColorCache
    {
        /// <summary>
        /// Prepares color cache for next use.
        /// </summary>
        void Prepare();

        /// <summary>
        /// Called when a palette is about to be cached, or precached.
        /// </summary>
        /// <param name="palette">The palette.</param>
        void CachePalette(IList<Color> palette);

        /// <summary>
        /// Called when palette index is about to be retrieve for a given color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="paletteIndex">Index of the palette.</param>
        void GetColorPaletteIndex(Color color, out Int32 paletteIndex);
    }
}


and the base color class

using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.Collections.Generic;
using SimplePaletteQuantizer.ColorCaches.Common;

namespace SimplePaletteQuantizer.ColorCaches
{
    public abstract class BaseColorCache : IColorCache
    {
        #region | Fields |

        private readonly ConcurrentDictionary<Int32, Int32> cache;

        #endregion

        #region | Properties |

        /// <summary>
        /// Gets or sets the color model.
        /// </summary>
        /// <value>The color model.</value>
        protected ColorModel ColorModel { get; set; }

        /// <summary>
        /// Gets a value indicating whether this instance is color model supported.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is color model supported; otherwise, <c>false</c>.
        /// </value>
        public abstract Boolean IsColorModelSupported { get; }

        #endregion

        #region | Constructors |

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseColorCache"/> class.
        /// </summary>
        protected BaseColorCache()
        {
            cache = new ConcurrentDictionary<Int32, Int32>();
        }

        #endregion

        #region | Methods |

        /// <summary>
        /// Changes the color model.
        /// </summary>
        /// <param name="colorModel">The color model.</param>
        public void ChangeColorModel(ColorModel colorModel)
        {
            ColorModel = colorModel;
        }

        #endregion

        #region << Abstract methods |

        /// <summary>
        /// Called when a palette is about to be cached, or precached.
        /// </summary>
        /// <param name="palette">The palette.</param>
        protected abstract void OnCachePalette(IList<Color> palette);

        /// <summary>
        /// Called when palette index is about to be retrieve for a given color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="paletteIndex">Index of the palette.</param>
        protected abstract void OnGetColorPaletteIndex(Color color, out Int32 paletteIndex);

        #endregion

        #region << IColorCache >>

        /// <summary>
        /// See <see cref="IColorCache.Prepare"/> for more details.
        /// </summary>
        public virtual void Prepare()
        {
            cache.Clear();
        }

        /// <summary>
        /// See <see cref="IColorCache.CachePalette"/> for more details.
        /// </summary>
        public void CachePalette(IList<Color> palette)
        {
            OnCachePalette(palette);
        }

        /// <summary>
        /// See <see cref="IColorCache.GetColorPaletteIndex"/> for more details.
        /// </summary>
        public void GetColorPaletteIndex(Color color, out Int32 paletteIndex)
        {
            Int32 key = color.R << 16 | color.G << 8 | color.B;

            paletteIndex = cache.AddOrUpdate(key,
                colorKey =>
                {
                    Int32 paletteIndexInside;
                    OnGetColorPaletteIndex(color, out paletteIndexInside);
                    return paletteIndexInside;
                }, 
                (colorKey, inputIndex) => inputIndex);
        }

        #endregion
    }
}


What I have tried:

Ive tried playing around with MustInherit Inherit and Interface but I really have never quite understood how these interfaces work. I've tried reading up on Implementing IEnumerable and IEnumerator and my ears are bleeding the info out.
I'm old, bald too.
Posted
Updated 3-Oct-19 17:29pm

1 solution

Inherits IColorCache

I think you want:
Implements IColorCache

"Inherits" is used to inherit an interface in another interface (apparently).

(I'm C#.)
 
Share this answer
 
Comments
Maciej Los 4-Oct-19 2:04am    
5ed!
[EDIT]
Seems that Inherits keyword is available too (for classes only):
Public Class square : Inherits shape

See: MustInherit (Visual Basic) | Microsoft Docs[^]
phil.o 4-Oct-19 5:01am    
More exactly, you can inherit from a class, abstract or not; but you can only implement an interface.
Maybe this has changed since there is the ability to provide default implementations in interfaces declarations? I don't know since I do not code much in VB.NET anymore.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900