Home > Software design >  how to use it to add in-lined values or multiline strings in vb.net
how to use it to add in-lined values or multiline strings in vb.net

Time:07-30

I don't know create a multi line string please the best solution. The code below does not all use multi line strings. You can see the code I commented out was because I didn't know how to create a multi line string.

Thanks Jack outwithmycode 'information code

Public Shared Function GetReceipt(ByVal transaction_Conflict As Transaction) As String
            Return GenerateReceiptTemplate().Replace("<Orders/>", GetOrderRows(transaction_Conflict)).Replace("<Total/>", transaction_Conflict.Total.ToString() & "PHP").Replace("<Cash/>", transaction_Conflict.Cash.ToString() & "PHP").Replace("<Change/>", transaction_Conflict.Change.ToString() & "PHP").Replace("<Id/>", transaction_Conflict.Id).Replace("<Cashier/>", transaction_Conflict.GetCashier().Fullname).Replace("<Date/>", transaction_Conflict.Date.ToString())
        End Function

Private Shared Function GetOrderRows(ByVal transaction_Conflict As Transaction) As String
            Dim result As String = ""
            transaction_Conflict.GetOrders().ForEach(Sub(item As Order)
                result &= "<tr>"
                result &= "<td>" & item.GetProduct().Name & "</td>"
                result &= "<td align='center'>x " & item.Quantity & "</td>"
                result &= "<td align='right'>" & item.Subtotal & "PHP</td>"
                result &= "</tr>"
            End Sub)
            Return result
        End Function

 'code output in VB.NET
        
 Private Shared Function GenerateReceiptTemplate() As String
            Return "<center>" & vbCrLf &
                           "<font size='24px'><b>WcDonalds</b></font><br/>" & vbCrLf &
                           "<span>[email protected]</span>" & vbCrLf &
                       "</center>" & vbCrLf &
                       "<br/><br/>" & vbCrLf &
                       "<table width='100%'>" & vbCrLf &
                           "<thead>" & vbCrLf &
                               "<tr>" & vbCrLf &
                                   "<th align='left'>Product Name</th>" & vbCrLf &
                                   "<th align='center'>Quantity</th>" & vbCrLf &
                                   "<th align='right'>Subtotal</th>" & vbCrLf &
                               "</tr>" & vbCrLf &
                           "</thead>" & vbCrLf &
                           "<tbody>" & vbCrLf &
                               "<Orders/>" & vbCrLf &
                           "</tbody>" & vbCrLf &
                       "</table>" & vbCrLf &
                       "<br/>" & vbCrLf &
                       "<center>---------------------------------------</center>" & vbCrLf &
                       "<br/>" & vbCrLf &

            'Total: <b><Total/></b><br/>
            'Cash: <b><Cash/></b><br/>
            'Change: <b><Change/></b><br/>
            '               <br/>
            'Transaction ID:   #<Id/><br/>
            'Cashier: <Cashier/><br/>
            'Date: <Date/><br/>

            '               <br/>
            '               <center>---------------------------------------</center>
            '               <br/>

            '               <center><b>Thanks For visiting WcDonalds</b></center>





        End Function

CodePudding user response:

All right, all what you need here is the first function to generate and return the receipt. I'll modify the answer of your SOQ72747486

CodePudding user response:

You just type the String literal over multiple lines, with the opening double-quote on the first line and the closing double-quote on the last line. Just note that you'll need to push all but the first physical line to the left of the code window as any leading whitespace you include will be considered part of the String. E.g.

    Dim str = "First Line
Second Line
Third Line"

I can't recall exactly when this feature was introduced, so it may not be available in VB 2010. In that case, you can still use an XML literal, e.g.

    Dim str = <text>First Line
Second Line
Third Line</text>.Value

CodePudding user response:

