Basic4PPC Objects and libraries concepts - some general questions

TWELVE

Active Member
Licensed User
Hello,

even though i'm working a while with Basic4PPC and write since ~ 9 Months on my 5000+ lines project, i still don't understand some concepts of Basic4PPC.This might be due to the fact, that i'm not very familiar with .Net and it's concepts.There's a couple of questions i have, and this is the first one:

- Before i can use an object ( from an external lib) , i need to initialize it.In B4P this is usually done by : someobject.new(1)

- in other languages, i'm used to give the new object a name or a number upon creation, and if successfully created, the object gives back a pointer to it - sometimes.So creation of multiple instances of the same object is not a problem.The use of functions hosted in external libraries does not require to initialize these prior to the use, the compiler just needs to know where the function is located ( include etc.)

- why is this so in B4P...? Before the use of the external lib functions, i add these libs to the components and i also add the objects prior to the compile.So B4P knows everything that's necessary ?


- in B4P, there's no number for the object, so i can open / initialize it only one time ( except all built-in controls, like textbox etc.).I'm asking myself, how i could open 2 or more http requests at the same time using http lib..?

This here:

Response.Value = Request.GetResponse

has no number or indentifier on it.So this must be the one an only request ?
Or can i open another http request by just doing the same like with the first one:

Response.New1
Request.New1(URL)
...
Response.Value = Request.GetResponse
...
Response.Close

, while the first request is still open ?

Or could i open another http request / object in a thread ?

An even better example is the serial lib:

serial1.New2 (4,9600,"N",8,1)
serial1.PortOpen = true

For me does this clearly mean, that there can only be one comport handled - at a time.You can change the settings, close port and re-open the port, but not for 2 or more ports at the same time.Correct ? Or can this sequence called for more than one comport sequentially ( or even "at the same time" in a thread code) ? Like this ?

serial1.New2 (4,9600,"N",8,1)
serial1.PortOpen = true
serial1.New2 (5,9600,"N",8,1)
serial1.PortOpen = true
serial1.New2 (6,9600,"N",8,1)
serial1.PortOpen = true

I would expect to get an error here ( even though i didn't dare yet ;-) ), because THE serial port is already opened and not closed yet.Some libs appear to make a difference, e.g. threading lib allows to open / call more than one thread a the same time ( at least i believe that ;-) ).Until now this didn't bother me to much, but the more my project is growing, the more these code structures get obstructive.

- What does the .new do ? Is it just to reserve some space in the memory and initialize some arrays/structures needed by the library functions ? So do i need this only one time in a program run or is it gone by the .close and need to be called when the function is used/opened again ? Or do i have to understand the .new like an Open.Whatever() ? At least in the GPSDriver lib i need an .new and then still an Open ? Why does the Open not the job of .new upon first call ? Or is it like Init and Open ? If so, why do i need another response.new after a Response.Close ? Is the .new in http lib like an Init and Open at the same time ? Why do the libs have different concepts ?


I really appreciate if someone can enlighten me somehow :)


regards,

TWELVE
 

Cableguy

Expert
Licensed User
Longtime User
You can use as many objects of the same lib as you may wish or as it is possible..
ie, my ImagebuttonEX can be used to set several imagebuttons, as long as the objects name's are different...
I can add the same dll, to several objects...and then initialize them one by one...

- What does the .new do ? Is it just to reserve some space in the memory and initialize some arrays/structures needed by the library functions ? So do i need this only one time in a program run or is it gone by the .close and need to be called when the function is used/opened again ? Or do i have to understand the .new like an Open.Whatever() ? At least in the GPSDriver lib i need an .new and then still an Open ? Why does the Open not the job of .new upon first call ? Or is it like Init and Open ? If so, why do i need another response.new after a Response.Close ? Is the .new in http lib like an Init and Open at the same time ? Why do the libs have different concepts ?

still using my imagebuttonEX as an example, my "new" statement, wich is the constructor, translates to something like this:

public imgbtn
{
imgbtn = new System.Windows.Forms.Image;
}

Wich initializes the control and gives back to b4p the "pointer"....
 
Last edited:

agraham

