How to use custom c# function inside c# code block?

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
I have the following code that cleans the text for any special chars and I would like to use it in my c# code block

Код:
public static string RemoveSpecialCharacters(string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') {
         sb.Append(c);
      }
   }
   return sb.ToString();
}
I have tried adding this into my c# code block as it is and I get errors: CS1513, CS1519, CS1003.

Have also tried to add that code into the OwnCodeUsings>Shared code and then execute RemoveSpecialCharacters(myString) but this throws RemoveSpecialCharacters does not exist in the current context.
 

Nick

Client
Регистрация
22.07.2014
Сообщения
1 983
Благодарностей
817
Баллы
113
Just remove the first and last string, only the snippet inside the functin body is needed. But you also need to paste a value of one of the variables inside StringBuilder constructor, try doing right-click and pasting a variable name from the list.

You can also use it as a function but you need to use a Delegate for that. You register your delegate in one block and then can call it in other blocks. But that's an advanced feature, don't use it yet.
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
@Nick When you say to remove the first and last string you mean the first and last line from my code? If yes how can I use it like this: RemoveSpecialCharacters(myString)?
And by saying paste a value of one of the variables inside StringBuilder you mean that I should be using project.Variables["myString"].Value?

The Delegate function looks like what I am searching for so I'll try to learn more about it.
 

Nick

Client
Регистрация
22.07.2014
Сообщения
1 983
Благодарностей
817
Баллы
113
I meant,


StringBuilder sb = new StringBuilder(project.Variables["myString"].Value);
foreach (char c in str) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') {
sb.Append(c);
}
}
return sb.ToString();
 

LaGir

Client
Регистрация
01.10.2015
Сообщения
227
Благодарностей
927
Баллы
93
  • Спасибо
Реакции: azp и zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
@Nick Your solution is what I'm doing right now but because I need to use that code over and over again in different parts of my script I needed that as a function that I can call whenever I need. Pretty much what LaGir did in his example.
@LaGir This is exactly what I was looking for, you opened a brand new world for me. Now I can understand how to properly use OwnCode. Thank you!
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
After successfully using my RemoveSpecialCharacters function with OwnCode I tried to get the same result with other pieces of code that I was using in my c# blocks and tried this:



And getting this:

Do I need to initiate the instance somewhere in the OwnCodeUsings because when using the same code in my C#Code works out of the box.
 

LaGir

Client
Регистрация
01.10.2015
Сообщения
227
Благодарностей
927
Баллы
93
Do I need to initiate the instance somewhere in the OwnCodeUsings because when using the same code in my C#Code works out of the box.
You can create field or property in your class (by the way, project works in the same way):
2017-05-02_16-20-52.png


And in the first C#-action of your template you need to initiate them:
2017-05-02_16-20-07.png



There is another way. If you need to use "instance" or "project", you can just pass them to your method as arguments:

2017-05-02_16-30-04.png


2017-05-02_16-31-53.png
 
  • Спасибо
Реакции: zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
@LaGir I have basically begged a few days ago for this info you have provided - http://zennolab.com/discussion/threads/how-to-include-run-external-c-script-into-my-project.36319.

You just made my day :-)

Don't want to be a pusher or anything but there's only one more thing I need in order to start the real coding. If you could tell me real quick how to debug the code I'm writing in c# code blocks or CodeCreator with help from Visual Studio I would be forever in debt with you. I tried to explain my request here: http://zennolab.com/discussion/threads/debugging-codecreator.36966/ but it's pretty simple - I need to debug zenno c# code.
 

LaGir

Client
Регистрация
01.10.2015
Сообщения
227
Благодарностей
927
Баллы
93
@zenfreak, you can debug your scripts in VS for using in Zenno, but you need to remember that it does not support last versions of C#.

For example, your code from that topic:

