We help IT Professionals succeed at work.

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?
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
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)

Author

Commented:
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
Most Valuable Expert 2012
Top Expert 2014
Commented:
The error about null is understandable but I dont know why it accepts nothing in vb. But you can just declare it

T value;

Did you close the parenthesis on the typeof statement? ( I missed one).

Author

Commented:
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
Most Valuable Expert 2012
Top Expert 2014

Commented:
What would you use this class for?

Author

Commented:
Well to assign an Id to each object when stored.

Author

Commented:
I did get my full answer on another post,

thanks though, it helped me a bit.