C# theory and the example plugin

This is where you talk about Plugins. How they are made and how they work, show examples.

C# theory and the example plugin

Postby beefy » Sat Nov 05, 2016 11:26 pm

Could anyone help me understand the following:

I’m trying to be a good boy and learn my C# theory. In classes we have instance variables and static variables.

An instance variable is declared if the static keyword in NOT used to declare it. Then we can only access / use that instance variable via an instance/object of the class.

So I create an instance variable:
bool beefyBool = true;

Then I declare a new instance of class UCCNCplugin:

UCCNCplugin beefysInstance = new UCCNCplugin();

To access beefsBool, don’t I have to do something like:

beefyInstance.beefysBool = false; // correct ?????????????

Yet in the plugin this is not so. For example the “firstrun” bool variable is accessed in

public void Loop_event()
{
if (firstrun)
{
firstrun = false;
…………………………….
…………………………….
…………………………….
}

(Tabbing / indentation disappears in the forum post).
There’s no instance accessing an instance variable. What am I not understanding.

Keith.
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Re: C# theory and the example plugin

Postby cncdrive » Sun Nov 06, 2016 12:36 am

An instance of that class is created by the UCCNC and the object is then passed to the plugininterface.
And then since the interface has an instance of an instancable class it can call it's public functions and could access it's public properties.

And yes, you have to access the non static variables inside a non static class with instancing the class, like

myclass myclassinstance = new myclass();
myclassinstance.myvariable ...

and you can access static functions of a class like:
myclass.myvariable ...

and you can't instance a non static class.

, but I think what you forgot about and what confuses you is that when for example the loop_event is called, then the code execution is already inside an instance of the UCCNCplugin class and therefor it's variables can be accessed from there. The UCCNC creates that object, the instance of the class to be able to call the functions of that class, since the UCCNCplugin is non-static it can be instanced and it's non static functions can be called only through an instance of the class. So, that the code execution is there makes it clear that the class was instanced, because other way the code execution could not get there, because the class and it's functions are non static. :)
cncdrive
Site Admin
 
Posts: 4695
Joined: Tue Aug 12, 2014 11:17 pm

Re: C# theory and the example plugin

Postby beefy » Sun Nov 06, 2016 6:37 am

Thanks very much Balazs.

I see you are working on a Sunday again, be careful your head doesn't explode, you are too valuable to us :P

I'm going to have to read that one a few times. It's certainly a different game learning basic C# to seeing how it works in the plugin.

Regarding this bit:

"and you can't instance a non static class"

Was that a typo and you meant it to be: "and you can't instance a static class".
I mean I read that a static class can only contain static variables and methods, and you cannot create an instance in them.

Could you explain this part of the example plugin a little bit:

public Plugininterface.Entry UC;

I've decompiled the plugininterface.dll and I see that Plugininterface is a namespace and Entry is a class of that namespace but don't quite understand what "public Plugininterface.Entry UC;" does.
I know every command in the plugin starts with UC. and that all those commands are in the plugininterface dll so I'm guessing it's a way of creating access to all those functions. For example would I be correct in saying we could write:

public Plugininterface.Entry COMMAND;

and then all the methods would be accessed with "COMMAND." example "COMMAND.GetLED(301);

Sorry to be a pain, just trying to learn and understand a few things.

Keith.
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Re: C# theory and the example plugin

Postby cncdrive » Sun Nov 06, 2016 7:04 pm

Hi KEith,

Yes, that was a typo, sorry.

Sure, you could name the UC whatever you want, e.g. COMMAND if that is better for you.
The UC as you can see is declared in the plugin as global variable, so you can ofcourse change it's name.
cncdrive
Site Admin
 
Posts: 4695
Joined: Tue Aug 12, 2014 11:17 pm


Return to Plugins

Who is online

Users browsing this forum: No registered users and 1 guest