Let's take this line of code for example:
C#:
MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
This line is perfectly valid in VS but in Zenno it generates an error: CS056 "Unexpected character '$''.
Code examples like this
C#:
string k = "example";
return $"Some {k} of text";
is a short variant of String.Format method:
C#:
string k = "example";
return String.Format("Some {0} of text", k);
Variant with "$" appeared in C# 6.0. Zenno doesn't support it, so you need to redo your code with String.Format.

About debagging in CodeCreator - in last versions breakpoints work the same as in its earlier versions. It's better than in ProjectMaker, but worse than in VisualStudio.
 
  • Спасибо
Реакции: zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
@zenfreak, you can debug your scripts in VS for using in Zenno, but you need to remember that it does not support last versions of C#
What is the current version of C# supported by zenno and is it that hard for the developers to upgrade zenno to the latest version of C#?

I was trying to replicate the debugging method described here: http://zennolab.com/discussion/threads/code-creator-and-visual-studio-2015-debug-mode.28386/#post-203278 where it says that you could debug CodeCreator C# code directly in Visual Basic but couldn't "connect" them. I find this the best approach to debugging as VS offers a complex and complete solution.

 

LaGir

Client
Регистрация
01.10.2015
Сообщения
227
Благодарностей
927
Баллы
93
What is the current version of C# supported by zenno and is it that hard for the developers to upgrade zenno to the latest version of C#?
This question is for the developers. But it seems there is a fifth version.
I was trying to replicate the debugging method described here: http://zennolab.com/discussion/thre...ual-studio-2015-debug-mode.28386/#post-203278 where it says that you could debug CodeCreator C# code directly in Visual Basic but couldn't "connect" them. I find this the best approach to debugging as VS offers a complex and complete solution.
In Visual Studio, it's better to debug the main logic of your templates, and work with the instance is better done in CodeCreator, imho. It is possible to emulate the instance and other things in VS, but there are many difficulties, and it will still be crutches. I know that some users do this, but I'm not, so unfortunately I can't give any advice.

PS: About the screenshot - it seems you need to connect DLLs from "Progs" directory of ZennoPoster to your project (like Global.dll, ZennoLab.CommandCenter.dll, ZennoLab.Emulation.dll, ZennoLab.InterfacesLibrary.dll).
 
  • Спасибо
Реакции: zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
PS: About the screenshot - it seems you need to connect DLLs from "Progs" directory of ZennoPoster to your project (like Global.dll, ZennoLab.CommandCenter.dll, ZennoLab.Emulation.dll, ZennoLab.InterfacesLibrary.dll).
So basically I need to add the references (Global.dll, ZennoLab.CommandCenter.dll, ZennoLab.Emulation.dll, ZennoLab.InterfacesLibrary.dll) to my project.

Today I tried going even further and managed to do the following:
I found this great library for converting docx files to html - https://github.com/mwilliamson/dotnet-mammoth
I have downloaded the library files and created the Mammoth.dll by building the project.
Created a VS project and added the Mammoth.dll reference and then I used it like this:



Everything is running as it should and I'm getting the html version of my .docx file.

Now I went on moving this to Zenno so I did the following:

1. Added the Mammoth.dll to the GAC references:

2. Added my C# Method to the Shared code window:


3. Called the htmlConverter method from within the c# code box:


And got this error:


My guess is that the Mammoth library loads its own references and that I also need them to be added here but I have 2 questions:

Why is it running in VS and not in Zenno and how do I find those references? (I did look for them and couldn't find any)
 

PHaRTnONu

Client
Регистрация
01.10.2016
Сообщения
340
Благодарностей
48
Баллы
28
this was teh exact problem i ahd in the newest version 11 roll back to 10.7 and test im sure it will work fine
 
  • Спасибо
Реакции: zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18

PHaRTnONu

Client
Регистрация
01.10.2016
Сообщения
340
Благодарностей
48
Баллы
28
Yeah it's not loading the embedded dependicys inside the dull or something funky idk I haunt had time to look into it recently probably get to it this week...
 

Кто просматривает тему: (Всего: 2, Пользователи: 0, Гости: 2)