none
Convertible Structure RRS feed

  • Question

  • Hello everyone,

    I am creating a structure, and here is the code

    Imports System.Text.RegularExpressions
    Namespace Steamworks
        Public Structure SteamID : Implements IDisposable
    
            Shared ReadOnly TypeChrs As String = "IUMGAPCgTLca"
            Shared ReadOnly Steam2Regex As Regex = New Regex("STEAM_(?<Universe>[0-4]):(?<AuthServer>[0-1]):(?<AuthID>[1-9][0-9]{0,9}$|^2147483647)", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
            Shared ReadOnly Steam3Regex As Regex = New Regex("(?<Type>[IUMGAPCgTLca]):(?<Universe>[0-4]):(?<ID>[1-9][0-9]{0,9})(?::(?<Instance>[0-9]{1,10}))?", RegexOptions.Compiled)
    
    #Region "Properties"
            Public Property TypeChr As Char
            Public Property AuthID As UInteger
            Public Property AuthServer As Boolean
            Public Property Instance As UInteger
            Public Property Universe As EAccountUniverse
            Public Property Type As EAccountType
                Get
                    Return TypeChrToEnum(Me.TypeChr)
                End Get
                Set(value As EAccountType)
                    Me.TypeChr = EnumToTypeChr(value, 1)
                End Set
            End Property
    #End Region
    #Region "Main"
            Public Sub New(ByVal Value As String) 'SteamID2 SteamID3 SteamID64
                If Steam2Regex.IsMatch(Value) Then
                    Dim Match = Steam2Regex.Match(Value)
                    Me.Universe = CType(Match.Groups("Universe").Value, Integer)
                    Me.AuthServer = CType(Match.Groups("AuthServer").Value, Integer)
                    Me.AuthID = CType(Match.Groups("AuthID").Value, Integer)
                    Me.TypeChr = "U"
                    Me.Instance = 1
                ElseIf Steam3Regex.IsMatch(Value) Then
                    Dim Match = Steam3Regex.Match(Value)
                    Me.TypeChr = Match.Groups("Type").Value
                    Me.Universe = CType(Match.Groups("Universe").Value, Integer)
                    Me.AuthID = CType(Match.Groups("ID").Value, Integer) / 2
                    If Not String.IsNullOrWhiteSpace(Match.Groups("Instance").Value) Then Me.Instance = CType(Match.Groups("Instance").Value, Integer)
    
                    Select Case Me.TypeChr
                        Case "c"
                            Me.Instance = CUInt((CType(Instance, EChatInstanceFlag) Or EChatInstanceFlag.Clan))
                        Case "L"
                            Instance = CUInt((CType(Instance, EChatInstanceFlag) Or EChatInstanceFlag.Lobby))
                        Case Else
                            Instance = 1
                    End Select
                ElseIf Regex.IsMatch(Value, "^[0-9 ]+$") AndAlso Value >= 76561197960265728 Then
                    Dim Hex = Kit.Utilities.DataConverter.DecimalToHexadecimal(Value, 16)
                    Select Case Hex.Last
                        Case "1", "3", "5", "7", "9", "B", "D", "F"
                            Me.AuthServer = True
                        Case Else
                            Me.AuthServer = False
                    End Select
                    Me.AuthID = (Kit.Utilities.DataConverter.HexadecimalToDecimal(Hex.Remove(0, 8)) - Me.AuthServer) / 2
                    Me.Instance = Kit.Utilities.DataConverter.HexadecimalToDecimal(Hex.Remove(0, 3).Remove(5))
                    Me.Type = Kit.Utilities.DataConverter.HexadecimalToDecimal(Hex.Remove(0, 2).Remove(1))
                    Me.Universe = Kit.Utilities.DataConverter.HexadecimalToDecimal(Hex.Remove(2))
                Else
                    Me.Dispose()
                End If
            End Sub
    
            Public Shadows Sub Dispose() Implements IDisposable.Dispose
                Me.Finalize()
            End Sub
    #End Region
    #Region "Methods"
            Public Shadows Function ToString() As String
                Return Me.ToSteamID64
            End Function
            Public Function ToSteamID2() As String
                If Me.AuthID < 1 OrElse Me.Universe < 0 OrElse Not Me.Type = EAccountType.Individual Then Return Nothing
    
                Dim UniverseDigit = If((Universe <= EAccountUniverse.[Public]), "0", [Enum].Format(GetType(EAccountUniverse), Me.Universe, "D"))
                Return $"STEAM_{CInt(UniverseDigit)}:{If(Me.AuthServer, "1", "0")}:{Me.AuthID}"
            End Function
            Public Function ToSteamID32() As UInteger
                If Me.AuthID < 1 Then Return Nothing
    
                Return Me.AuthID * 2 + Me.AuthServer
            End Function
            Public Function ToSteamID3() As String
                If Me.AuthID < 1 OrElse Me.Universe < 0 Then Return Nothing
    
                Dim renderInstance As Boolean = False
                Select Case Type
                    Case EAccountType.AnonGameServer, EAccountType.Multiseat
                        renderInstance = True
                    Case EAccountType.Individual
                        renderInstance = (Instance <> EInstanceType.Desktop)
                    Case EAccountType.Invalid, EAccountType.Unknown
                        Return Nothing
                End Select
    
                Dim UniverseDigit = If((Me.Universe <= EAccountUniverse.[Public]), "1", [Enum].Format(GetType(EAccountUniverse), Me.Universe, "D"))
    
                Return $"[{Me.TypeChr}:{CUInt(UniverseDigit)}:{Me.AuthID * 2 + Me.AuthServer }" & If(renderInstance, $":{Me.Instance}", "") & "]"
            End Function
            Public Function ToSteamID64() As ULong
                If Me.AuthID < 1 OrElse Me.Type <= 0 OrElse Me.Universe < 0 Then Return Nothing
    
                Dim Hex As String = ""
                If Type = EAccountType.Individual AndAlso Me.Universe = EAccountUniverse.Individual Then
                    Hex &= Kit.Utilities.DataConverter.DecimalToHexadecimal(EAccountUniverse.Public, 2)
                Else
                    Hex &= Kit.Utilities.DataConverter.DecimalToHexadecimal(Me.Universe, 2)
                End If
                Hex = Hex.Insert(Hex.Length, Kit.Utilities.DataConverter.DecimalToHexadecimal(Me.Type, 1))
                Hex = Hex.Insert(Hex.Length, Kit.Utilities.DataConverter.DecimalToHexadecimal(Me.Instance, 5))
                Hex = Hex.Insert(Hex.Length, Kit.Utilities.DataConverter.DecimalToHexadecimal(Me.AuthID * 2 + Me.AuthServer, 8))
                Return Kit.Utilities.DataConverter.HexadecimalToDecimal(Hex)
            End Function
    #End Region
    
        End Structure
    End Namespace

    When i check

    If not SteamID "=" or "is" Nothing Then
                    Return ProfileResponse
    End If

    It gives me an error that the operator "=","is" is not defined

    and when i set a variable like the following

    Dim Test As New SteamID
    If Test = 4124125125236411231231 Then
         'do something
    End If

    It also return an error.

    So my question is, how can i check if my structure equals a ULong, UInteger or string with just the operator "=", and how can i check if it's nothing.

    Thanks in advance


    • Edited by Lersubem Saturday, June 1, 2019 9:46 AM
    Saturday, June 1, 2019 9:45 AM

Answers

  • Hello,

    See the following post 

    https://stackoverflow.com/questions/3169028/vb-net-structs-and-nothing-problems

    A structure is a value type, it cannot be Nothing. The Nullable type can solve your problem, put a question mark after the type name to make it short and snappy. Here's an example:

    Module Module1
    
        Structure Test
            Dim field1 As String
        End Structure
    
        Private Function Foo() As Test?
            Return Nothing
        End Function
    
        Sub Main()
            Dim st As Test? = Foo()
            Debug.Assert(st is Nothing)
        End Sub
    
    End Module
    There are three other examples on the page, pretty much the same as above to decide from.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Lersubem Tuesday, July 9, 2019 10:50 PM
    Saturday, June 1, 2019 10:10 AM
    Moderator