Google+
Join free weekly webinars
Find us on
Back

Error in snippets: Cannot implicitly convent type int to ulong. Any explicit conversion exists?

Tags: snippets, C#, C# types

Introduction

You may have seen this error message or it variations while compiling your snippets in EventIDE. Or, you will probably see it soon. What is it about?  In this post I will explain why this error appears and how to get rid of it.

In the present version of EventIDE a modern programming language, C#, is used for snippet coding. Nowadays, C# is successor of C and C++ in professional programming community. Similar to C++ and other, C# is statically-typed language. It means that you have to define explicitly a type for each of your variables (and you can not change it afterwards). There any many types in C# that you can flexibly choose, for example one of 9 different types just the integer numbers. Knowing in advance what type is a variable helps compiler to generate a fast code and efficiently manage a memory. This is why the  statically typed languages are popular among professionals.

In other hand, statically typed languages are less comfortable in use. You always have to remember what variables types you introduced and manage type conversions if necessary (for example, when different numerical types are met in the same expression). If you do it wrong, you get an error message similar to one in the header of this post. Then you need to take care of type conversion.

There are several methods for type conversion in C#:

Implicit conversion

This is the easiest one, since C# compiler makes all job itself. Your types are converted automatically in the following cases:

 
/* Small to large number conversion */
/*****************************/
byte byt=5; // works
int num=byt; // works
long longnum=num; //works
double fnum=longnum; // works
num=longnum // does not work !
 
 
 
 
/* Number to string conversion if the number is in a composite string expression*/
/**************************************************************************/
int num=5;
double fnum=5.0;
string stri=fnum; // does not work ! 
stri=num+"+"+fnum+"=?"; // works producing a composite string
stri=num+""; // works producing a string with the number
stri=num; // does not work !
 
Implicit conversion works only in limited cases but it is exception-safe: you won’t get runtime exception.

Explicit conversion

if a conversion cannot be made without a risk of losing information, the compiler requires that you perform an explicit conversion. It could be done either by type cast or by a set of converting functions of the system Convert class:

 
 
/* type casting */
/****************/
double fnum=45.8;
int num=(int) fnum; // cast double to int with resulting num=45 (notice flooring in the conversion)
 

 

 
 
/* Converting functions of the Convert class */
/***********************/
 
double fnum=45.8;
int num=Convert.ToInt32(fnum); // converting double to int with resulting num=46 (rounding in the conversion)
string stri=Convert.ToString(fnum); // converting double to string
stri=Convert.ToString(fnum,"0.00"); // formatted conversion
long longnum=2345678;
byte byt=Convert.ToByte(longnum); // is ok for compiler but but will cause a runtime exception 
                                  // because the longnum value is outside of the byte range 0..255
 

 

 
/* Inner converting function: turn any type to string */
/**************************************************/
double fnum=45.8;
int num=23;
byte byt=255;
string stri=fnum.ToString(fnum); // inner converting function of the fnum variable
stri=fnum.ToString(fnum,"0.00"); // inner function with formatting string
stri=num.ToString()+":"+byt.ToString(); // inner functions in a composite string
 

Explicit conversions are recommended in the most of the cases but they are unsafe, since can cause runtime exceptions if the converted value does not fall in the proper range. Use them carefully.

Conversions of EventIDE proxy variables

In EventIDE you may need use conversions with the proxy variables attached to properties of the events and elements.  To learn exactly what type has a particular proxy/property, check the documentation about the used event or elements. In general, the following C# types are most frequently used in EventIDE:
 
  • ulong(unsigned long integer) type for timing properties, like Duration, Trigger Time and so on.
  • floattype for coordinates and sizes in visual degrees
  • intfor position/sizes in pixels
  • doubletype for ratios, parameters and range properties, like a contrast ratio or playback position
  • byte type for a single color channel

 

Mind these types and apply appropriate conversions when you use the proxy variables in your snippets.
 

Useful links

  1. Casting and Type Conversions (C# Programming Guide)
  2. List of converting functions of the Convert Class (MSDN)
  3. C# value types (MSDN)