C# code (C sharp code .net)
Please read the Terms of Use for Materials on ZennoLab
Table of contents
Description
This action allows you to insert pieces of code written in the popular programming language C# into the project and thereby repeatedly expand the functionality of ZennoPoster and the scope of its use.
C# is an object-oriented language, but this action does not take full advantage of this approach (classes, inheritance) and the code is executed sequentially, except for the use of classes and public variables from the using directive and shared code
How to add an action to a project?
Via context menu Add Action -> Custom code -> C# code
Or use smart search .
Where can it be applied?
Almost any action of the cubes can be replaced with similar ones executed in C#, thus accelerating the development and efficiency of code execution.
Use of any development in C# in your project.
Integration of third-party libraries and their application in code.
How to work with an action?
The " Custom C# Code " cube is a regular text editor with basic code highlighting.
You can use any project variables ( Variables processing ), and the result can also be saved in variables, text files, tables and databases. To use the methods and properties of the project, the project
entity is used, and to work with the browser, the instance
entity is used.
If you want to use a project variable in your code, you must call it like this - project.Variables ["counter"]
. Value, where counter is the name of the variable.
Context menu
Right-clicking on the cube window opens a context menu that has the following options:
Undo-Redo
Undoes the last change in the code. If the cancellation was done incorrectly, you can redo the canceled entry. It is important to note that these actions only work in the C # window code and do not cause changes in other cubes. For a similar function in the workspace for working with cubes, there are similar actions on the ProjectMaker toolbar.
Cut-Copy-Paste-Delete
Standard actions for working with code as text.
Comment-Undo comment
Code commenting is extremely useful functionality. Especially in large projects or when debugging code. Comments can contain information about changes, links, functionality of lines or parts of code. Comments can quickly enable/disable individual lines or pieces of code for error checking and testing. To comment out a part of the code, you need to select it and click this item.
Line numbering
Enables/disables line numbering, which is important for quick navigation through the code, for finding errors after receiving information about an error in the logs. But on small projects, numbering can be turned off to expand the workspace.
In the program settings, you can set behavior by default.
Wrap lines
Setting enabled
Setting disabled
Go to line
In large projects, it is important to quickly find the erroneous section of the code. Errors are displayed in the program logs. Clicking on this menu item opens a dialog box where you can enter the line number and column number. When you confirm the entry, the cursor moves exactly to the specified place in the cube code.
Search
Opens a search box for the code of this action. You can search based on parameters: case-sensitive or not, whole word, reverse search direction, and use regular expressions or wildcards when searching. By pressing the “ Find Next ” button, the cursor moves to the first found value, repeated pressing moves the cursor to the next found value, etc.
Replacement
Almost the same as “Search”, but immediately after finding the desired value, it is replaced with the entered value. It can work in a step-by-step mode, or immediately replace all found occurrences by clicking the "Replace All" button
Insert C# snippet
The entire contents of the selected file will be inserted into the place where the cursor is currently located.
Initially, this menu item is not displayed, so that it appears, you need to add at least one file to the C# Snippets Directory or save a code fragment using the Save in C# snippet function (described below).
Files in a directory can be moved to folders and thus it is convenient to group them.
Save snippet in C#
Allows you to save the selected code fragment to a TXT-file as a snippet for further use and quick insertion in other projects.
Set the value from the variable
When you hover the cursor over this context menu item, a list of all “Custom” and “Auto-generated” project variables opens. Selecting the required variable in the editor appears a construction of the form project.Variables ["myVar"]. Value
, which is the value of the variable myVar
.
This value always has a string type and conversion is necessary to use it as other types.
Do not return a value
Disabling this checkbox allows you to pass the result of code execution using the return statement
.
Save the result to the variable
From this list, you can substitute any variable into which the value will be saved according to the result of the return
execution, of course, if the checkbox from the previous paragraph is disabled.
Settings
To set the default settings for the C# cube, use this part of the project settings (Project settings)
Converting actions to code
It is important to note that ZennoPoster has functionality that allows beginners to quickly get used to C# and start using this language at the very beginning of their work with the program. Almost any action/cube can be converted to C# code and then work with the resulting code similar in functionality to the cube. To do this, after creating a cube and setting its properties, click on “ Convert to C# ” in the context menu and then paste the copied code into the “ C# code ” cube.
Debugging C #
In complex and large pieces of C#, it is often difficult to quickly find an error. Therefore, it is necessary to debug C# code with step-by-step monitoring of changes in variables and data in lists, tables and databases. As with the main ZennoPoster project, each C# action can be debugged in Project Maker by setting one or more breakpoints.
To add a breakpoint, click in the field to the left of the code editor opposite the required line. After that, by clicking "Next", we start the execution of the cube and then using the navigation in the panel above the code editor in step-by-step mode or in the mode until the next breakpoint we check the work of the code, look at the changes in the variables in Project variables and it helps to efficiently correct errors.
Examples of using
Learning to program in C# is beyond the scope of this document, however, it is possible to give you some tips and practical examples, which are often used in the practice of ZennoPoster users when working with C#.
Integer arithmetic
int value1 = Convert.ToInt32(project.Variables["value1"].Value);
int value2 = Convert.ToInt32(project.Variables["value2"].Value);
int value3 = value1 + value2; //или value1 - value2 or value1 * value2 etc.
return value3.ToString(); //the sum of two numbers
Rounding the result of division
float value1 = Convert.ToSingle(project.Variables["value1"].Value);
float value2 = Convert.ToSingle(project.Variables["value2"].Value);
return Math.Ceiling(value1/value2); // round up
//or
return Math.Ground(value1/value2); // round down
Create a list with random numbers from 1 to 10
var list = project.Lists ["numbers"]; // we refer to the list of project lists to get the essence of one of them.
list.Clear (); // clear the list before filling
int value; // explaining an integer variable
List <string> tempList = new List <string> (); // create a new list of strings, but this list exists only within this action and will be destroyed after the action for (int i = 0; i <10; = "" i ++) = "" {// loop = "" from = "" 10 = "" iterations = "" value = "i; // assign" value = "" counter = "" cycle = "" so that = "" not = "" change = "" it = "" value ++ ; // increase = "" by = "" 1 = "" value = "" variable = "" templist.add (value.tostring ()); // add = "" to = "" temporary = "" list = "" string = "" value = "" numbers = ""} // repeat = "" so = "" 10 = "" times = "" templist.shuffle (); // shuffle = "" list = "" list .addrange (templist); // add = "" to = "" result = "" list = "" mixed = "" list = "" numbers = "" from = "" 1 = "" to = "" 10] ] = ""> </ ac: plain-text-body> </ ac: structured-macro> <h3> Getting a random string from a file with access to accounts and splitting it into a username and password </h3> < ac: structured-macro ac: name = "info" ac: schema-version = "1" ac: macro-id = "a6c84dbf-782d-4da5-854b-93031e07d735"> <ac: rich-text-body> <p > The <code> return statement </code> can be used to return <strong> null </strong>. The <strong> null </strong> keyword is a literal representing a null reference that does not refer to an object. When <strong> null is </strong> returned, the <strong> C # </strong> action will exit on the red line, which is often useful for creating relationships with other cubes. In the example below, with an empty list of accounts, you can display a warning (although the same can be done inside <strong> C # </strong> using the <code> project.SendInfoToLog (& quot; Empty list & quot ;, true ); </code> method) and fill the empty list from TXT with new accesses. </p> </ ac: rich-text-body> </ ac: structured-macro> <ac: structured-macro ac: name = "code" ac: schema-version = "1" ac: macro-id = "dc6aca72-07c3-4c9b-8279-b60452f17731"> <ac: plain-text-body> <! [CDATA [IZennoList list = project. Lists ["accounts"]; // get a list with an attached TXT file in which accesses are stored line by line in the login: password format
if (list.Count == 0) return null; // If the list is empty, then exit the cube along the red line
Random rnd = new Random (); // create a random number generator
string str = list [rnd.Next (0, list.Count)]; // calculate a random purely from 0 to the number of elements in the list (not inclusive) and assign the value of the found index to a string variable
string [] arr = str.Split (':'); // form an array of strings by splitting the variable using a delimiter:
project.Variables ["login"]. Value = arr [0]; // take the first element of the array, which is the login (indices of arrays, lists always start from 0) and assign this value to the login variable
project.Variables ["password"]. Value = arr [1]; // the second element of the array will be the password
Getting a random string from a file with access to accounts and splitting it into a username and password
The return
operator can be used to return null. The null keyword is a literal representing a null reference that does not refer to an object. When null is returned, the C # action will exit on the red line, and this is often useful for creating relationships with other cubes. In the example below, with an empty list of accounts, you can display a warning (although the same can be done inside C# using the project.SendInfoToLog
("Empty list", true);
method) and fill the empty list from TXT with new accesses.
Working with HTML elements
Through C#, you can work with instace
object methods in the same way as with standard cubes, but at a higher level. In the example below, we get a collection of HTML elements and add links from their children to the list.
Working with files: getting the resolution (width x height) of an image
Working with CustomCode and Images: Overlaying a semi-transparent "watermark" in the center
And the actual SetImageOpacity
class, which needs to be inserted in the OwnCode.CommonCode
class