View Article

Data Types and Variables

By Xelence Documentation posted 07-02-2024 02:27

  

Introduction

In X#, all variables and expressions have a data type such as primitive or collection.

Primitive Data Types

 Data Type  Size  Description
Int16(short) 2 byte

Stores whole numbers ranging from -32,768 to 32,767.

Int32(int) 4 bytes

Stores whole numbers ranging from -2,147,483,648 to 2,147,483,647.

Int64(int) 8 bytes

Stores whole numbers ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Single(float) 4 bytes

Stores fractional number and is sufficient for storing up to 7 decimal digits.

Double(double) 8 bytes

Stores fractional number and is sufficient for storing up to 15 decimal digits.

Decimal(decimal) 16 bytes

Stores fractional number and is sufficient for storing up to 31 decimal digits.

Boolean(bool/Boolean) 1 bit

Stores either true or false.

Char(char) 2 bytes

Stores a single character, surrounded by single quotes.

String(string) 2 bytes per character

Stores a sequence of characters, surrounded by double quotes.

 

For example:

int myNum = 5;                                    /// Integer (whole number)

double myDoubleNum = 5.99D;         ///Floating point number

char myLetter = 'D';                         /// Character

bool myBool = true;                        /// Boolean

string myText = "Hello";               /// String

Object(object):

It is an instance of a class which is used to access its member or methods.

For example:

object lobjData = new();

lobjData = 10;

lobjData = "Ten";

Entity instance:

It is an instance of application entities which is used to access its member or methods.

For example:

entOrder lentOrder = new(); ///Object of the type entOrder

lentOrder.Update();             ///Object accessing its method

DateTime(datetime):

It is a data type that gives more information about date and time.

For example:

DateTime date1 = DateTime.Now();     ///Initializing to current date

ArrayList(arraylist):

It is a collection of objects whose size increases dynamically.

For example:

ArrayList arlist = new();

arlist.Add(2);

arlist.Add(“Steve”);

arlist.Add(true);

arlist.Add(4.5);

arlist.Add(null);

Hashtable:

It is a collection that stores unique key-value pairs.

For example:

Hashtable countryCity = new();

countryCity.Add("UK", "London, Manchester, Birmingham");

countryCity.Add("USA", "Chicago, New York, Washington");

countryCity.Add("India", "Mumbai, New Delhi, Pune");

Collection:

It is a group of related objects. In X#, you can create a collection of objects like entities.

For example:

Collection<entOrder> icblOrder = new();     ///Collection of the type entOrder

List:

It is a list of same type of objects that can be accessed using an index and iteration.

For example:

List<int> primeNumbers = new();

primeNumbers.Add(2);

primeNumbers.Add(3);

primeNumbers.Add(5);

primeNumbers.Add(7);

Dictionary:

It is a collection that randomly stores unique key-value pairs.

For example:

Dictionary<string, string> cities = new();

cities.Add("UK","London, Manchester, Birmingham");

cities.Add("USA","Chicago, New York, Washington");

cities.Add("India","Mumbai, Delhi, Pune");

Variables

A variable is a container that is used to store a data value with a specified type.


Rules for Variable declaration:

  • It can only contain alphanumeric characters. Special characters like #, $, etc. are not allowed. The only exception to this rule is _. 
  • It should always begin with a letter.
  • Multiple variables cannot be declared in a single statement. Keywords (such as int, or, string) cannot be used as a variable name.
  • Variable names are case sensitive i.e., variables myNum and mynum are different.

 Allowed Not Allowed
int lintDesign; int or;
string lstrDocument_1; string lstrDocument#;
double myDoubleNum; double 2myDoubleNume;

Note: Using descriptive names while declaring a variable is recommended to make it more readable.


Null and Initial Value


All variables are initialized to ‘null’ if they aren’t explicitly initialized. In this case, null means the absence of a value. You can also explicitly assign a null value to a variable of primitive data type.


For example:

string lstrDocument;


The value of lstrDocument is null. All value type variables are initialized to ‘null’ if they aren’t explicitly initialized. 

int lintDesign;


The value of lintDesign is 0. In this case, null means the absence of a value.


You can also explicitly assign a null value to a variable of primitive data type.

int lintDesign;

string lstrDocument = null;


You can also initialize the variable to any desired values as per business requirement.


For example
:

Collection<entOrder> icblOrder=new();

Note: You may get a null pointer exception if a collection is not explicitly initialized before it is used in the rule.


Scope of a Variable


The part of the program where the variable is accessible is called scope of the variable.


Rule Level scope


The variables that are declared within the rule, but outside the control flow statements like for, foreach, etc., can be directly accessed from anywhere in the rule.


For example:

int lintplace = 0;

               string lstrmedal;

               if (lintplace == 0)

               {

                              lstrmedal = "gold";

               }

               else

{

                              lstrmedal = "red";

               }

               lintplace=1;


Block Level scope


The variables are declared inside the control flow statements like for, foreach, etc., and can only be accessed within the loop statement where it is declared. 


For example:

int sum = 0;

for(int lintCount = 1; lintCount <11; lintCount ++)

{

               sum+= lintCount;

               }

Comments in X#


Comments are statements in X# that are not executed by the compiler but provide information or purpose of the variable, loop, or any block of code.


You need to enter ‘///’ to begin any single-line comment.


For example:

///Variable declaration

int sum = 0;


Also, an XML template gets added as a comment above the X# code when you create a rule.

 XML Element Description
<summary> Describes the rule
<param> Describes the parameter in the rule
<return> Describes the return value in the rule

Rules of Conversion

Numbers follow a hierarchy. So, variables of lower numeric type get implicitly converted when assigned to a variable of higher numeric type. For example, when a value of the data type ‘int’ is assigned to a ‘decimal’, the value gets stored without using any method.


For example:

decimal ldecPlace = 2;


However, if we assign a higher numeric type variable to a lower numeric type variable, it will lose its fractional value. For instance, when a value of the data type ‘decimal’ is assigned to an ‘int’, the value gets stored after losing the fractional value.


For example:

int lintPlace = 2.212   /// This will get stored as 2.


In the case of string data type, you need to explicitly convert one data type to another. For instance, a variable of the type int cannot be implicitly converted to a string. 


You must use the <variable_name>.ToString() method to do the same.


For example:

int lintPlace = 2;

string lstrMedal;

lstrMedal = lintPlace.ToString();


This post is part of the Rules topic. 
Click here to open the Rules Overview.


#Rules

#Xelence

0 comments
84 views