Вы находитесь на странице: 1из 10

Ring Documentation, Release 1.

We uses GetChar() Three times


The first time we get the user option
Option = GetChar()

But in the second and the third times (We accept the new line characters from the buffer)
GetChar() GetChar() # End of line

Example : when the user select the option number 1 then press ENTER
We have Three Characters
The first character is : Number 1
The second character is : CHAR(13)
The third character is : CHAR(10)
Because Windows uses CHAR(13) and CHAR(10) for each new line ( i.e. CR+LF )

68.33 How to use NULL and ISNULL() function?

when we try to use uninitialized variable in the Ring programming language, we get a clear runtime error message
Example
See x

Output
Line 1 Error (R24) : Using uninitialized variable : x
in file tests\seeuninit.ring

The same happens when you try to access uninitialized attributes


Example
o1 = new point
see o1
see o1.x
class point x y z

Output
x: NULL
y: NULL
z: NULL

Line 3 Error (R24) : Using uninitialized variable : x


in file tests\seeuninit2.ring

if you want to check for the error, just use Try/Catch/End


Try
see x
Catch
See "Sorry, We can't use x!" + nl
Done

Output

68.33. How to use NULL and ISNULL() function? 798


Ring Documentation, Release 1.3

Sorry, We can't use x!

Now we will talk about NULL and ISNULL()


Since we get error message when we deal with uninitialized variables
We can check these errors using Try/Catch/Done, So we uses NULL and ISNULL() for dealing with Strings.
NULL is a variable contains an empty string
ISNULL() is a function that returns true (1) if the input is an empty string or just a string contains NULL
This because we need to test these values (empty strings) and strings contains NULL that sometimes come from
external resource like DBMS.
Example
See IsNull(5) + nl + # print 0
IsNull("hello") + nl + # print 0
IsNull([1,3,5]) + nl + # print 0
IsNull("") + nl + # print 1
IsNull("NULL") # print 1

68.34 How to print lists that contains objects?

In this example we will see how we can print a list contains objects.
aList = [[1,2,3] , new point(1,2,3), new point(1,2,3)]
see "print the list" + nl
see alist
see "print the item (object)" + nl
see alist[2]
class point x y z
func init p1,p2,p3 x=p1 y=p2 z=p3

Output
print the list
1
2
3
x: 1.000000
y: 2.000000
z: 3.000000
x: 1.000000
y: 2.000000
z: 3.000000
print the item (object)
x: 1.000000
y: 2.000000
z: 3.000000

68.35 How to insert an item to the first position in the list?

To insert an item we can use the insert(aList,nIndex,Value) function.

68.34. How to print lists that contains objects? 799


Ring Documentation, Release 1.3

aList = 1:5
insert(aList,0,0)
See aList # print numbers from 0 to 5

68.36 How to print new lines and other characters?

To print new line we can use the nl variable.


See "Hello" + nl

or we can use multi-line literal as in the next example


See "Hello

"

if we want to print other characters we can use the char(nASCII) function


See char(109) + nl + # print m
char(77) # print M

68.37 Why we dont use () after the qApp class name?

When we use RingQt to create GUI application, we uses () after the class name when we create new objects for
example.
new qWidget() { setWindowTitle("Hello World") resize(400,400) show() }

but before doing that we create an object from the qApp class and we dont use () after that
Load "guilib.ring"
app = new qApp
{
win=new qWidget()
{
setwindowtitle(:test)
show()
}
exec()
}

Using () after the class name means calling the init() method in the class and passing parameters to this method.
If we used () while no init() method in the class we get the expected error message.
The class qApp dont have this method while the other classes have it because they need it to create an object using
a function that return a pointer to that object and this pointer will be stored in an attribute called pObject, for more
information see ring_qt.ring file which contains the classes.

68.38 Why the window title bar is going outside the screen?

When we write the next code

68.36. How to print new lines and other characters? 800


Ring Documentation, Release 1.3

Load "guilib.ring"
app = new qApp
{
win=new qWidget()
{
setwindowtitle(:test)
setGeometry(0,0,200,200)
show()
}
exec()
}

I would expect that the window will run at the point (0,0) with (200,200) size but the actual result is that the window
title bar is going outside the screen.
This is related to the behavior of Qt framework.
The next code will avoid the problem
load "guilib.ring"
new qApp {
new qWidget() {
move(0,0)
resize(200,200)
show()
}
exec()
}

68.39 How to create an array of buttons in GUI applications?

Check the next example:


Load "guilib.ring"

