New framework and Using

After avoiding the new framework for sometime, I have decided to dabble a bit by converting some code I have. I was reading the “Using Section” in which I understand it as to save on typing out the name space; however, I have questions on when it applies or not.

For example, in my below code, I call:

using Xojo.IO

which should give me access to BinaryStream in the new framework; however, I can not specify LockModes.Read as referenced in the documentation (LockModes.Read. I have to call it with the full name space of Xojo.IO.BinaryStream.LockModes.Read

Can someone explain?

[code]
using Xojo.Core
using Xojo.IO

dim bs as BinaryStream
dim dcmFile as new FolderItem(dcmStr.ToText)

if dcmFile.IsReadable = false then return false ’ check to see if file is readable
bs = BinaryStream.Open(dcmFile, Xojo.IO.BinaryStream.LockModes.Read) ’ Read file to memory
mbDCM = nil

mbDCM = if(bs.Length > 64132, bs.read(64132), bs.read(bs.Length))
bs.Close

mbDCM = mbDCM.Mid(128, mbDCM.Size - 128)
mbDCM.LittleEndian = true

return if(Xojo.Core.TextEncoding.UTF8.ConvertDataToText(mbDCM.Mid(0, 4)) = “DICM”, true, false)

exception
return false[/code]

You need to write BinaryStream.LockModes.Read, not LockModes.Read. An enumeration belonging to a class needs to be prepended by the class name.

Thank you Eli.

that did the trick.

In general, it has nsothing to do with your question but I found it very handy when I am learning the new framework.

I took a complete empty framework, created a module. I wrote all the framework code I learned in a method with a lot of comments. In this way, I can look back to what I learned in the past, copy and paste a particular instruction. Everything new I learn, I add to that method. Also everything is working. If I want to see the result, I comment out the msgbox in a particular section and I see the results.

It helped me very much to find my way around the new framework. Hopefully, you will also find a method to make things easier for you.

Chris

Chris,

This is a very useful best practice tip and thank you for sharing. I am going to ingrate this into my test environment.

Hello Rich,

You are welcome. The following code is what I have. I have put it in the “Open” event handler of the “App” class, so it is easy to add new functions and run it.

This is what I have :

[code] // This Dim must be before the “Using Xojo.IO” line
// to create a Classic FolderItem.
Dim f_CF_Map As FolderItem

Using Xojo.Core // Using the “Core” functions in the new framework
Using Xojo.IO // Using the “IO” InputOutput functions in the new framework

// Xojo.Core variables (to make the following code work as a demo)
Dim textExample As Text
Dim textCharacter As Text
Dim textReverse As Text
Dim textBlank As Text
Dim textResult As Text
Dim textCaseResult As Text
Dim textNewText As Text
Dim textTA() As Text
Dim textFifthCharacter As Text
Dim textSplit() As Text
Dim textArray() As Text
Dim textJoinText As Text
Dim textTrimText As Text
Dim textTrimResult As Text
Dim textChar As Text
Dim UintCodepoint As UInteger
Dim UintCP As UInteger
Dim intCompareResult As Integer
Dim intLen As Integer
Dim intPos As Integer
Dim blnResult As Boolean

// Xojo.IO variables
Dim textOutput As Text
Dim textRecord As Text
Dim f_Map As FolderItem // Xojo.IO FolderItem
Dim f_NF_Map As FolderItem // Xojo.IO FolderItem
Dim tosOutput As TextOutputStream
Dim tisInput As TextInputStream

//Dim f_NF_Map As xojo.IO.FolderItem

textExample = “Hello, World!”

// BeginsWith
blnResult = textExample.BeginsWith(“Hello”) // Returns True
blnResult = textExample.BeginsWith(“hello”) // Returns True
blnResult = textExample.BeginsWith(“hello”, Text.CompareCaseSensitive) // Returns False

// EndsWith
blnResult = textExample.EndsWith("!") // Returns True
blnResult = textExample.EndsWith(“World!”) // Returns True
blnResult = textExample.EndsWith(“world!”, Text.CompareCaseSensitive) // Returns False

// Show boolean result
//MsgBox Str(blnResult)

// Characters and Codepoints
For Each textCharacter in textExample.Characters
textReverse = textCharacter + textReverse
Next

// Show result (!dlroW ,olleH)
// MsgBox textReverse

// Codepoints
For Each UintCP in textExample.Codepoints
UintCodepoint = UintCP
// Show Results
// MsgBox Str(UintCodepoint) // Shows decimal value of each codepoint (character)
Next

// Compare
intCompareResult = textExample.Compare(“hello, world!”) // Value 0; matched
intCompareResult = textExample.Compare(“abc”) // Value 1; original text is greater than “abc”
intCompareResult = textExample.Compare(“Xojo”) // Value -1; original text is less than “Xojo”
intCompareResult = textExample.Compare(“hello, world!”, Text.CompareCaseSensitive) // Value -1
intCompareResult = textExample.Compare(“Hello, world!”, Text.CompareCaseSensitive) // Value -1

// Empty
blnResult = textExample.Empty // Returns False
blnResult = textBlank.Empty // Returns True

// Length
intLen = textExample.Length // Value 13; “Hello, World!”

// IndexOf (is ZERO based)
intPos = textExample.IndexOf(“World”) // Value 7 (start position)
intPos = textExample.IndexOf(“world”) // Value 7 (start position)
intPos = textExample.IndexOf(“world”, Text.CompareCaseSensitive) // Value -1 (case sensitive now)

// Left, Mid, Right
textResult = textExample.Left(5) // Hello
textResult = textExample.Right(6) // World!
textResult = textExample.Mid(7, 5) // Value World (Remember; Mid is ZERO based)

// Lowercase, Titlecase, Uppercase
textCaseResult = textExample.Lowercase // Value hello,world!
textCaseResult = textExample.TitleCase // Value Hello, World! (each word start uppercase)
textCaseResult = textExample.Uppercase // Value HELLO WORLD!

// Replace
textNewText = textExample.Replace(“World”, “Earth”) // Value Hello, Earth!
textNewText = textExample.Replace(“world”, “Earth”) // Value Hello, Earth! (case insensitive)

// Split, Join
textTA() = textExample.Split // Split ALL characters in individual array elements
// Split on 5th character (ZERO based)
textFifthCharacter = TextTA(4) // Value “o” (Position within array is ZERO based)
textSplit() = textExample.Split(",")
// Value textSplit(0) = “Hello”
// Value textSplit(1) = " World!"

// Join
textArray() = Array(“One”, “Two”, “Three”, “Four”)
textJoinText = Text.Join(textArray, “,”) // Value One,Two,Three,Four

// TrimLeft, TrimRight, Trim
textTrimText = " hello "
textTrimResult = textTrimText.Trim // Value “hello” (without quotes)
textTrimResult = textTrimText.TrimLeft // Value “hello " (without quotes)
textTrimResult = textTrimText.TrimRight // Value " hello” (without quotes)

// FromUnicodeCodepoint, Literal
textChar = Text.FromUnicodeCodepoint(224) // Value “” (without quotes)
textChar = &uE0 // Value “” (without quotes)

// Xojo.IO

textOutput = “How much would could a woodchuck chuck if a woodchuck could chuck wood”

// Writing to disk.
// Get a valid FolderItem
f_Map = SpecialFolder.Documents.Child(“Woodchuck.txt”)
// Creating textfile with TextEncoding UTF16
tosOutput = TextOutputStream.Create(f_Map, TextEncoding.UTF16)
// Writing to text file, with encoding UTF16
tosOutput.Write(textOutput)
// Closing TextOutputStream
tosOutput.Close

// Reading from disk
//Open file we created above
tisInput = TextInputStream.Open(f_Map, TextEncoding.UTF16)
// Reading the content of the file
textRecord = tisInput.ReadAll
// closing TextInputStream
tisInput.Close

// Creating a Xojo.IO.FolderItem from a FolderItem coming from the Classic FrameWork
f_CF_Map = GetOpenFolderItem("")
f_NF_Map = New Xojo.IO.FolderItem(f_CF_Map.NativePath.ToText)

MsgBox f_NF_Map.Path
[/code]

I download the webinars of Paul Lefevre which are very educational and nice to follow. However I had one problem, after viewing things get lost and I had to look them up again. So I thought by myself, I create and empty desktop project and add those code at a central place to it. It made things much easier for me.

So you should not only thank me but also Paul Lefevre which in first place teached me all those things.

Maybe it is a good idea if other people can also contribute, to add their findings also here, so we share with each other.

Thank you very much for your nice reply.

Chris

Another possible source of transition is the iOS Wrapper I came up with when iOS came about with the new framework.

I had existing code I wanted to incorporate but instead of rewriting it, I wrapped the new framework in methods that behave like the classic framework. Like Instr with IndexOf inside, or FromUnicodePoint inside Chr.

Although it was born in iOS, the actual module that contains all the methods can be used in any platform.

Just drag Wrapper.xojo_binary_code into your project. Since it often takes or return Text instead of String, you can probably readily use it as overload of the built-in methods.

https://github.com/Mitchboo/XojoiOSWrapper