0

My goal is be able to print out a Structure as a receipt without using a SortedList.

Structure:

Private Structure Facture
    Dim noClient As String
    Dim nom As String
    Dim prenom As String
    Dim adresse As String
    Dim ville As String
    Dim province As String
    Dim codePostal As String
    Dim courriel As String
    Dim noTel As String
    Dim noProduit As String
    Dim marque As String
    Dim modele As String
    Dim annee As String
    Dim couleur As String
    Dim categorie As String
    Dim noSerie As String
    Dim prix As String
    Dim dateVente As String
End Structure

Structure Instance :

Private Function getFacture(ByVal ID As Integer) As Facture
    Dim receipt As Facture
    receipt.noClient = mtxtNoClient.Text
    receipt.nom = txtNom.Text
    receipt.prenom = txtPrenom.Text
    receipt.adresse = txtAdresse.Text
    receipt.ville = txtVille.Text
    receipt.province = CboProvince.Text
    receipt.codePostal = mtxtCodePostal.Text
    receipt.courriel = txtCourriel.Text
    receipt.noTel = mtxtNoTel.Text
    receipt.noProduit = mtxtNoProduit.Text
    receipt.marque = txtMarque.Text
    receipt.modele = txtModele.Text
    receipt.annee = mtxtAnnee.Text
    receipt.couleur = txtCouleur.Text
    receipt.categorie = txtCategorie.Text
    receipt.noSerie = txtNoSerie.Text
    receipt.prix = mtxtPrix.Text
    receipt.dateVente = dtpickerDateVente.Text
    Return (receipt)

End Function

And I need to print this along side an Array of Strings.

FORMAT :

Private Sub btnAfficherVente_Click(sender As Object, e As EventArgs) Handles btnAfficherVente.Click

    ' gotta read each line and print them as toString formatted for facturation 
    Dim infoProduit As String() = New String() {"No de client:", "Nom:", "Prénom:", "Adresse:", "Ville:", "Province:", "Code Postal:", "Courriel:", "Téléphone:",
        "No de produit:", "Marque:", "Modèle:", "Année:", "Couleur:", "Catégorie:", "No de série:", "Prix:", "Date de vente:"}

    'Output in facturation formatted text
    Dim i As Integer = 0
    Do While i < infoProduit.Length
        If i = 9 Then
            txtFacturation.Text += vbCrLf + "==============================================" + vbCrLf
        End If
        txtFacturation.Text += String.Format("{0, -20}", infoProduit(i)) & vbTab & getFacture(i).ToString & vbCrLf
        i += 1
    Loop
End Sub

OUTPUT :

enter image description here

I have looked at the documentation and multiple questions on StackOverflow. Most of the answers are in regards of using a Dictionnary, Collection or SortedList.

In the documentation, the only thing I found was to Override the ToString() in the Structure, which would no work for me because that would mean I have to individually format them in ToString but that would mess the final output since I have to print it along side an Array of Strings.

4
  • Your getFacture doesn't make sense. It ignores the ID-parameter. You are also calling it in the loop with the index as parameter but you just need to use the same, single instance of Facture because you want to get all fields of it. Commented Sep 26, 2018 at 14:26
  • You have to use ID to determine which property of the current facture to show. You don't need to create a new one every time and you can't just return the entire thing. For instance if ID is 0 return noClient, if ID is 1 return nom (and so on). Commented Sep 26, 2018 at 14:28
  • the getFacture(i).ToString() is wrong! you dpn't use ID param inside the getFacture() function. Why you don't use a dictionary? You need to use reflecection to get properties by index which may be not required! Commented Sep 26, 2018 at 14:29
  • So I should return an Integer which is the ID position Commented Sep 26, 2018 at 14:29

1 Answer 1

1

I would use a class instead of a structure. But you'll need a method inside the structure that will return the wanted text.

Private Structure Facture
    Dim noClient As String
    Dim nom As String
    Dim prenom As String
    Dim adresse As String
    Dim ville As String
    Dim province As String
    Dim codePostal As String
    Dim courriel As String
    Dim noTel As String
    Dim noProduit As String
    Dim marque As String
    Dim modele As String
    Dim annee As String
    Dim couleur As String
    Dim categorie As String
    Dim noSerie As String
    Dim prix As String
    Dim dateVente As String

    Function obtenirFactureTexte() As String

        Dim resultat As String = ""

        resultat &= "No de client: " & noClient & vbCrLf
        resultat &= "Nom: " & nom & vbCrLf
        resultat &= "Prénom: " & prenom & vbCrLf
        ' ...

        Return resultat
    End Function

End Structure

Sub Main()

    Dim nouvelleFacture As New Facture

    nouvelleFacture.noClient = "123"
    nouvelleFacture.nom = "Lagare"
    nouvelleFacture.prenom = "Bob"

    Console.Write(nouvelleFacture.obtenirFactureTexte())

End Sub

You can then use that method to output the result anywhere.

txtFacturation.Text = getFacture(i).obtenirFactureTexte()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.