lothos12345 Posted March 18, 2005 Posted March 18, 2005 Can anyone give me an example of how to rewrite the section of code, proper .NET format. For this string conversion. If Trim(TextBox1.Text) <> "" Then ttext = Replace(ttext, "<patientname>", StrConv(TextBox1.Text, VbStrConv.ProperCase)) If Trim(TextBox2.Text) <> "" Then ttext = Replace(ttext, "<patientssn>", TextBox2.Text) If Trim(TextBox5.Text) <> "" Then ttext = Replace(ttext, "<treatingdoctor>", StrConv(TextBox5.Text, VbStrConv.ProperCase)) Quote
*Experts* DiverDan Posted March 18, 2005 *Experts* Posted March 18, 2005 (edited) I'll try one: If Char.IsLetterOrDigit(TextBox1.Text) Then ttext = ttext.Replace(patientname, TextBox1.Text.Trim) 'there is no .ProperCase in true .Net but VB.Net does support .ProperCase although it's not the "true" .Net way If Char.IsLetterOrDigit(TextBox1.Text) Then ttext = ttext.Replace(patientname, StrConv(TextBox1.Text, VbStrConv.ProperCase)) Edited March 18, 2005 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
lothos12345 Posted March 18, 2005 Author Posted March 18, 2005 Problem Such a thing would work if Option Strict On was not set. This disallows conversions from string to char. Any work arounds, that does not involve turning Option Strict Off? Any help given is greatly appreciated. Quote
Leaders snarfblam Posted March 18, 2005 Leaders Posted March 18, 2005 If TextBox1.Text.Trim <> String.Empty Then 'Don't know if you want the replaced with text to be trimmed. ttext = ttext.Replace("<patientname>", TextBox1.Text.Trim) '<-- But it is here. End If This, like DiverDan's example, will not convert your string to proper casing. I do not know how to do this (I didn't even know that there was a VB function for this). Quote [sIGPIC]e[/sIGPIC]
*Experts* DiverDan Posted March 18, 2005 *Experts* Posted March 18, 2005 (edited) Option Strict On Dim patientname as String If Not TextBox1.Text = Nothing Then ttext = ttext.Replace(patientname, ProperCase(TextBox1.Text.Trim)) Nothing, "" and String.Empty are all the same for textural values Not and <> are also the same, but I prefer using Not as it reads better Public Function ProperCase(ByVal strText As String) As String Dim i, intLen As Integer Dim strTemp, strFinal As String Dim isSpace As Boolean strTemp = LCase(strText.Trim) intLen = strText.Trim.Length isSpace = True For i = 1 To intLen If Mid(strTemp, i, 1) = Chr(32) Then strFinal = Mid(strFinal, 1, i - 1) & Chr(32) & Mid(strFinal, i + 1) isSpace = True ElseIf isSpace Then strFinal = Mid(strFinal, 1, i - 1) & UCase(Mid(strTemp, i, 1)) & Mid(strFinal, i + 1) isSpace = False Else strFinal = Mid(strFinal, 1, i - 1) & Mid(strTemp, i, 1) & Mid(strFinal, i + 1) End If Next Return strFinal End Function the proper case function capitalizes the first letter of every word in strText. This maybe a problem with some names and addresses. Edited March 18, 2005 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Administrators PlausiblyDamp Posted March 18, 2005 Administrators Posted March 18, 2005 Why roll your own ProperCase function .Net provides a perfectly usable and culture aware one. Dim s As String = "helLo woRld" s = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s) as to the original code I would tend to do it like Dim ti As System.Globalization.TextInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo If TextBox1.Text.Trim <> String.Empty Then ttext = ttext.Replace("", ti.ToTitleCase(TextBox1.Text)) If TextBox2.Text.Trim <> String.Empty Then ttext = ttext.Replace("", TextBox2.Text) If TextBox5.Text.Trim <> String.Empty Then ttext = ttext.Replace("", ti.ToTitleCase(TextBox5.Text) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted March 19, 2005 Leaders Posted March 19, 2005 I was under the impression that a zero length string was not considered to be equal to nothing (because a zero length string could still be instantiated and hence not a null reference, i.e. nothing), but I checked and appearently I was wrong. Quote [sIGPIC]e[/sIGPIC]
*Experts* DiverDan Posted March 19, 2005 *Experts* Posted March 19, 2005 The reason that you would roll your own Case function is to add qualifiers for some names and addresses that do not fit the "Capitalize Every First Letter" format. True, however, that can also be done with another function using System.Globalization. John McCormink, Javier del Sorges, etc. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.