0

What i would like to be able to do is control the assignment of two objects. So if my class looked like this in the case of a string (very crude but just and example)

public class fixLen
{
    public int len;
    public string str;

    public fixLen(string str, int len)
    {
       this.str = str;
       this.len = len;
    }
}

Then in my main class i do this;

fixLen str1 as new fixLen("abc", 3);
fixLen str2 as new fixLen("wxyz", 4);

str1 = str2

When str2 is assigned to str1 i would like to be able to query the len property of each object and perform the assignment. In this case i would like the str1.str property to end up with a value of "wxy" truncating "wxyz" to 3 characters (str1.len). I have tried to implement this with the "operator" program element but it doesn't appear to allow me to query both the source and target objects. A key element of this functionality is it has to be implemented in the class. I do not want to have any other syntax in my main class other than;

str1 = str2

I guess in it's simplest terms i would like to create another atomic type, like int, float or string.

In the case of a number;

public class fixNum
{
    public int len;
    public string val;

    public fixLen(int val, int len)
    {
       this.val= val;
       this.len = len;
    }
}

Then in my main class;

fixNum num1 as new fixNum(0, 2)
fixnum num2 as new fixNum(999, 3)
num1 = num2 + 1

So num1 would end up with a value of 99.

blink
  • 163
  • 12
  • I'm just having an idea here, but why not create a method in `fixLen` class like `Import(FixLen obj)` that takes another object of this type as a parameter, and then copies the string from the `obj` but limited to the length of it's own (`this.len`)? Seems simple to implement, but maybe there are other options to achieve this kind of behaviour. – Asunez Oct 12 '18 at 06:37
  • I think it can be done with the help of copy constructor, but you have to add your required functionality i.e. number of characters of string to copy – Waleed Naveed Oct 12 '18 at 06:42
  • So; str1 = str2 Would change to; str1.Import(str2) Is that what you're thinking? In my case the underlying functionality would have to be implemented across all functionality. Like; num1 = num3 + num4 / num5 So in my case those num objects would all have a len property and num1 would be truncated to a fixed number of digits. So i need this functionality to kick in whenever the object is referenced – blink Oct 12 '18 at 06:43
  • https://dotnetfiddle.net/17BgTQ see this.. and try to explain things while updating the code for a better help – Md. Tazbir Ur Rahman Bhuiyan Oct 12 '18 at 06:46

1 Answers1

0

The only way I can think of is:

define a method in your class:

public void SetValue(fixLen f)
{
    str = f.str.Substring(0, len);
}

And use it str1.SetValue(str2);.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Just thinking, can we overload "Equal Operator" in C#, as in C++ . ? – Waleed Naveed Oct 12 '18 at 06:52
  • 1
    @WaleedNaveed No, at least I don't know any posiibility in C#. [See this](https://stackoverflow.com/questions/18782218/is-it-possible-to-override-assignment-in-c-sharp/18782263) – Michał Turczyn Oct 12 '18 at 06:53
  • You can but it's static and as far as i can see you can only access the source class, not the target class (I'm sure i'm missing something here) https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-implement-user-defined-conversions-between-structs – blink Oct 12 '18 at 06:56
  • @blink It's conversion between two structs. – Michał Turczyn Oct 12 '18 at 07:03
  • My apologies. It did work when i tried it in a class though. I guess i'll have to use . notation and add setter getters – blink Oct 12 '18 at 07:07
  • @blink Well, I am happy you figured it out. If my answer helped you, consider accepting it (green check mark on the left) and optionally upvoting. – Michał Turczyn Oct 12 '18 at 07:11