Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet377/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   373   374   375   376   377   378   379   380   ...   453
366

CHAPTER 17
ExtEnding ironPython Using VisUal Basic.nEt
FIgURE 17-5: 
Add the TestCalcs.py file to the solution.
Creating the IronPython Application
It’s time to write code to test 
Calcs.DLL
. Listing 17-2 shows the code you’ll use for testing purposes.
LISTINg 17-2: 
 Testing the extension using IronPython
# Add a reference to the CLR
import clr
# Obtain access to the extension.
clr.AddReferenceToFile(‘Calcs.DLL’)
import Calcs
# Create an instance of the class and fill it with data.
Value1 = Calcs.Calcs(10)
# Print the original value, then decrement and increment it.
print ‘Original Value1 Content: ‘, Value1 
print ‘Value1 + 1: ‘, Value1.Inc() 
print ‘Value1 - 1: ‘, Value1.Dec()
# Create a second value and display it.
Value2 = Calcs.Calcs(5) 
print ‘\nOriginal Value2 Content: ‘, Value2
# Use the two values together in different ways.
print ‘\nValue1 + Value2 = ‘, Value1 + Value2
print ‘Value1 - Value2 = ‘, Value1 - Value2
print ‘Value1 * Value2 = ‘, Value1 * Value2
548592c17.indd 366
2/24/10 12:49:34 PM
www.finebook.ir


Using Visual Basic.NET for User Interface Support
 

 
367
print ‘Value1 / Value2 = ‘, Value1 / Value2
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
The code begins by importing the Common Language Runtime (CLR). It then uses the 
AddReferenceToFile()
method to create a reference to 
Calcs.DLL
. The final step is to import 
the 
Calcs
code.
Visual Basic automatically creates a hidden namespace for you. Even though the 
namespace is hidden in the Visual Basic editor, you must still use it when accessing 
the class in IronPython. Consequently, you see the 
Calcs
 accessed as 
Calcs 
.Calcs
, where the first reference is the namespace and the second is the class.
Before the code can use the 
Calcs
code, it must create an instance of it, 
Value1
. Notice that
the code calls the 
Calcs.Calcs()
constructor with an initial value. Any time you want to assign
a value to 
Value1
, you must use the 
Calcs.Calcs()
constructor. If you were to assign a value 
using 
Value1 = 15
, it would change the type of 
Value1
. A consequent addition, such as
Value1 + Value2
, would produce the following error:
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘Calcs’
One way to overcome this problem would be to override the = operator.
After the code creates 
Value1
, it demonstrates the use of the 
Inc()
and 
Dec()
methods. These two 
methods simply add or remove 1 from the value of 
Value1
. If you want to change the actual value of 
Value1
, you need to make 
Value1
equal to the output of the method like this:
Value1 = Value1.Inc()
The next step is to create 
Value2
, a second 
Calcs
object you can use for binary operations. The 
code outputs the initial value of 
Value2
. The remainder of the example demonstrates the use of the 
various operators. As you can see, they work precisely as you would expect. You could even use 
them to create a third value like this:
Value3 = Value1 + Value2
Figure 17-6 shows the output from this example. Except for the absence of the 
++
and 
--
operators, 
everything works much as you would expect.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   373   374   375   376   377   378   379   380   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish