Home Help Center

How to define uncertain variable type

hisensewxzhisensewxz Member wang xin zhongOrganization: HisenseProject: grid node
In c#,there is a variable type 'var',how to define it in ice?


  • matthewmatthew NL, CanadaMember Matthew NewhookOrganization: ZeroC, Inc.Project: Internet Communications Engine ✭✭✭
    Slice has no such construct. What is your use case?
  • hisensewxzhisensewxz Member wang xin zhongOrganization: HisenseProject: grid node
    I want to use it in a interactive system.the server can give me a variable.it may be int/float/string etc.
    how i achive this object use ice?
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    The best option is almost certainly to define separate operations:
    interface Example {
        int getIntValue(/*...*/);
        string getStringValue(/*...*/);
        // ...

    You can use Slice classes to get the effect of a union. For example:
    enum ValType { IntValue, StringValue /* ... */ };
    class ValueBase {
        ValType type;
    class IntegerValue extends ValueBase {
        int val;
    class StringValue extends ValueBase {
        string val;
    // etc...
    interface Example {
        ValueBase getValue(/*...*/);

    The receiver of the ValueBase looks at the enum to decide how to down-cast the instance.

    Which approach is better for you depends on your application. Be warned though that unions are often unnecessary and can be an indication of a half-baked design, so I'd carefully review the design before committing to this approach. (Unions tend to undermine the type system because they delay the decision as to what type is actually exchanged until run-time. Sometimes, that can be just what is needed. However, I quite often see unions that, with a bit of thought, could have been avoided, resulting in an easier-to-use system with stronger type safety.)


  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Also, if both your client and server are C# or .NET, you could simply pass this parameter "as-is" (provided it's serializable), without mapping it to a Slice type.

    See http://www.zeroc.com/doc/Ice-3.3.1/manual/Csharp.15.14.html

    Best regards,
  • hisensewxzhisensewxz Member wang xin zhongOrganization: HisenseProject: grid node
    thank you for your advice!
Sign In or Register to comment.