Link to home
Start Free TrialLog in
Avatar of Mutsop
MutsopFlag for Belgium

asked on

Generic Id class vb.net to c#

Hi,

I recently found a very useful Id class which could be inherited by any class to actually store an Id.

Here is the vb.net version:
Option Strict On
Option Explicit On
Option Infer On

Public MustInherit Class Entity(Of T As Structure)

    Protected Friend Overridable Property Id As T

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim other As Entity(Of T) = TryCast(obj, Entity(Of T))
        If other Is Nothing Then Return False
        If Not [GetType].Equals(other.GetType) Then Return False
        If Transient() OrElse other.Transient Then
            Return MyBase.Equals(obj)
        End If
        Return Id.Equals(other.Id)
    End Function

    Public Overrides Function GetHashCode() As Integer
        If Transient() Then Return MyBase.GetHashCode()
        Return Id.GetHashCode
    End Function

    Protected Overridable Function Transient() As Boolean
        Dim value As T = Nothing
        Return Id.Equals(value)
    End Function
End Class

Open in new window


No I tried translating this into c# but am having  some problems:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TCG.Domain
{
    abstract class Entity<T> where T : struct
    {
        protected internal virtual T Id;

        public override bool Equals(object obj)
        {
            Entity<T> other = obj as Entity<T>;
            if(other == null){return false;}
            if(![GetType].Equals(other.GetType)){return false;}
            if(Transient() || other.Transient()){return Id.Equals(obj);}
        }

        public override int GetHashCode()
        {
            if(Transient()) {return base.GetHashCode();}
            return Id.GetHashCode();
        }

        protected virtual bool Transient()
        {
            T value == null;
            return Id.Equals(value);
        }
    }
}

Open in new window


The problems are at:
[GetType] which doesn't really give me any indication in the error message on what to change.
and T value == null

I'm new to learning c# but have a good knowledge on vb.net.

Any ideas on what to change?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

This

T value == null;

should be

T value = null;

as == is comparison operator.


>if(![GetType].Equals(other.GetType)){return false;}

I think it would be

if (typeof(this) != typeof(other)
Avatar of Mutsop

ASKER

On the typeof part I get following error:
Only assignment, call, increment, decrement, and new object expressions can be used as a statement

And on the null part I get this:
Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead
Which shouldn't be happening :S
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mutsop

ASKER

Yes I did close it,

But apparently there are 2 errors: A Type expected missing and the error I posted before.

Also just declaring the value gives me a Use of unassigned local variable 'value' error
What would you use this class for?
Avatar of Mutsop

ASKER

Well to assign an Id to each object when stored.
Avatar of Mutsop

ASKER

I did get my full answer on another post,

thanks though, it helped me a bit.