App1 = new qApp {

win1 = new qWidget() {


move(0,0)
resize(500,500)
new qPushButton(win1)
{
settext("OK")
setclickevent("click()")
}
btn1 = new qPushButton(win1)
{
setgeometry(100,100,100,30)
settext("Button1")
}

btn2 = new qPushButton(win1)


{
setgeometry(200,100,100,30)
settext("Button2")
}

68.39. How to create an array of buttons in GUI applications? 801


Ring Documentation, Release 1.3

button = [btn1, btn2]


show()
}

exec()

func click

button[1] { settext ("Button3") }


button[2] { settext ("Button4") }

68.40 How to Close a window then displaying another one?

This example demonstrates how to close a window and show another one
Load "guilib.ring"

app=new qApp
{
frmBefore=new Qwidget()
{
setWindowTitle("before!")
resize(300,320)
move(200,200)

button=new qPushButton(frmBefore)
{
setText("Close")
setClickEvent("frmBefore.close() frmMain.show()")
}

show()
}

frmMain=new Qwidget()
{
setWindowTitle("After!")
resize(300,320)
move(200,200)
}

exec()

68.41 How to create a Modal Window?

This example demonstrates how to create a modal window


load "guilib.ring"
app=new qApp
{

68.40. How to Close a window then displaying another one? 802


Ring Documentation, Release 1.3

frmStart=new Qwidget()
{
setWindowTitle("The First Window")
resize(300,320)
move(200,200)

button=new qPushButton(frmStart)
{
setText("Show Modal Window")
resize(200,30)
setClickEvent("frmModal.show()")
}

new qPushButton(frmStart)
{
setText("Close Window")
move(0,50)
resize(200,30)
setClickEvent("frmStart.Close()")
}

show()
}

frmModal =new Qwidget()


{
setWindowTitle("Modal Window")
resize(300,320)
move(200,200)
setparent(frmStart)
setwindowmodality(true)
setwindowflags(Qt_Dialog)
}

exec()

Related Documents
http://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html
http://doc.qt.io/qt-5/qt.html#WindowType-enum
http://doc.qt.io/qt-5/qwindow.html#setParent
http://doc.qt.io/qt-5/qt.html#WindowModality-enum

68.42 How can I disable maximize button and resize window?

Use the method setWindowFlags()


Load "guilib.ring"
app1 = new qapp {
win1 = new qwidget() {
setwindowtitle("First")
setgeometry(100,100,500,500)

68.42. How can I disable maximize button and resize window? 803
Ring Documentation, Release 1.3

new qpushbutton(win1) {
setgeometry(100,100,100,30)
settext("close")
setclickevent("app1.quit()")
}

new qpushbutton(win1) {
setgeometry(250,100,100,30)
settext("Second")
setclickevent("second()")
}

showmaximized()
}
exec()
}

func second
win2 = new qwidget() {
setwindowtitle("Second")
setgeometry(100,100,500,500)
setwindowflags(Qt_dialog)
show()
}

68.43 How to use SQLite using ODBC?

In Ring 1.1 and later versions we have native support for SQLite, so you dont need to use it through ODBC.
Also we can access SQLite through RingQt.
The answer to your question
pODBC = odbc_init()
odbc_connect(pODBC,"DRIVER=SQLite3 ODBC Driver;Database=mydb.db;LongNames=0;"+
"Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;")
odbc_execute(pODBC,"create table 'tel' ('ID','NAME','PHONE');")
odbc_execute(pODBC,"insert into 'tel' values ('1','Mahmoud','123456');")
odbc_execute(pODBC,"insert into 'tel' values ('2','Ahmed','123456');")
odbc_execute(pODBC,"insert into 'tel' values ('3','Ibrahim','123456');")
odbc_execute(pODBC,"select * from tel") + nl
nMax = odbc_colcount(pODBC)
See "Columns Count : " + nMax + nl
while odbc_fetch(pODBC)
See nl
for x = 1 to nMax
see odbc_getdata(pODBC,x)
if x != nMax see " - " ok
next
end
odbc_disconnect(pODBC)
odbc_close(pODBC)

Output:
Columns Count : 3

68.43. How to use SQLite using ODBC? 804


Ring Documentation, Release 1.3

1 - Mahmoud - 123456
2 - Ahmed - 123456
3 - Ibrahim - 123456

The program will create the file : mydb.db


Note : when I print the odbc drivers I see the long list that includes
SQLite3 ODBC Driver - UsageCount=1
SQLite ODBC Driver - UsageCount=1
SQLite ODBC (UTF-8) Driver - UsageCount=1

And Im using SQLite3 ODBC Driver.

68.44 Can I connect to dbase/harbour database?

You can connect to any database using ODBC


To connect to xbase files (*.DBF)
See "Using DBF Files using ODBC" + nl
pODBC = odbc_init()
See "Connect to database" + nl
odbc_connect(pODBC,"Driver={Microsoft dBase Driver (*.dbf)};"+
"datasource=dBase Files;DriverID=277")
See "Select data" + nl
odbc_execute(pODBC,"select * from tel.dbf")
nMax = odbc_colcount(pODBC)
See "Columns Count : " + nMax + nl
while odbc_fetch(pODBC)
See "Row data:" + nl
for x = 1 to nMax
see odbc_getdata(pODBC,x) + " - "
next
end
See "Close database..." + nl
odbc_disconnect(pODBC)
odbc_close(pODBC)

Output
Using DBF Files using ODBC
Connect to database
Select data
Columns Count : 3
Row data:
Ahmad - Egypt - 234567 - Row data:
Fady - Egypt - 345678 - Row data:
Shady - Egypt - 456789 - Row data:
Mahmoud - Egypt - 123456 - Close database...

Also you can connect to a Visual FoxPro database (requires installing Visual FoxPro driver)
See "ODBC test 6" + nl
pODBC = odbc_init()
See "Connect to database" + nl
odbc_connect(pODBC,"Driver={Microsoft Visual FoxPro Driver};"+
"SourceType=DBC;SourceDB=C:\PWCT19\ssbuild\PWCTDATA\CH1\Data\mydata.dbc;")

68.44. Can I connect to dbase/harbour database? 805


Ring Documentation, Release 1.3

See "Select data" + nl


see odbc_execute(pODBC,"select * from t38") + nl
nMax = odbc_colcount(pODBC)
See "Columns Count : " + nMax + nl
while odbc_fetch(pODBC)
See "Row data:" + nl
for x = 1 to nMax
see odbc_getdata(pODBC,x) + " - "
next
end
See "Close database..." + nl
odbc_disconnect(pODBC)
odbc_close(pODBC)

68.45 Why setClickEvent() doesnt see the object methods directly?

setClickEvent(cCode) take a string contains code. The code will be executed when the event happens.
Ring support Many Programming Paradigms like Procedural, OOP, Functional and others.
But when you support many paradigms at the language level you cant know which paradigm will be used so you have
two options
1. Provide General Solutions that works with many programming paradigms.
2. Provide Many Specific solutions where each one match a specific paradigm.
setClickEvent() and others belong to (General Solutions that works with many programming paradigms).
You just pass a string of code that will be executed without any care about classes and objects.
This code could be anything like calling a function, calling a method and setting variable value.
Some other languages force you to use OOP and call methods for events. Also some other languages uses anonymous
functions that may get parameters like the current object.
Now we have the general solution (not restricted with any paradigm), In the future we may add specific solutions that
match specific paradigms (OOP, Functional, Declarative and Natural).

68.46 Why I get Calling Function without definition Error?

Each program follow the next order


1 - Loading Files 2 - Global Variables and Statements 3 - Functions 4 - Packages, Classes and Methods
So what does that mean ?
1. **** No Functions comes After Classes ****
2. **** No command is required to end functions/methods/classes/packages ****
Look at this example
See "Hello"
test()
func test
see "message from the test function!" + nl
class test

68.45. Why setClickEvent() doesnt see the object methods directly? 806
Ring Documentation, Release 1.3

In the previous example we have a function called test() so we can call it directly using test()
In the next example, test() will become a method
See"Hello"
test() # runtime error message
class test
func test # Test() now is a method (not a function)
see "message from the test method!" + nl

The errors comes when you define a method then try calling it directly as a function.
The previous program must be
See"Hello"
new test { test() } # now will call the method
class test
func test # Test() now is a method (not a function)
see "message from the test method!" + nl

68.47 Can Ring work on Windows XP?

Ring can work on Windows XP and load extensions without problems.


Just be sure that the extension can work on Windows XP and your compiler version support that (modern compilers
requires some flags to support XP)
Check this topic https://blogs.msdn.microsoft.com/vcblog/2012/10/08/windows-xp-targeting-with-c-in-visual-studio-
2012/
For example, We added
/link /SUBSYSTEM:CONSOLE,"5.01"

To the batch file to support Windows XP


See : https://github.com/ring-lang/ring/blob/master/src/buildvccomplete.bat

68.48 How to extend RingQt and add more classes?

You have many options


In general you can extend Ring using C or C++ code
Ring from Ring code you can call C Functions or use C++ Classes & Methods
This chapter in the documentation explains this part in the language http://ring-
lang.sourceforge.net/doc/extension.html
For example the next code in .c file can be compiled to a DLL file using the Ring library (.lib)
#include "ring.h"

RING_FUNC(ring_ringlib_dlfunc)
{
printf("Message from dlfunc");
}

68.47. Can Ring work on Windows XP? 807

Вам также может понравиться