Visual Basic Help ? | Sololearn: Learn to code for FREE!

+1

Visual Basic Help ?

Hello everyone. It great to be back on here. Please can anyone help me with this code fragment below ? Its in Visual Basic and I need to produce a flowchart of how it works for my project. So basically describing the functionality. Its only a small fragment and i'm hopeless at doing them. Any pointers would help me out a bunch, like how to do them properly and how to detail each of the steps. I also need to discuss the strengths and weaknesses of the code. So talk about the choice of programming language for this program, it's structure and style maybe ? Any of your help would be greatly appreciated 1 Public Class Form1 2 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 3 Dim I As Integer 4 Dim F As Boolean 5 Dim Ch As String 6 Dim Len As Integer 7 Dim FileWrite As System.IO.StreamWriter 8 Len = TextBox1.Text.Length 9 Ch = TextBox1.Text.ToCharArray() 10 F = True 11 FileWrite = My.Computer.FileSystem.OpenTextFileWriter("file.txt", False) 12 For I = 0 To Len - 1 13 If Not (Char.IsLetter(Ch(I))) Then 14 F = False 15 End If 16 Next 17 If (F = False) Then 18 MsgBox("Not valid name") 19 End If 20 If (F = True) Then 21 MsgBox("Name ok") 22 FileWrite.WriteLine(Ch) 23 End If 24 FileWrite.Close() 25 End Sub 26 End Class

10/10/2019 11:03:22 PM

harry

32 Answers

New Answer

+3

harry - Apologies for not responding sooner. I've been out on a college road trip with my daughter. Below is my revised code. -------- Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If (IsValidName(TextBox1.Text)) Then Try Using file = My.Computer.FileSystem.OpenTextFileWriter("file.txt", False) MsgBox("Name ok") file.WriteLine(TextBox1.Text) End Using Catch ex As Exception MsgBox($"Unable to save the value '{TextBox1.Text}' to file. {vbCrLf}See error message for more details: - {ex.GetType()} - {ex.Message}") End Try Else MsgBox("Not valid name") End If End Sub Public Shared Function IsValidName(name As String) Return Not Regex.IsMatch(name, "(\d|^\s*$)") End Function End Class

+6

harry As someone who hasn't worked in VB.NET since the first couple of years it was released, it hurts my eyes having to parse this today. 😅 That said, there are a few obvious issues with this code sample that stand out and is likely what the exercise is trying to highlight. I'll list the concepts for you to research and match up with this code: 1. No Try / Catch / Finally blocks in case the target file doesn't exist. 2. Opening a reference to a file handle is an unmanaged resource and won't be release from memory by the Garbage Collector (GC). You therfore need to either call Dispose() explicitly or wrap the usage in a Using statement. 3. Before invoking a method on the FileWriter, the object should be checked to see if it's Nothing. 4. Local method scoped variables should be using camelCase name format, rather than PascalCase. (continued...)

+5

5. The For loop variables don't need to be scoped outside of the looping block. This could be rewritten as: For i As Integer = 0 To TextBox1.Text.Length .... Next Then lines 3, 6, and 8 can be removed. 6. Lines 4, 10, and 13 - 15 should be removed. Lines 17 - 23 can be rewritten as: 17 If (ch.All(Function(c) => Not(c.IsDigit()))) Then 18 MsgBox("Name ok") 19 file.WriteLine(ch) 20 Else 22 MsgBox("Not valid name") 23 End If 7. A ForEach loop would be better than a For loop in iterating a Char Array. These should capture the gist of the problems being highlighted in this exercise. I hope these details were helpful.

+4

harry The only strengths I can see are... 1. This is so badly written, it's impressive. 2. This code could be rewritten in C# since it's .NET. BTW... I made some changes in my last answer for #6 line 17 and 19.

+4

Edward Thanks for the vote of confidence with my AMA. harry I did provide a lot of career advice in that AMA. I recommend you visit the link below, click the comments text, sort by [Most Recent], then scroll to the very bottom and work your way up. I tried to take my time in providing insightful and meaningful advice for people like yourself trying to get started. Hope this helps. https://www.sololearn.com/learn/17360/

+3

Indentation could help with readability. I haven't used VB for long time, but still would be nice to see a well indented code (easier to find problem). I'm afraid I can't help you with flowcharts, I'm bad at that. But I have a bit of suggestion. 1. In your for-loop, use `Exit For` to break out from the loop when you found a non alphabetical character. If you don't do this, you'll go all the way through the string end unnecessarily. 2. Instead of writing two blocks of If ... End If, Use If ... Else ... End If. Hth, cmiiw

+3

additionally Len is a function and is bad idea to use it as variable. The strong part of it is that it checks against invalid characters before writing to file, so some robustness included. But of course as others pointed ou it could be further improved.

+3

I can post that tomorrow. It's nearly 4am here. Gotta sleep.

+3

harry -------- Summary of Code Revisions: -------- 1. Added IsValidName() method using Regular Expressions 2. Added Try/Catch 3. Added Using block NOTE: I've verified this code will work. Let me know if you have any questions. I hope this helps. Also, if Regular Expressions don't make sense, you can use the alternative version of the IsValidName() method below: -------- Public Shared Function IsValidName(name As String) Return Not String.IsNullOrWhiteSpace(name) And name.All(Function(c) [Char].IsLetter(c)) End Function

+2

Thanks for all your help. I get it now. I'm a computing science student at college. And I had to study this code fragment and write all the ins and outs of it, so It wasn't part of any larger software program. Just 1 written example of an event.

+2

Edward Excellent points!

+2

don't worry, we all started at some point in time. David Carroll gave lots of programmers advices regarding professional work in the industry in his 24h AMA. Give it s search here in sololearn. You might begin thinking about it as shortcuts, and evolve from there. Object oriented programming is a paradigm, and VBA was build on it. You can do impressive things with a very good readability. And also perform very complex tasks like some machine learning tasks.

+1

I still don't understand why it would go from 0 to -1. Why subtract 1 ?

+1

harry It was my pleasure. If you have any thoughts on it, feel free to ask, me or someone else in the Community will try to help (as we possibly could of course).

+1

Ah thank you ! I don't know VB. Very well. Not even used in the tech industry anymore right ? I'm just updating my coursework now. Do you think there is any strengths of the code ?

+1

harry use a simple trick: in Excel you can record a macro. so hit the record button, do something like adding a formula, deleting a row, changing the color of a cell. Then go and look what was recorded. Eliminate the unnecessary things like screen scroll, selecting of cells you don't need, etc. Add some For-Next, some If-Then-Else-EndIf, some While-Wend or some Do-Loop and you are on the run. Understanding how the object orientation works it's very helpful. You might also benefit from tracing the object "Application", click on the "+" symbol in front of it and discover all sub-instances linked to it, like application.workbook("MyFile.xls").worksheet(1).cells(12,4).formular1c1 or apply commands like activecell.entirerow.delete or selection.interior.colour="blue”

+1

and very important: remember to save BEFORE you run the macro. There is no “undo” for a macro, unless you program it (and it is a pain in the a** programming it).

+1

Ah I see. So it's all shortcuts basically ? Thanks for all the help. You guys are great. I'm actually thinking of getting in touch with a software developing company soon to get some advice on how I can improve my employability in the industry for the future. I'm still a student and learning to program does look very intimidating at the moment

0

Thanks for the reply. What exactly is the For Loop doing on line 12 ?

0

It runs a check for each character in string <Ch> seeing whether if the character is a letter (assuming alphabetical). The loop variable <I> refers to the index of character in the string; from 0 to (length of string - 1). (Edited)