How to create and use a simple function inside the c# Code

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
I wrote a piece of code inside the C# Code box and it works fine just that I need to repeat that so I wanted to encapsulate into a function so I can create a loop.



If I run this code I get this error:


Using
Код:
void testFunction()
in Visual Studio I get no errors at all. I know it's a simple syntax problem but I can't figure out what could be.
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 453
Благодарностей
5 912
Баллы
113
Function should be declared as public, not private. And called from snippet - Own code>C#
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
12
Баллы
18
Function should be declared as public, not private. And called from snippet - Own code>C#
The commented lines were previous versions of that function and I did tried to declare it as public.

So I guess the answer is that I cannot declare/create functions inside the C# code block but only in the OwnCodeUsings>Shared code and then call from from the C# code block? Is that correct?
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 453
Благодарностей
5 912
Баллы
113
  • Спасибо
Реакции: zenfreak

Nick

Client
Регистрация
22.07.2014
Сообщения
1 983
Благодарностей
817
Баллы
113
Well, you also actually can declare a delegate in one code block and then call this delegate from other blocks

Код:
//http://zennolab.com/discussion/threads/neponjatki-s-kontekstom.19667/
//define a function
project.Context["test"] = (Func<int>)(() => {
   return new Random(Global.Variables.MainRandom.GetNext()).Next(111, 999);
});

//call it from anywhere
int Result = project.Context["test"]();
 
  • Спасибо
Реакции: zenfreak

zenfreak

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

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