Report post

Are you sure you want to report the post shown below? This will send an email to the ATVO administrators. Please include a short reason for reporting.

Users reporting for no reason may be locked out.


Post

Posted by Nick Thissen
on
In any C#, you will that the first few lines always list a couple "using" statements. For VB scripts, the keyword is "Imports" instead of "using" but the concept remains the same.

A typical C# script will start with these lines:

using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;


To explain the need for these lines, you must understand the concept of namespaces and fully qualified names. Each object you use in the script has a certain type, and these types have names. The fully qualified type name is usually long and includes references to the project it lives in: the namespace. This avoids name conflicts between types that have the same name but are defined in different namespaces.

For example (not a real case, made up to make it more clear), there could be multiple types called Driver which live in different namespaces:
  • System.Printing.Driver: a driver for a printer.
  • System.Gamepad.Driver: a driver for a gamepad.
  • ATVO.Data.Driver: a race-car driver.

As you can see, each Driver type lives in a different namespace depending on the context. The namespaces are "System.Printing", "System.Gamepad" and "ATVO.Data".

For a script to make sense to the compiler, it must know the fully qualified name of each type including the namespace. However, scripts would become very long and hard to read if you had to write out the fully qualified name of every type in each case. For example, here is a simple C# script with all types using fully qualified names:
namespace Scripts
{
public class test : ATVO.ThemeEditor.Scripting.DotNET.IScript
{
public object Execute(ATVO.ThemeEditor.ThemeModels.ThemeContentItem item, object value, string parameter, ATVO.ThemesSDK.ISimulation sim)
{
ATVO.ThemesSDK.Data.Entity.IDriver driver = (ATVO.ThemesSDK.Data.Entity.IDriver) value;
return driver.ThreeLetterCode;
}
}
}


In this case there are several places that have to specify long type names:
  • ATVO.ThemeEditor.Scripting.DotNET.IScript
  • ATVO.ThemeEditor.ThemeModels.ThemeContentItem
  • ATVO.ThemesSDK.ISimulation
  • ATVO.ThemesSDK.Data.Entity.IDriver

To avoid having to type out the full name all the time, C# allows you to use a shortcut by placing the namespaces in the "using" directives at the top of the script. The using directive tells the compiler that you're going to be using types from this namespace frequently, and it allows you to write only the short name of the type instead of specifying the fully qualified name the entire time.

The same example from above can be written more clearly by introducing the using directives for the namespaces:
using System;
using ATVO.ThemesSDK;
using ATVO.ThemesSDK.Data.Entity.IDriver;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;

namespace Scripts
{
public class test : IScript
{
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
IDriver driver = (IDriver) value;
return driver.ThreeLetterCode;
}
}
}



How do you know in which namespace a type is defined?
When writing scripts you will frequently encounter the problem that the compiler requires the fully qualified type name of some type, but you don't know in which namespace the type is defined. The compiler will complain with the following error:
The type or namespace name '(your typename)' could not be found

This error means that you need to specify the fully qualified name (e.g. "ATVO.ThemesSDK.Data.Entity.IDriver" instead of "IDriver"), or add a using directive at the top for this namespace.

The Theme Editor can provide some assistance in finding the correct namespace for you. If you double-click these errors, you will see a popup window which will search all available code for the type name you are trying to use. It will then display a list of all matching results, showing the fully qualified name of the type. You can select the type name that is correct for your context, and then the textbox at the bottom will display the exact "using" directive you have to copy to the top of your script to fix the error. The image at the bottom of this post shows this popup in action.