Public Class RoomState
' Properties to save colors and styles
Public wallsHatchStyle As HatchStyle
Public wallsColor1 As Color
Public wallsColor2 As Color
Public floorsHatchStyle As HatchStyle
Public floorsColor1 As Color
Public floorsColor2 As Color
code
Assistant
' Arrays to save wall states
Public verticalWalls()() As Boolean
Public horizontalWalls()() As Boolean
' Arrays to save placed items
Public placedItems()() As PlacedItemData
Public verticalWallItems()() As PlacedItemData
Public horizontalWallItems()() As PlacedItemData
' Parameterless constructor for deserialization
Public Sub New()
' This constructor is used by the deserializer
End Sub
' Constructor: captures the current state of the RoomView
Public Sub New(roomView As RoomView)
' Save colors and styles
wallsHatchStyle = roomView.wallsHatchStyle
wallsColor1 = roomView.wallsColor1
wallsColor2 = roomView.wallsColor2
floorsHatchStyle = roomView.floorsHatchStyle
floorsColor1 = roomView.floorsColor1
floorsColor2 = roomView.floorsColor2
' Save wall states
verticalWalls = CloneJaggedArray(roomView.verticalWalls)
horizontalWalls = CloneJaggedArray(roomView.horizontalWalls)
' Save placed items
placedItems = CopyPlacedItems(roomView.placedItems)
verticalWallItems = CopyPlacedItems(roomView.verticalWallItems)
horizontalWallItems = CopyPlacedItems(roomView.horizontalWallItems)
End Sub
' Helper method to clone jagged arrays
Private Function CloneJaggedArray(Of T)(array As T()()) As T()()
Dim lengthOuter As Integer = array.Length
Dim newArray As T()() = New T(lengthOuter - 1)() {}
For i As Integer = 0 To lengthOuter - 1
If array(i) IsNot Nothing Then
Dim lengthInner As Integer = array(i).Length
newArray(i) = New T(lengthInner - 1) {}
array.Copy(array(i), newArray(i), lengthInner)
End If
Next
Return newArray
End Function
' Helper method to copy placed items
Private Function CopyPlacedItems(items As PlacedItem()()) As PlacedItemData()()
Dim lengthOuter As Integer = items.Length
Dim copiedItems As PlacedItemData()() = New PlacedItemData(lengthOuter - 1)() {}
For x As Integer = 0 To lengthOuter - 1
If items(x) IsNot Nothing Then
Dim lengthInner As Integer = items(x).Length
copiedItems(x) = New PlacedItemData(lengthInner - 1) {}
For y As Integer = 0 To lengthInner - 1
If items(x)(y) IsNot Nothing Then
copiedItems(x)(y) = New PlacedItemData With {.ImageKey = items(x)(y).ImageKey}
End If
Next
End If
Next
Return copiedItems
End Function
' Restore the saved state back into the RoomView
Public Sub RestoreState(roomView As RoomView)
' Restore colors and styles
roomView.wallsHatchStyle = wallsHatchStyle
roomView.wallsColor1 = wallsColor1
roomView.wallsColor2 = wallsColor2
roomView.floorsHatchStyle = floorsHatchStyle
roomView.floorsColor1 = floorsColor1
roomView.floorsColor2 = floorsColor2
' Restore wall states
roomView.verticalWalls = CloneJaggedArray(verticalWalls)
roomView.horizontalWalls = CloneJaggedArray(horizontalWalls)
' Restore placed items
roomView.placedItems = RestorePlacedItems(placedItems)
roomView.verticalWallItems = RestorePlacedItems(verticalWallItems)
roomView.horizontalWallItems = RestorePlacedItems(horizontalWallItems)
End Sub
' Helper method to restore placed items
Private Function RestorePlacedItems(data As PlacedItemData()()) As PlacedItem()()
Dim lengthOuter As Integer = data.Length
Dim restoredItems As PlacedItem()() = New PlacedItem(lengthOuter - 1)() {}
For x As Integer = 0 To lengthOuter - 1
If data(x) IsNot Nothing Then
Dim lengthInner As Integer = data(x).Length
restoredItems(x) = New PlacedItem(lengthInner - 1) {}
For y As Integer = 0 To lengthInner - 1
If data(x)(y) IsNot Nothing Then
restoredItems(x)(y) = New PlacedItem With {.ImageKey = data(x)(y).ImageKey}
End If
Next
End If
Next
Return restoredItems
End Function
End Class
' Data class to represent a placed item
Public Class PlacedItem
Public Property ImageKey As String
End Class
' Serializable data class for placed item
Public Class PlacedItemData
Public Property ImageKey As String
End Class
Public Class ColorJsonConverter
Inherits JsonConverter(Of Color)
code
Public Overrides Sub Write(writer As Utf8JsonWriter, value As Color, options As JsonSerializerOptions)
writer.WriteStartObject()
writer.WriteNumber("A", value.A)
writer.WriteNumber("R", value.R)
writer.WriteNumber("G", value.G)
writer.WriteNumber("B", value.B)
writer.WriteEndObject()
End Sub
Public Overrides Function Read(ref As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color
Dim a As Byte = 255
Dim r As Byte = 0
Dim g As Byte = 0
Dim b As Byte = 0
While ref.Read()
If ref.TokenType = JsonTokenType.EndObject Then
Exit While
End If
If ref.TokenType = JsonTokenType.PropertyName Then
Dim propertyName = ref.GetString()
If ref.Read() Then
Select Case propertyName
Case "A"
a = ref.GetByte()
Case "R"
r = ref.GetByte()
Case "G"
g = ref.GetByte()
Case "B"
b = ref.GetByte()
End Select
End If
End If
End While
Return Color.FromArgb(a, r, g, b)
End Function
End Class
Public Class HatchStyleJsonConverter
Inherits JsonConverter(Of HatchStyle)
code
Public Overrides Sub Write(writer As Utf8JsonWriter, value As HatchStyle, options As JsonSerializerOptions)
writer.WriteStringValue(value.ToString())
End Sub
Public Overrides Function Read(ByRef reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As HatchStyle
Dim hatchStyleName = reader.GetString()
Dim hatchStyle As HatchStyle
If [Enum].TryParse(hatchStyleName, hatchStyle) Then
Return hatchStyle
Else
Return HatchStyle.Cross ' Default value if parsing fails
End If
End Function
End Class
We do have errors. Including the warning. I had no idea what you meant when telling me how to fix it :(
Severity Code Description Project File Line Suppression State
Error (active) BC30398 'Public Overrides Function Read(ref As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color' cannot override 'Public MustOverride Overloads Function Read(ByRef reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color' because they differ by a parameter that is marked as 'ByRef' versus 'ByVal'. Enigma D:\01 - Grags\Visual Studio\Enigma\Enigma\RoomState.vb 142
Error (active) BC30668 'Utf8JsonReader' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'. Enigma D:\01 - Grags\Visual Studio\Enigma\Enigma\RoomState.vb 142
Error (active) BC30668 'Utf8JsonReader' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'. Enigma D:\01 - Grags\Visual Studio\Enigma\Enigma\RoomState.vb 181
Warning (active) BC42025 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. Enigma D:\01 - Grags\Visual Studio\Enigma\Enigma\RoomState.vb 57
The errors you're encountering are primarily due to mismatches in method signatures and limitations in the version of VB.NET compiler or .NET framework your project is targeting. Let's address each error one by one and explain how to fix them.
1. Error BC30398:
code
'Public Overrides Function Read(ref As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color' cannot override 'Public MustOverride Overloads Function Read(ByRef reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color' because they differ by a parameter that is marked as 'ByRef' versus 'ByVal'.
Cause:
This error occurs because the overridden Read method in your ColorJsonConverter class does not match the signature of the base class method in terms of parameter passing. Specifically, the base method expects a ByRef parameter, but your method has a parameter named ref which is being passed ByVal (the default in VB.NET).
Additionally, using ref as a parameter name can cause confusion because ref is a keyword in C# (though not in VB.NET), and it's better to avoid using it as a parameter name.
Solution:
Update the method signature to match the base class exactly, using ByRef and matching the parameter name (reader).
Updated Code:
vb
Public Overrides Function Read(ByRef reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color
' Method implementation...
End Function
Do the same for your HatchStyleJsonConverter class.
2. Error BC30668:
code
'Utf8JsonReader' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'.
Cause:
Utf8JsonReader is a ref struct, which means it contains managed pointers and is subject to certain limitations. Specifically, older versions of the VB.NET compiler do not support ref struct types and ByRef returns, leading to this error.
Solution:
You need to ensure that your project targets a version of .NET that supports ref struct types in VB.NET. This means updating your project to target at least .NET 5.0 or later, and using a version of Visual Studio that includes the VB.NET compiler capable of handling ref struct types.
Steps to Fix:
Update Target Framework:
Right-click on your project in the Solution Explorer and select Properties.
Under the Application tab, find the Target framework dropdown.
Change it to .NET 5.0 or .NET 6.0 (if available).
Update Visual Studio:
Make sure you're using Visual Studio 2019 (Version 16.8 or later) or Visual Studio 2022, which support VB.NET with .NET 5.0 and above.
Install Required SDKs:
Download and install the .NET 5.0 or .NET 6.0 SDK from the official Microsoft website if you haven't already.
3. Warning BC42025:
code
Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
Cause:
This warning occurs because you're accessing a shared (static) member Copy of the Array class through an instance (array.Copy(...)), which is not the recommended way.
Solution:
Access the shared member directly through the class name Array.Copy(...) instead of an instance.
Updated Code:
In your CloneJaggedArray method, change:
vb
array.Copy(array(i), newArray(i), lengthInner)
to:
vb
Array.Copy(array(i), newArray(i), lengthInner)
Summary of Changes:
Change Method Signatures:
In both ColorJsonConverter and HatchStyleJsonConverter, update the Read method to use ByRef reader As Utf8JsonReader and ensure parameter names match.
Update Project to .NET 5.0 or Later:
Adjust your project's target framework to .NET 5.0 or newer.
Update Visual Studio if necessary to support the new framework and compiler features.
Correct Usage of Shared Members:
Replace array.Copy(...) with Array.Copy(...) in your CloneJaggedArray method to eliminate the warning.
Example of the Corrected ColorJsonConverter:
vb
Public Class ColorJsonConverter
Inherits JsonConverter(Of Color)
Public Overrides Sub Write(writer As Utf8JsonWriter, value As Color, options As JsonSerializerOptions)
writer.WriteStartObject()
writer.WriteNumber("A", value.A)
writer.WriteNumber("R", value.R)
writer.WriteNumber("G", value.G)
writer.WriteNumber("B", value.B)
writer.WriteEndObject()
End Sub
Public Overrides Function Read(ByRef reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Color
Dim a As Byte = 255
Dim r As Byte = 0
Dim g As Byte = 0
Dim b As Byte = 0
While reader.Read()
If reader.TokenType = JsonTokenType.EndObject Then
Exit While
End If
If reader.TokenType = JsonTokenType.PropertyName Then
Dim propertyName = reader.GetString()
If reader.Read() Then
Select Case propertyName
Case "A"
a = reader.GetByte()
Case "R"
r = reader.GetByte()
Case "G"
g = reader.GetByte()
Case "B"
b = reader.GetByte()
End Select
End If
End If
End While
Return Color.FromArgb(a, r, g, b)
End Function
End Class
Alternative Solution:
If for some reason you cannot update to .NET 5.0 or later, you might consider using Newtonsoft.Json (Json.NET) instead of System.Text.Json. Newtonsoft.Json does not use ref struct types and is compatible with older versions of .NET Framework and VB.NET.
However, using Newtonsoft.Json would require you to adjust your serialization and deserialization code accordingly.
Conclusion:
By updating your method signatures to match the base class and updating your project to a version of .NET that supports ref struct types in VB.NET, you should be able to resolve the errors you're encountering. Additionally, correcting the use of shared members will eliminate the warning.
Feel free to ask if you need further clarification or assistance with these steps!