Defining a new object from another namespace class.

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

Defining a new object from another namespace class.

Postby beefy » Sun Jun 18, 2017 12:27 am

Hi Balazs,

(or anyone else who can explain this).

Trying to get a deeper understanding of the example plugin. At the beginning we have this line:

Code: Select all
public Plugininterface.Entry UC;


My understanding is that this creates a new instance/object of class "Entry" of the namespace "Plugininterface".

From my C# studies, I've only encountered this way of creating a new object of a class:

Code: Select all
Entry UC = new Entry();


However, those types of example were in the same namespace, so I would expect the correct way to be:

Code: Select all
Plugininterface.Entry UC = new Plugininterface.Entry();


or something like that. So that's why I'm confused about the way the plugin example creates the UC object and adds the "public" access modifier too:

Code: Select all
public Plugininterface.Entry UC;


So this sounds to me like a "public object" is being created, but I've never heard of that so far. Have I got it completely wrong.

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

Re: Defining a new object from another namespace class.

Postby cncdrive » Sun Jun 18, 2017 2:02 am

Hi Keith,

public Plugininterface.Entry UC;


This defines a Plugininterface.Entry type of object named UC and the visibility of the object is public.
The object is not instanced yet with this statement, you only defined your object.

Entry UC = new Entry();


This is a code where you create an instance of an object.
This is the same as:

Plugininterface.Entry UC = new Plugininterface.Entry();


You just did not type out the full type identifier and generally speaking this short typed out code can work if you have a using Plugininterface; include directive, or if your class is also in the Plugininterface namespace and if there is no other class definition names Entry, I mean if there was then you must type out the full type identifier otherwise there was a distinguish between the 2 same type short names.
The compiler is clever enough that you don't always have to type out the full type of your objects, you can shorted it if there is no distunguish, I mean if there are no others included with the same class name in different namespaces, however the compiler also clever enough to find these kind of issues and it will give you an error if there is a distunguish, if for example there is Plugininterface.Entry class type and there is also e.g. Myclass.Entry class type included and so visible, and so if you will try to define an object like Entry Myobject; then that will give an error telling you that the compiler can't distunguish between the 2 types, so it will be unable to decide which object type you will want to create and so will give you an error.
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Defining a new object from another namespace class.

Postby beefy » Sun Jun 18, 2017 3:00 am

Much appreciated Balazs,

hey you should be relaxing on a Sunday :P (not that I am).

I try not to bother you with general C# questions, but I could not find the answer in books or Google. Anyway, very happy to have learned a little bit more about how it all works. I'm a lot further ahead than when I first started.

Cheers,

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


Return to Plugins

Who is online

Users browsing this forum: No registered users and 9 guests