Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Dec 15, 2019 11:41 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: For Each Character As Char In TextArea1.Text
PostPosted: Tue Nov 15, 2011 1:06 am 
Offline
User avatar

Joined: Sun Jul 05, 2009 3:12 pm
Posts: 97
Location: Barrie, Ontario Canada
Dim ContainsAlpha As Boolean
I'm trying to get this converted to Real language but I'm stuck at: For Each Character As Char

Any ideas?

Dim ContainsNumeric As Boolean
Dim ContainsInvalidCharacter As Boolean

'Iterate through each character and determine if its a number,
'letter, or non number of letter.
For Each Character As Char In TextArea1.Text
If Char.IsNumber(Character) Then
ContainsNumeric = True
ElseIf Char.IsLetter(Character) Then
ContainsAlpha = True
ElseIf Not Char.IsWhiteSpace(Character) Then
ContainsInvalidCharacter = True
Exit For
End If
Next

If ContainsInvalidCharacter Then
Label1.text = "Good"
'Contains a non alpha or numeric character
ElseIf Not ContainsAlpha OrElse Not ContainsNumeric Then
Label1.text = "Bad"
'Doesn't have at least one of each.
End If


Top
 Profile  
Reply with quote  
 Post subject: Re: For Each Character As Char In TextArea1.Text
PostPosted: Tue Nov 15, 2011 1:50 am 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
You're treating TextArea1.Text as an array, but it's not: it's a string. You can create a String Array of the characters and iterate over it with For Each or you can iterate over the String itself using String.Len and Mid:
For i As Integer = 0 To TextArea1.Text.Len
Dim char As String = Mid(TextArea1.Text, i, 1)
If IsNumeric(char) Then
//The character is Numeric
Else
//The character is NOT numeric
End If
Next

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: For Each Character As Char In TextArea1.Text
PostPosted: Tue Nov 15, 2011 9:36 am 
Offline
User avatar

Joined: Fri Nov 10, 2006 4:10 pm
Posts: 1855
Location: Michigan, USA
Charonn0 is completely correct.

Just as a note, though, if Textarea.Text were an array (which it isn't), you wouldn't want to use the For Each.. As method of iterating through the array. It's mentioned in the documentation, but that iteration method is not a reliable way of going through an array when order is important. Yes, it will iterate through the array, but not necessarily from 0 to n, as it were. I don't know how the order is decided, but it's not like a standard For... Next loop — which guarantees going through the array sequentially.

Honestly, I almost never use For Each. I'm more likely to use a For... Next and type cast within the loop the object I want. More often then not, order is important to me.

Hope this helps. :)

_________________
Windows 8 x64
Windows XP Pro SP3
Ubuntu 11.04 via Virtual Box
RS Enterprise 2012r1.1

Programming Tutorials & Free Projects: http://www.JasonTheAdams.com
"Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton


Top
 Profile  
Reply with quote  
 Post subject: Re: For Each Character As Char In TextArea1.Text
PostPosted: Tue Nov 15, 2011 2:43 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Jason_Adams wrote:
It's mentioned in the documentation, but that iteration method is not a reliable way of going through an array when order is important. Yes, it will iterate through the array, but not necessarily from 0 to n, as it were. I don't know how the order is decided, but it's not like a standard For... Next loop — which guarantees going through the array sequentially.


I never knew that, but it's good to know.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: For Each Character As Char In TextArea1.Text
PostPosted: Wed Nov 16, 2011 11:15 pm 
Offline
User avatar

Joined: Sun Jul 05, 2009 3:12 pm
Posts: 97
Location: Barrie, Ontario Canada
Thanks for all the input. I appreciate it. I would like to test each character against a list of allowed characters like: "1234567890-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

If they are not in this list then an error is flagged.

This only allows me to check for Numbers and that could even be non-integers which messes things up a bit more:
If IsNumeric(char) Then
//The character is Numeric
Else
//The character is NOT numeric
End If


Top
 Profile  
Reply with quote  
 Post subject: Re: For Each Character As Char In TextArea1.Text
PostPosted: Thu Nov 17, 2011 12:52 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Use Instr() to check each character in the field.

Although you might be better off checking each character as they are entered (in KeyDown) and disallow those you don't want, so they don't get into the field in the first place.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group