Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Just wondering if this is "good practice" or not. I want to create a Token class that can hold a string or an integer, and process tokens of either type. I have looked at open generics, generics, abstracts, overriding etc. and chose simple solution below:-
C#
using System.IO;
using System;

public class Token 
{
    public dynamic Value {get; set;}
}

class Program
{
    static void Main()
    {   
        Token token1;
        Token token2; 
    
        token1 = new Token();
        token2 = new Token();
        
        token1.Value=1;
        token2.Value="hi";
        Console.WriteLine(token1.Value);
        Console.WriteLine(token2.Value);
        token1.Value=token2.Value;
        Console.WriteLine(token1.Value);
        Console.WriteLine(token2.Value);
    }
}


What I have tried:

?? as above - not sure what to put here ?
Posted
Updated 17-May-17 1:30am
v2
Comments
F-ES Sitecore 17-May-17 7:03am    
You're probably better going down the generics route. It's rare that you'll never really care about the type in question, and if all you're doing is printing the value to a console or log etc then you can have a base generic class and concerete classes that inherit that base class which provide their own ToString implementations

abstract class MyBaseClass<t>
{
public T Value {get; set;}
abstract string ToString();
}

class IntClass : MyBaseClass<int>
{
public string ToString()
{
return Value.ToString();
}
}

That lets you use any type and each concrete class can then provide its own implementation of ToString as you may want "T" to be a class like Person with complex properties.
ninjaef 17-May-17 8:52am    
I like this. It also consistent with base/simple types (int, int32, ...) not sure what you call them. Thanks. But, how do I declare an object of MyBaseClass and call .Value for both?
ninjaef 17-May-17 8:58am    
okay, but how do I declare an object to receive a token:

MyBaseClass token; //wouldnt compile?
...
...
token = getNextToken() // could return a string or an int

if <<token is int>> then ... else <<token is string>> ...

???
F-ES Sitecore 17-May-17 9:36am    
You would have to work with the specific derived concrete type or MyBaseClass<int> for example. Or have the class that needs to use getNextToken be a generic class also. There is no real way of handling completely type-agnostic data, you have to specify a type at some point, or have getNextToken return a fixed type such as a string, or an object but again the calling code needs to know what type to cast to. You have to think about the problem you're actually trying to solve, .net is a compiled language so it doesn't really have the concept of non-defined types.
ninjaef 17-May-17 9:57am    
the original ...

public class Token
{
public dynamic Value {get; set;} //compiles
}
Token mytoken = GetNextToken() // works runtime ; token.Value could yield an (int) or a (char) etc

if mytoken.Type = INTEGER then <<treat as int>> else <<treat as char>>

// so polymorphism?

...

as I say, all works as required, arguably easier than your solution, but I was wondering whether my solution was good practice - clearly its allowed in C# and compiles/runs

??

It depends what you want to do with it. Probably, I'd create an abstract Token class, and derive more concrete classes from that to represent what teh token types are supposed to do, or be used for:
C#
public class TokenNumber: Token ...
public class TokenUserName: Token ...
I'd then add abstract processing methods which each derived class has to implement. It's possible that this might be an Interface rather than an abstract class, but that would depend on what else you need to do with your Tokens.
Using a Dynamic variable as you are is basically the same as using an object:
public class Token 
{
    public object Value {get; set;}
}
Which defeats the strong typing that is one of the reasons C# is so much better than VB.
 
Share this answer
 
Using your solution, the read/write to/from Value would require a cast

...
public object Value {get; set;}
...
(int)Value
(string)Value

??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900