1

My Language is C#.

obj1.Value = "test";

If object value is changed like this, it is easy to handle. I can put the event code in Name Property Set.

But

Class obj1 = new Class("string1");
obj1  = new Class("string1");

When object is changed by assignment like this, How can I know that and Handle it?

I want to limit Name Length and it is limited by another property "Length".

I have 3 constructors.

Class(string value);
Class(int length);
Class(int length, string value);

And 2 Properties.

.Value
.Length

I made obj1 like this

Class obj1 = new Class(3, "ab")

My problem is this.

obj1 = new Class("abcde")

I want to limit Value length 3 but the length of new Class("abcde") is 5. (It can have different value by coding but in my case length is 5)

How can I solve it?

jeonggu
  • 103
  • 1
  • 6
  • 1- You should specify the language you're using. For instance in C++, you could overload operator "=" to handle this. However it's probably not possible in most of language to watch for a reference change. 2- What is the purpose of this ? What are you really trying to do with this ? – Walfrat Jun 16 '16 at 07:08
  • @Walfrat 1. Sorry, my language is C#. It seems that "=" overloading is not possilble in C#. right? 2. I want to limit Name Property Length by 10. – jeonggu Jun 16 '16 at 07:13
  • You don't need to know if the object has changed then do you? just check the length in the setter maybe? – n1ff Jun 16 '16 at 07:15
  • Look here http://stackoverflow.com/questions/12989192/custom-setter-for-c-sharp-model you can define a custom setter, so you can throw an exception if the length is > 10. – Walfrat Jun 16 '16 at 07:18
  • Your code shows an assignment, not a change to the object referenced by the obj`1 *variable*. The object didn't change at all. The variable now simply points to a different object. Why do you care that a new object was stored in the variable? If you have a long method that assigns different values to the same variable, you should fix the method, not try to detect assignments – Panagiotis Kanavos Jun 16 '16 at 07:25
  • @PanagiotisKanavos I have edited my question. – jeonggu Jun 16 '16 at 07:37
  • Provided the possible lengths are all quite short and you're happy to switch some of your syntax around, you may be able to achieve something horrific by abusing the generics system. But I'd severely recommend against going down this path. What you're trying just plain isn't how C# *should* work. – Damien_The_Unbeliever Jun 16 '16 at 07:47

1 Answers1

0

You can check the length of string in the constructor. If the length exceeds the limit, you can throw a new exception. On the object initiation side, you can use try catch block to check if the object is initiated correctly or not. Example:

class Class
{
    public Class(string string1)
    {
        if(string1.Length > 10)
            throw new Exception("Length Exceeded than limit");
    }
}

class Program
{
    static void Main()
    {
        Class obj;
        try
        {
           obj = new Class("stri");
        }
        catch
        {
           MessageBox.Show("Error");
        }
    }
}
Khuram Nawaz
  • 121
  • 1
  • 9
  • I edited my question. see again and give me a help. thank you. – jeonggu Jun 16 '16 at 07:39
  • @jeonggu According to your edit, This is not possible for me. I have many other possible solutions for your problem. If you provide me your problem briefly, its possible that I can give you a much better solution. – Khuram Nawaz Jun 16 '16 at 12:14
  • I got to know what I need is = operator overloading but it is not possible in c#. Thank you for your attention. – jeonggu Jun 17 '16 at 02:22