Expert
Licensed User
Longtime User
The use of functions hosted in external libraries does not require to initialize these prior to the use, the compiler just needs to know where the function is located ( include etc.).
In .NET functions do not stand alone as in, say a C library. Everything in .NET is an object and methods (functions) are always implemented by, and called on, objects. Most objects in Basic4ppc are created, but NOT instantiated, at design time, like the built-in controls and instances of library objects. You can create new objects at Runtime by the appropriate Add.... statement for built-in objects or AddObject for library objects.
how i could open 2 or more http requests at the same time using http lib..?
You need two WebRequest objects, say Request1 and Request2, and deal with them separately.
For me does this clearly mean, that there can only be one comport handled - at a time.You can change the settings, close port and re-open the port, but not for 2 or more ports at the same time.Correct ?
Yes. A library could be written to handle more than one Com port at once but having a single object that handles a single Com port is more intuitive.
e.g. threading lib allows to open / call more than one thread at the same time ( at least i believe that ;-) )
Sorry, but it doesn't! Two simultaneous threads need two different Thread objects.

What does the .new do ?
New instantiates the object. That is it creates its instance in memory. In general it may also do additional initialisation as a convenience. For example controls have their parent and size specified when they are Newed although these can also be specified after by methods and properties. Until New is called the object name is just a pointer variable (in old money) waiting for a reference to an object to be assigned to it. New creates the object and assigns its reference (pointer) to the object variable of that name. In general New only needs to be called once on an object but unfortunately there are exceptions and occasionally it is is not a good thing to call New again on an object.

At least in the GPSDriver lib i need an .new and then still an Open ? Why does the Open not the job of .new upon first call ? Or is it like Init and Open ?
New creates the object that controls the GPS. Open and Close are commands to that object to Open and Close the GPS. This is a conceptual separation between the controlling object and what is being controlled.

[If so, why do i need another response.new after a Response.Close ? Is the .new in http lib like an Init and Open at the same time ?
In practice you don't need another New, you just need to assign another response from a WebRequest to the Value property of the WebResponse. In this case multiple News on the same object are benign.

Why do the libs have different concepts ?
Because they were written by different people at different times with different ideas and differing experience of .NET! It would be nice to go back and do a rigorous conceptual review of each library examining things like behaviour on multiple invocations of New but that would be far too big a task now.
 

TWELVE

Active Member
Licensed User
Thanks for your replies !

In practice you don't need another New, you just need to assign another response from a WebRequest to the Value property of the WebResponse. In this case multiple News on the same object are benign.

Ok, that's something i do wrong in my code, because the .new is called everytime a do a request/response.I doesn't seem to be harmful, because the code is running fine anyway.


You need two WebRequest objects, say Request1 and Request2, and deal with them separately

How would i do that..? Add the same object from lib under a new name ( object2, object3...) in IDE tools menu (static) or do i need to add the object during runtime using AddObject() (dynamic) ..or both..?

regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
Ok, that's something i do wrong in my code, because the .new is called everytime a do a request/response
It's not "wrong", its just that it is probably not necessary after the first New. Putting it where you have it is convenient and does no harm.

How would i do that..? Add the same object from lib under a new name ( object2, object3...) in IDE tools menu (static) or do i need to add the object during runtime using AddObject() (dynamic) ..or both..?
Either would do. If you do both, using the same name without Disposing of the first static object, you will get an error as you will be trying to make two instances of an object with the same name.
 

TWELVE

Active Member
Licensed User
Thanks Andrew, i'm getting a better picture, more and more..;-)

Is it correct, if i think of the .dispose as the opposite of .new ? Is .dispose generally there in every lib, or only in these they mention it in the help ? If there's no .dispose, does B4P/.net CF itself some cleanup to free up resources ? I'm used to use something like AllocateSomeThing and on the end FreeSomeThing for instance.



regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
Is it correct, if i think of the .dispose as the opposite of .new ?
Yes, that close enough. New creates a new instance of an object and initialises it. Dispose invalidates an object and makes it immediately available for garbage collection.
Is .dispose generally there in every lib, or only in these they mention it in the help ?
I think that Basic4ppc offers a Dispose method on everything but in practice only calls it if it exists in the library. Libraries don't have to implement Dispose unless they need to.
If there's no .dispose, does B4P/.net CF itself some cleanup to free up resources ?
Yes. The Framework periodically checks that all the current objects are accessible. In short this means that at least one usable reference (pointer) exists for each object. If not then the object cannot be used and is a candidate for garbage collection. So for normal .NET objects dispose is not needed, they are automatically collected at the end of their useful life.
I'm used to use something like AllocateSomeThing and on the end FreeSomeThing for instance.
The .NET Framework has tried to eliminate that, and the leaks and errors caused by it, wherever possible internally. However when dealing with the non-managed world it has to obtain and free native resources like Bitmaps and Graphics objects. Things that do this generally have a Dispose method for the programer to call and a Finalize method that the Garbage Collector calls when cleaning up. Between them their job is to free any native resources that need freeing, like open files, allocated memory, graphics object handles etc.
 
Top