Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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))

  • *Experts*
Posted (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 by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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.

  • Leaders
Posted

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).

[sIGPIC]e[/sIGPIC]
  • *Experts*
Posted (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 by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • Administrators
Posted

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
  • *Experts*
Posted
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.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...