-1

I'm sure about problem "types" in C #.

Suppose I have a class with the working name "item". This class has a field such as "variable". This field should match those of an element in my program, e.g. Boolean int16, int32, int64, double, uint, uint16.

Is there any possibility to redefine the type of a variable in dependency needs? Or is there any other approach to this problem?

I thought on the definition of this variable as var or object but then projecting it on a given type.

The problem is that the later the check when assigning values do not exceed the range?

  • 1
    Anything wrong with an `Item` type? I'm having a hard time understanding what you're asking for. – jdphenix Nov 27 '16 at 19:11
  • Please show some example code of how you would use this variable. – CodeCaster Nov 27 '16 at 19:16
  • you should look into generics – Jonesopolis Nov 27 '16 at 19:16
  • (some clarification on my question, since someone opted to turn my non-answer into one...) - What kind of problem are you trying to solve here? You're saying you need to change the type of a variable - why do you need to do so? What does doing that give you by way of solving your problem? – jdphenix Nov 27 '16 at 19:18
  • you need more info on "The problem is that the later the check when assigning values do not exceed the range?" .. is your actual issue with collections and OutOfRange Exception? or something like that? – Brett Caswell Nov 27 '16 at 19:54
  • I want create a language editor for LD (PLC). In my program, contacts, coils can take a Boolean variable. But functions can no longer accept any different types of variables. Accordingly, I would like to class 1 item can define various syntax elements of the language. For example, The ABS function can take real types, int, uint16, IN16. Sweeper help me a lot with his example . Thanks . – Michał Gałkowski Nov 27 '16 at 19:58

2 Answers2

0

You can use the generics. It will automatically create a class definitione in code behind when you create an object with a new type.

public class GenericClass<T>
{
    public T MyProperty { get; set; }

    public void TestMethod()
    {
        Console.WriteLine(MyProperty.ToString());
    }
}

Then you can use it with different type

        var myIntClass = new GenericClass<int>();
        var myStringClass = new GenericClass<string>();
        myIntClass.MyProperty = 1;
        myStringClass.MyProperty = "test";

        myIntClass.TestMethod();
        myStringClass.TestMethod();

You can also put constrains so that the generic class must be implementing a specific interface, be class, have constructor. public interface IPrintable { void Print(); }

public class GenericClassWithConstraint<T> where T : IPrintable
{
    public T MyProperty { get; set; }

    void Print()
    {
        MyProperty.Print();
    }
}

You may also check out the new keyword dynamic. It will allow you to work on an object in runtime

7h4ro3
  • 123
  • 1
  • 8
  • 1
    Explain how this works, and why it answers the OP's question. – Robert Harvey Nov 27 '16 at 19:14
  • it does address the concept of "Or is there any other approach to this problem?".. but doesn't really address the "The problem is that the later the check when assigning values do not exceed the range?" consideration.. which is the actual problem that needs more clarification by OP to address.. I suspect `List>` will be a scope to that.. but dunno yet. – Brett Caswell Nov 27 '16 at 19:51
0

You can either use generics, or dynamic, depending on how you want to use Item.

To use the generics approach, define Item as such:

class Item<T> {
    public T Variable { get; set; }
}

When you want an item whose Variable is an int, do this:

var intItem = new Item<int>()
// you can set the Variable property to an int now!
intItem.Variable = -1;

When you want an item whose Variable is a byte, do this:

var byteItem = new Item<byte>()
// you can set the Variable property to a byte
byteItem.Variable = 10;

And so on and so forth...

One feature of this approach is that the item's Variable's type cannot be changed once the item is created. So this is not possible:

intItem.Variable = "Hello";

If you want to change its type to something else without creating a new item, you should use a dynamic variable:

class Item {
    public dynamic Variable {get; set;}
}

You can now do something like this:

var myItem = new Item();
myItem.Variable = "Hello";
myItem.Variable = 10;

This is basically the same as defining Variable as object, but it saves your time casting between object and the desired type.

And regarding your worry about checking whether the value is out of range, it might be a little hard to check it if you use dynamic. But I did this little test and found that when the value overflowed, it will just wrap around:

var item = new Item();
item.Variable = byte.MaxValue;
item.Variable++;
Console.WriteLine(item.Variable); // prints 0
Sweeper
  • 213,210
  • 22
  • 193
  • 313