Using .NET Data Types

Must Read

Private Student Loans to Fund Your College Program

With the cost of college and university education rising everyday, students have to endure a greater burden when it...

Bad Credit Debt Consolidation Grants

Many people facing financial difficulties owing to large multiple debts search for free grants or government debt consolidation loans....

Financial Debt Settlement – Financial Debt Consolidation Solutions for Financial Debt Holders

When you are in debt, there is a tendency to think that there is no way to get out...

2008 Holiday Online Shopping Analysis

Depending on where you choose to get your news, you are getting many different reports on how retail and...

Credit Card Consolidation – Why Debt Settlement Might Be A Cheaper Option

Credit card consolidation is a method which is used by many consumers today as a result of owning a...

Lean Six Sigma Barreras

Times are changing and companies must change. Just because something works today does not mean that you work now.There...

When you work with .NET in IronPython, you have full access to every type that .NET supports. However, you don’t always create these types as you would in another language. For example, when working with C# or Visual Basic, you simply declare a variable of a certain type and then make an assignment to it. When working in IronPython, you must remember that making an assignment creates a Python type, not a .NET type. For example, let’s suppose you create a UInt32 variable and then make an assignment to it. Figure 7-10 shows the sequence of events that will occur.

As you can see, when you initially create the variable, IronPython recognizes it as a UInt32. In fact, even the type() function knows that this is a UInt32. When the code makes a simple assignment, however, notice that IronPython changes the type to a simple int. A check using type() shows that the data type has indeed changed. In order to change the value of a UInt32, you must make another assignment using the UInt32() constructor.

Figure 7-10: .NET and Python variables don’t mix very well in most cases.
Figure 7-10: .NET and Python variables don’t mix very well in most cases.

You can easily convert Python variables to their .NET counterparts in many cases. For example, you can use the following code to create a Python variable and then use it to create a .NET variable.


PVar = 5
NetVar = System.UInt32(PVar)


In some cases, you’ll find that a direct conversion won’t work for any of a number of reasons. For example, you might find that converting Python numeric variables won’t work in some cases because .NET is expecting a string. In this case, you can try the clr.Convert() method. Here’s an example of using clr.Convert() to produce the same results as the previous example. (Remember that you must type import clr before you can use it.)


NetVar2 = clr.Convert(PVar, System.UInt32)


The first argument contains the Python variable. The second argument contains the .NET data type you want as output. Of course, you must ensure you have imported the .NET assembly that contains the desired output type because the call will fail otherwise. In fact, any time clr.Convert() fails to convert the data, you get a TypeError.

Of course, before you can convert anything, you need to know the best type to use for the conversion. For example, you run the type() function against a Python variable to obtain the type information, but don’t know the equivalent .NET type. In this case, you can use clr.GetClrType() to obtain the closest .NET equivalent, as shown here.


# Produces System.Int32 as output.
# Produces System.String as output.


The clr.GetPythonType() method obtains the Python equivalent of .NET data types. For example, if you type clr.GetPythonType(System.Int32), you get int as output. Of course, you’ll want to know how to convert your .NET data into a Python equivalent. In this case, you simply rely on the Python functions you use to create variables of specific types. For example, the following code converts a .NET UInt32 into a Python long.


MyVar = long(NetVar)


Some .NET to Python conversions will result in data loss and Python won’t warn you about the problem. However, Python normally chooses a data type override that will work. For example, if you try to convert a number that won’t fit into an int, Python will automatically choose a long for you. Make sure that you always test a conversion before you assume it will work.


Latest News

Digital Marketing for Beginners

Digital marketing for starter, Let to basic learning about connecting with your audience in the right place at the...

More Articles Like This