I have a question regarding on returning the value of function in VB6.0..below is my code
Public Function tracePackageError(oPackage As DTS.Package) As String
Dim ErrorCode As Long
Dim ErrorSource As String
Dim ErrorDescription As String
Dim ErrorHelpFile As String
Dim ErrorHelpContext As Long
Dim ErrorIDofInterfaceWithError As String
Dim i As Integer
For i = 1 To oPackage.Steps.Count
If oPackage.Steps(i).ExecutionResult = DTSStepExecResult_Failure Then
oPackage.Steps(i).GetExecutionErrorInfo ErrorCode, ErrorSource, ErrorDescription, _
ErrorHelpFile, ErrorHelpContext, ErrorIDofInterfaceWithError
End If
Next i
End Function
how am i gonna return the value of my function? Please help :(
I think this would work for you...
Public Function tracePackageError(oPackage As DTS.Package) As String
Dim ErrorCode As Long
Dim ErrorSource As String
Dim ErrorDescription As String
Dim ErrorHelpFile As String
Dim ErrorHelpContext As Long
Dim ErrorIDofInterfaceWithError As String
Dim i As Integer
For i = 1 To oPackage.Steps.Count
If oPackage.Steps(i).ExecutionResult = DTSStepExecResult_Failure Then
oPackage.Steps(i).GetExecutionErrorInfo ErrorCode, ErrorSource, ErrorDescription, _
ErrorHelpFile, ErrorHelpContext, ErrorIDofInterfaceWithError
tracePackageError = ErrorDescription
End If
Next i
End Function
Try it out and let me know
Related
I got code for getting the first match. But the code does not get all matches. Can someone explain me how to do that? :)
Dim Start As Integer = 0, [End] As Integer = 0
If content.Contains(startString) AndAlso content.Contains(endString) Then
Start = content.IndexOf(startString, 0) + startString.Length
[End] = content.IndexOf(endString, Start)
Return content.Substring(Start, [End] - Start)
Else
Return String.Empty
End If
This works beautifuly for the first string and is easy to use. Just need to make it get a string array that i will use later (there are aprox. 5-10 strings that are always present between startString and endString).
Update:
Dim Searchstring As String
Searchstring = RichTextBox1.Text
Dim SearchStringPlus = Regex.Replace(Searchstring, "\s+", "+")
Dim SearchHTML As String
Dim WebClient1 As New Net.WebClient
SearchHTML = WebClient1.DownloadString("https://www.imdb.com/find?ref_=nv_sr_fn&q=" & SearchStringPlus & "&s=tt")
Dim SearchIndex As String = "<td class=""primary_photo""> <a href=""/title/tt"
Dim iSearch As System.IO.StreamWriter
iSearch = My.Computer.FileSystem.OpenTextFileWriter("iSearch.txt", False)
iSearch.WriteLine(SearchHTML)
iSearch.Close()
Dim SearchHTMLR As String
SearchHTMLR = IO.File.ReadAllLines("iSearch.txt").FirstOrDefault(Function(x) x.Contains(SearchIndex))
Dim titles As String
titles = GetStringBetween(SearchHTMLR, "<a href=""/title/", "/?ref_=fn_tt_tt_1")
MessageBox.Show(titles)
You can achieve this by using the Substring method.
Vb.Net
Private Function GetStringBetween(ByVal str As String, ByVal startStr As String, ByVal endStr As String) As String
Return str.Substring(startStr.Length, str.Length - (startStr.Length + endStr.Length))
End Function
C#
private string GetStringBetween(string str, string start, string end)
{
return str.Substring(start.Length, str.Length - (start.Length + end.Length));
}
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 :
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.
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()
I am trying to convert the following VB6 code to VB.NET:
Public Function SingleToHex(ByVal Tmp As Single) As String
Dim TmpBytes(0 To 3) As Byte
Dim TmpSng As Single
Dim tmpStr As String
Dim x As Long
TmpSng = Tmp
Call CopyMemory(ByVal VarPtr(TmpBytes(0)), ByVal VarPtr(TmpSng), 4)
For x = 3 To 0 Step -1
If Len(Hex(TmpBytes(x))) = 1 Then
tmpStr = tmpStr & "0" & Hex(TmpBytes(x))
Else
tmpStr = tmpStr & Hex(TmpBytes(x))
End If
Next x
SingleToHex = tmpStr
End Function
I tried to find a function in the "Conversions" namespace, but I did not find any.
Can anybody tell me how this can easily be done?
Public Function SingleToHex(ByVal Tmp As Single) As String
Dim arr = BitConverter.GetBytes(Tmp)
Array.Reverse(arr)
Return BitConverter.ToString(arr).Replace("-", "")
End Function
I am successfully converted structure data to string here in plain and (as suggested) in XML serialized way which obviously every has it's own good and bad side effects.
This is sample structure:
Public Structure myList
Dim a As String
Dim b As Integer
Dim c As Double
End Structure
Dim myInstance As New myList
myInstance.a = "Nemo"
myInstance.b = 10
myInstance.c = 3.14
Now I have 2 functions for converting structure data to string:
Dim xString As String = oStructToString(myInstance)
Public Function oStructToString(ByVal obj As Object) As String
Dim structString As String = ""
Dim i As Integer
Dim myType As Type = obj.GetType()
Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
For i = 0 To myField.Length - 1
structString &= myField(i).GetValue(obj)
If i = myField.Length - 1 Then Exit For
structString &= Convert.ToChar(161)
Next i
Return structString
End Function
Dim xString As String = xStructToString(myInstance)
Public Function xStructToString(ByVal obj As Object) As String
Dim x As New Xml.Serialization.XmlSerializer(obj.GetType)
Dim sw As New IO.StringWriter()
x.Serialize(sw, obj)
Return sw.ToString
End Function
But I can't get data back from string to structure.
Public Function oStringToStruct(ByVal xString As String) As Object
So I can call:
Dim mySecondInstance As New myList = oStringToStruct(xString)
Or
Dim mySecondInstance As New myList = xStringToStruct(xString)
How to do that?
So far I came to this:
Public Function xStringToStruct(ByVal xString As String) As Object
Dim x As New Xml.Serialization.XmlSerializer() ''<- what here?
Dim sr As New IO.StringReader(xString)
Return x.Deserialize(sr)
End Function
and this...
By help of har07 still one error remains here...
Public Function oStringToStruct(ByVal xString As String, ByVal type As Type) As Object
Dim structString() As String = xString.Split(Convert.ToChar(161))
Dim myType As Type = type.GetType()
Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
For i As Integer = 0 To structString.Length - 1
myField(i).SetValue(type, structString(i)) ''here crashes
Next i
Return type
End Function
Deserialize xml string back to struct is easier :
Public Function xStringToStruct(ByVal xString As String, ByVal type As Type) As Object
Dim x As New Xml.Serialization.XmlSerializer(type)
Dim sw As New IO.StringReader(xString)
Return x.Deserialize(sw)
End Function
You can use it like so :
Dim xObject As myList = xStringToStruct(xString, GetType(myList))
I am trying to generate a random code in vb.net like this
Dim r As New Random
Response.Write(r.Next())
But I want to generate code with 6 digits and should be alphanumeric like thie A12RV1 and all codes should be like this.
I have tried vb.net random class but I am unable to do that like as I want. I want to get the alphanumeric code each time when I execute the code. How can i achieve this in vb.net?
Try something like this:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Or you can do the common random string defining the valid caracters:
Public Function GenerateRandomString(ByRef iLength As Integer) As String
Dim rdm As New Random()
Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray()
Dim sResult As String = ""
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
I think this will suits your requirement,
Private sub GenerateString()
Dim xCharArray() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
Dim xNoArray() As Char = "0123456789".ToCharArray
Dim xGenerator As System.Random = New System.Random()
Dim xStr As String = String.Empty
While xStr.Length < 6
If xGenerator.Next(0, 2) = 0 Then
xStr &= xCharArray(xGenerator.Next(0, xCharArray.Length))
Else
xStr &= xNoArray(xGenerator.Next(0, xNoArray.Length))
End If
End While
MsgBox(xStr)
End Sub
Note: Tested With IDE
EDIT: Modified according to SYSDRAGON's Comment