user34537
user34537

Reputation:

overload operator = in C#. How can i accept other types?

So a friend was telling me how a game was hacked and how the technique worked. He then asked whats the best way to prevent that kind of attack. The most straight forward way i knew was to A) the shuffle the bits of important value B) hash the values and compare them every time (an int that holds the score or money is likely to be checked rarely).

Then i tried the implementation in C#, i couldnt overload the = operator. How can i do this?

ex code.

class EncryptVal <T>
{
    T v;
    public T operator = (T v2)
    {
        //shuffle bits
    }
    public T operator ()()
    {
        //return unshuffle bits
    }
}

Upvotes: 2

Views: 2608

Answers (6)

Guffa
Guffa

Reputation: 700562

You can encapsulate the value in the class and overload the implicit conversions to and from the class:

public class EncryptVal<T> {

    private T _value;

    private EncryptVal(T value) {
        _value = value;
    }

    public static implicit operator EncryptVal<T>(T value) {
        //shuffle bits
        return new EncryptVal<T>(value);
    }

    public static implicit operator T(EncryptVal<T> value) {
        //unshuffle bits
        return value._value;
    }

}

Usage:

// implicit conversion from int
EncryptVal<int> e = 42;

// implicit conversion to int
int i = e;

Upvotes: 10

thinkbeforecoding
thinkbeforecoding

Reputation: 6778

I would go the for implicit/explicit operator overloading for the implementation part.

Probably the explicit one since your conversion does heavy processing, and that it eventually could fail.

I would just add that shuffling bits seems to be only an obfuscation technic that will surely not last long if you have wishfull hackers interested in your game. You probably need stronger cryptography to protect your data, but more context is needed.

Upvotes: 0

Omar Kooheji
Omar Kooheji

Reputation: 55770

Dont use = for setting the value. You cant overload assignment.

What you can do is hide it behind a property.

int _encyptedValue;
Public int myInt
{
    get
    {
        return Decrypt(_encryptedValue);
    }
    set
    {
         _encryptedValue = Encrypt(value);
    }
}

You get to chosse your decryption/encryption

Upvotes: 0

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

I assume that you come from C++ where it is very common to write classes that are used like primitive data types. In C# you do things more explicitly.

I would write it as a property or as two methods, eg:

class EncryptVal <T>
{
    T v;
    public T Value
    {
      get
      {
        //return unshuffle bits
      }
      set
      {
        //shuffle bits
      }
    }
}

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185663

You're looking for the implicit and explicit operator, rather than saying =. This allows you to define how things will work when cast implicitly (ie, just an assignment) and explicitly (ie, there's a casting operator).

public static implicit operator Type1(Type2 p) {}
public static explicit operator Type1(Type2 p) {}

Upvotes: 13

Josh G
Josh G

Reputation: 14256

You are not allowed to overload the assignment operator in C#. Here's the MSDN documentation on it.

You'll have to create your own function for this behavior.

Upvotes: 2

Related Questions