问题
I am a Blue Prism RPA Developer and I want to create methods in my global code and call them from other code stages. In Global code info page, it is stated that this could be done but i could not find a satisfactory resource on the subject. Can anyone share a syntax rule set, maybe a sample global code and code stage code sniplets or guide me in the direction? I use C# and i would much appreciate responses in C#
Note: I am not from a developer background, i have elementary coding know how but i am not entirely knowladgeable about OOP subjects (namely: Classes, Methods, Constructors, Inheritence etc)
I tried declaring one class and one method, that worked, i could call the method but when i tried to add new method and or class it failed, it did not compile with the overall code
回答1:
If I understand your question correctly, you are looking for an example of a class containing one or more methods, that you can place into the Global Code stage of the Initialise page of your BP Object, and then be able to create/call instances of that class/method in your code-stages from the other pages in that BP Object.
I don't know how much you know, so I am going to assume every step needs to be explained.
Since you are using C#, your first stop should be the Code Options tab. Here you should reference any libraries you intend to use (.dll) on the top pane, and their respective namespaces in the bottom pane. BP already includes some basic ones as shown below. It is also very important to change the Language selection to C# (bottom left drop-down), as Visual Basic seems to be the default option:
Next, here is an example of a simple concrete class with some fields, a constructor, a property and a method. You can place this code inside the Global Code window as-is:
public class SomePerson
{
//Class variables
private string _firstName;
private string _lastName;
//Constructor
public SomePerson(string firstName, string lastName)
{
this._firstName = firstName;
this._lastName = lastName;
}
//Property
public string FullName
{
get
{
return string.Format("{0} {1}", this._firstName, this._lastName);
}
}
//Method
public string Hello()
{
string myText = "Hello "+FullName+", it is nice to meet you.";
return myText;
}
}
Now you will be able to call instances of this class from inside code-stages and use the property and the method. For example, you could supply the FirstName and LastName in a couple of data items in BP, and then use an instance of the SomePerson class property to get the FullName using this code:
SomePerson Anyone = new SomePerson(firstName, lastName);
fullName = Anyone.FullName;
Similarly, you could use the method as follows:
SomePerson Anyone = new SomePerson(firstName, lastName);
result = Anyone.Hello();
You could try all this out using a layout like this one:
And basically... that's it!. In this manner you can create as many classes (concrete or abstract) and interfaces as you need, just stack them up inside the Global Code pane as you did with this one.
Finally, make sure you understand that most VBOs (like the Excel one) are written in Visual Basic, so chopped-off stuff will not compile together with C# code; you must use one or the other. Yes, they are both .NET languages, but once you have chosen the BP Object language, you must write your code in that language.
回答2:
A good example of a VBO with a global code could be MS Excel VBO. It contains several functions and methods.
Protected Function GetWorksheet(Handle As Integer, _
WorkbookName As String, _
WorksheetName As String) As Object
Return GetWorksheet(Handle,WorkbookName,WorksheetName,True)
End Function
or
Protected Sub CloseInstance(Handle As Integer, SaveWorkbooks As Boolean)
...
End Sub
And that code can be later used in Code Stages
Dim ws as Object = GetWorksheet(handle, workbookname, worksheetname)
CloseInstance(handle, savechanges)
Additionally, every Code Stage is actually a method and you can call it from other code stages.
For example, if we have a Code Stage called "Create Instance" in MS Excel VBO, that contains no inputs, and only one output (integer), then we can call it using this code:
Create_Instance(out)
That's also why the code stages names must be unique.
来源:https://stackoverflow.com/questions/57065468/declaring-and-calling-methods-in-blue-prism-global-code-stage