My beginnings in VB
Spoddysweg42
Posted messages
1
Status
Member
-
NHenry Posted messages 15235 Registration date Status Moderator Last intervention -
NHenry Posted messages 15235 Registration date Status Moderator Last intervention -
Hello, I'm posting my first message here to show you one of my first codes.
I've recently started programming in VB.NET, and I'm trying out coding.
I recently coded a small application that processes two strings to check if they are anagrams.
The application works very well, but a friend told me the code was "pretty poor." Any ideas on how to improve it?
I've recently started programming in VB.NET, and I'm trying out coding.
I recently coded a small application that processes two strings to check if they are anagrams.
The application works very well, but a friend told me the code was "pretty poor." Any ideas on how to improve it?
Option Explicit On Option Strict On Module Module1 Sub Main() Dim mot1, mot2 As String 'We declare two variables that will hold the words to test Console.Clear() Console.WriteLine("Enter two names to see if they are anagrams") Console.WriteLine("Or type 'exit' to quit the console") mot1 = Console.ReadLine() If mot1 = "exit" Then Exit Sub Else mot2 = Console.ReadLine() 'We will launch our function to test the words If f_is_anagram(mot1, mot2) Then Console.WriteLine("Yes, it's an anagram!") Else Console.WriteLine("No, it's not an anagram") End If Console.ReadLine() Main() End If End Sub Function f_is_anagram(ByVal mot1 As String, ByVal mot2 As String) As Boolean 'We use ByVal as we do not modify the data of variables mot1 and mot2 'We perform a length test of the words; if they do not have the same length, they are not anagrams Dim motTemp As String = mot1 If mot1.Length <> mot2.Length Then Return False End If 'We perform a test that takes the letters from word 1 to remove them from word 2 For i = 0 To mot2.Length - 1 motTemp = f_erase(mot1, Mid(mot2, i + 1, 1)) mot1 = motTemp Next If motTemp.Length = 0 Then Return True Else Return False End If End Function 'We pass a name and a letter and the function returns the name by removing the letter if it is present Function f_erase(ByVal pTexte As String, ByVal pLettre As String) As String Dim i As Integer = 0 Dim texte As String = pTexte While i < pTexte.Length If Mid(pTexte, i + 1, 1) = pLettre Then Return texte.Remove(i, 1) End If i = i + 1 End While Return texte End Function End Module
1 answer
Remove the automatic import of "Microsoft.VisualBasic" (project properties -> "references"), as this namespace is just a compatibility element that is only used for VB6 -> VB.NET transformations.
Mid(MaChaine, ...) -> MaChaine.SubString(...)
Another solution would be to take the letters and sort them in ascending order:
(Implicitly, Option Infer On)
(Code typed directly on the forum may contain errors)
Then you compare the two arrays (ignoring punctuation and spaces).
I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, remember to enable "Option Explicit" and "Option Strict"
Mid(MaChaine, ...) -> MaChaine.SubString(...)
Another solution would be to take the letters and sort them in ascending order:
(Implicitly, Option Infer On)
dim ltChaine1=(FROM lChar In MaChaine1.ToLower().ToCharArray() ORDER By lChar).ToArray() dim ltChaine2=(FROM lChar In MaChaine2.ToLower().ToCharArray() ORDER By lChar).ToArray()
(Code typed directly on the forum may contain errors)
Then you compare the two arrays (ignoring punctuation and spaces).
I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, remember to enable "Option Explicit" and "Option Strict"