У меня возникла проблема с реализацией iinterfaces в структурах в VB .NET 15
Я получаю одну и ту же ошибку в нескольких местах. Я конвертирую некоторые c# в vb .net, когда пытаюсь определить публичные структуры или классы, реализующие некоторый Iinterface, я получаю "класс может наследовать только от других классов". Большое спасибо
A Simple - Yet Quite Powerful - Palette Quantizer in C#купить смарт-К8. Если есть какой-то интерес, я опубликую версию VB, когда она будет работать.
Вот пример этой проблемы:
интерфейс:
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
Вот этот класс:
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
Вот исходный код C# для 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); } }
и базовый цветовой класс
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 } }
Что я уже пробовал:
Я пробовал играть с MustInherit Inherit и Interface, но на самом деле никогда не понимал, как работают эти интерфейсы. Я пробовал читать о внедрении IEnumerable и IEnumerator, и мои уши кровоточат информацией.
I'm old, bald too.