Your HTML is a mess. This is just a little better.

    Dim xe As XElement = <div>
                             <p style="text-align: center; font-size: 24px;font-weight:700;">WcDonalds</p>
                             <p style="text-align: center;">[email protected]</p>
                             <table style="width: 100%;">
                                 <thead>
                                     <tr>
                                         <th style="width: 33.3%; text-align: left;">Product Name</th>
                                         <th style="width: 33.3%; text-align: center;">Quantity</th>
                                         <th style="width: 33.3%; text-align: right;">Subtotal</th>
                                     </tr>
                                 </thead>
                                 <tbody>
                                     <tr>
                                         <td style="width: 33.3%; text-align: left;">a product</td>
                                         <td style="width: 33.3%; text-align: center;">1</td>
                                         <td style="width: 33.3%; text-align: right;">$1.23</td>
                                     </tr>
                                 </tbody>
                             </table>
                             <br/>
                             <p style="text-align: center;">---------------------------------------</p>
                             <p>Total</p>
                             <p>Cash</p>
                             <p>Change</p>
                             <p style="text-align: center;">---------------------------------------</p>
                             <p style="text-align: center; font-weight:700;">Thanks For visiting WcDonalds</p>
                         </div>

    Return xe.ToString

CodePudding user response:

The text can be saved as an Embedded Resource (Text file) and retrieved from there. This helps one's code to look cleaner.

Try the following:

VS 2010:

Open Solution Explorer

  • In VS menu, select View
  • Select Solution Explorer

Open Properties Window

  • In VS menu, select View
  • Select Properties Window

Add Folder (name: Templates)

  • In Solution Explorer, right-click <project name>
  • Select Add
  • Select New Folder
  • Type desired name (ex: Templates)

Add Text File (name: ReceiptTemplate.txt)

  • In Solution Explorer, right-click on the new folder you just created (ex: Templates)
  • Select Add
  • Select New Item
  • Expand Common Items
  • Click General
  • Select Text File (name: ReceiptTemplate.txt)
  • Click Add

Set Properties for Text File

  • In Solution Explorer, click the text file that you added (ex: ReceiptTemplate.txt)
  • In the Properties Window, set Build Action to Embedded Resource

Add desired text to text file. For example:

ReceiptTemplate.txt:

<center>
  <font size='24px'>
    <b>ABC Cafe</b>
  </font>
  <br/>
  <span>[email protected]</span>
</center>

<br/>
<br/>

<table width='100%'>
    <thead>
        <tr>
            <th align='left'>Product Name</th>
            <th align='center'>Quantity</th>
            <th align='right'>Subtotal</th>
        </tr>
    </thead>
    <tbody>
        <Orders/>
    </tbody>
</table>
<br/>
<center>---------------------------------------</center>
<br/>

Total: <b><Total/></b><br/>
Cash: <b><Cash/></b><br/>
Change: <b><Change/></b><br/>
<br/>
Transaction ID:   #<Id/><br/>
Cashier: <Cashier/><br/>
Date: <Date/><br/>

<br/>

<center>---------------------------------------</center>
<br/>
<center><b>Thanks For visiting ABC Cafe</b></center>

Add a module (name: HelperLoadResource.vb)

'Notes: 
'        Need to set property Build Action: Embedded Resource For Each file that 
'        needs to be loaded
'
'Resources: 
'        https'stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file

Imports System.Text
Imports System.IO
Imports System.Reflection

Module HelperLoadResource

    Public Function ReadResource(filename As String) As String
        Return ReadResource(filename, System.Text.Encoding.UTF8)
    End Function

    Public Function ReadResource(filename As String, fileEncoding As System.Text.Encoding) As String
        Dim fqResourceName As String = String.Empty
        Dim result As String = String.Empty

        'get executing assembly
        Dim execAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

        'get resource names
        Dim resourceNames As String() = execAssembly.GetManifestResourceNames()

        If resourceNames IsNot Nothing AndAlso resourceNames.Length > 0 Then
            For Each rName As String In resourceNames
                If rName.EndsWith(filename) Then
                    fqResourceName = rName
                    Exit For
                End If
            Next

            If String.IsNullOrEmpty(fqResourceName) Then
                Throw New Exception(String.Format("Resource '{0}' not found.", filename))
            End If

            'get file text
            Using s As Stream = execAssembly.GetManifestResourceStream(fqResourceName)
                Using reader As StreamReader = New StreamReader(s, fileEncoding)

                    'read text
                    result = reader.ReadToEnd()
                End Using
            End Using
        End If

        Return result
    End Function
End Module

Note: This also works for newer versions of Visual Studio, however some of the steps listed in "Add Text File" are different.


The file/folder structure should look similar to the following:

enter image description here


Usage:

Dim receiptTemplate As String = HelperLoadResource.ReadResource("ReceiptTemplate.txt")

Resources:

  • Related