0% found this document useful (0 votes)
14 views4 pages

Visual Basic Program Examples and Code

Uploaded by

merrymutish03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Visual Basic Program Examples and Code

Uploaded by

merrymutish03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Use Table 1 to write a program to input mark attained by a student then output grade in
a given course unit using select case. Attach the code in a command button click event.

Figure 1: Command button click event with code.

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles


[Link]

End Sub

Private Sub btnProcess_Click(sender As Object, e As EventArgs) Handles


[Link]
Dim score As Integer
score = [Link]
Select Case score
Case 0 To 39
[Link] = "E"
Case 40 To 49
[Link] = "D"
Case 50 To 59
[Link] = "C"
Case 60 To 69
[Link] = "B"
Case 70 To 100
[Link] = "A"
Case Else
[Link] = "invalid score entered"
End Select

End Sub
End Class
2. Write a Visual Basic program to output the following series 1, 2 . . 99, 100 using Do
While...Loop.

Figure 2: Do While Loop Number Series

Public Class Form1


Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles [Link]
Dim i As Integer = 1

Do While (i <= 100)


[Link](i)
i=i+1
Loop

End Sub
End Class
3. Using With … End With statement write a subprogram that will set Text1 properties as
follows; font size =14, bold, color = red, text= “ Hello World”
(4 marks)

Public Class MainForm


Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles
[Link]
End Sub
Private Sub SetTextProperties()
With Text1
.Text = "Hello World"
.FontSize = 14
.Font = New Font(.Font, [Link])
.ForeColor = [Link]
End With
End Sub
End Class

4. Write a program code in Visual Basic using a CASE statement to output the type of
award of an employee given the number of years worked.

Module MainModule
Sub Main()
Dim yearsWorked As Integer

'Input the number of years worked


[Link]("Enter the number of years worked: ")
yearsWorked = [Link]([Link]())

'Determine the type of award based on the number of years


worked
Dim awardType As String = GetAwardType(yearsWorked)
'Output the type of award
[Link]("Employee's Award Type: " & awardType)

[Link]()
End Sub

Function GetAwardType(yearsWorked As Integer) As String


Select Case yearsWorked
Case Is >= 15
Return "Gold"
Case Is >= 5
Return "Silver"
Case Is >= 2
Return "Bronze"
Case Is > 2
Return "None"
Case Else
Return "No Award"
End Select
End Function
End Module

Common questions

Powered by AI

The use of a Visual Basic program to assign grades automatically illustrates automation’s key role in enhancing educational efficiency and accuracy. Automating grading minimizes human error, speeds up administrative processes, and allows educators to focus on more qualitative aspects of student interactions, such as feedback and personalized instruction. This representation of automation in education underscores a broader trend towards integrating technology in learning environments to optimize resources and maximize educational outputs .

Both programs utilize a default case label to address invalid input, such as negative or extremely high scores in the grading program. While functional, these mechanisms could be improved by explicitly validating inputs before they reach the Select Case statements, providing immediate and precise feedback to the user about invalid entries. For instance, checking input ranges prior to processing and using input sanitization can prevent extraneous cases where defaults handle these errors. Enhancements could include informing users of specific input errors and allowing for corrections, which enhances usability and overall user experience .

The Do While loop used in the Visual Basic program executes the loop body as long as the condition is true, which is appropriate for the task of outputting a continuous series until a specified limit is reached. Compared to a For loop, which explicitly manages the loop counter and endpoint, Do While offers flexibility in dynamically evaluating the exit condition. However, in this specific application where a known, fixed number of iterations is needed, a For loop might be marginally more efficient by reducing overhead in condition checking by setting bounds explicitly .

The program uses a Select Case statement to assign grades based on score ranges. However, it doesn't have specific conditions to handle negative scores or scores greater than 100 except for returning 'invalid score entered'. For negative scores, the program will correctly output 'invalid score entered'. For scores greater than 100, it also returns 'invalid score entered'. Despite handling these errors with the default case, it is a logical issue in terms of score validation outside realistic value ranges .

The code uses a With…End With statement to set multiple properties of Text1, which is efficient in terms of reducing repeated object references. However, improvements could involve adopting expressions or methods that allow for reactive changes based on user interactions, particularly in modern UX design practices. For better efficiency, ensuring properties such as Font and Color are contextually set based on current application theme or user settings could be beneficial. Organizing such settings externally, say in a configuration file, can also enhance flexibility and modernization in maintenance without hard-coding values .

The GetAwardType function uses a Select Case statement to check the value of yearsWorked, assigning awards based on descending thresholds. It first checks if yearsWorked is 15 or more for 'Gold', then 5 or more for 'Silver', then 2 or more for 'Bronze'. Importantly, the check Case Is > 2 results in 'None', confusingly placed after Case Is >= 2 due to redundancy as the previous case already captures all values >= 2. Therefore, having Case Is > 2 is unnecessary, potentially leading to redundant conditions, since Case Is >= 2 encompasses Case Is > 2 .

Select Case statements, by enabling multiple conditional evaluations succinctly, improve maintainability by allowing changes in condition handling to be implemented without restructuring complex if-else chains. This simplicity enhances readability, as grouping related conditions under Select Case clarifies logic paths. In larger programs, however, complexity can arise if nested or if cases proliferate without clear, segmented logic, potentially leading to confusion. Effective use of documentation and consistent coding practices bolsters maintainability when utilizing Select Case in extensive codebases .

The primary advantage of using a CASE statement for handling employee awards based on years is its straightforward, readable structure for matching values against various conditions. It simplifies decision-making logic by allowing developers to group different cases under a single statement, which ensures cleaner code when dealing with multiple discrete conditions as is typical in award determination by year milestones. CASE statements clearly distribute authority by easily recognizing and executing only the appropriate block of code based on met conditions .

Using the With…End With statement groups property assignments for the same object, improving readability and reducing code redundancy. Each property change within the With…End With block is performed on the last object accessed, which can marginally improve performance by reducing the overhead associated with repeatedly specifying the object name. This is especially beneficial when setting multiple properties or performing several operations on the object, as it results in cleaner, more organized code .

Chaining property changes using With…End With can yield issues if the initial object reference is incorrect or becomes stale, leading to a runtime error if the object does not exist or properties are incorrect. Moreover, any changes to object structure without concurrent updates to With…End With blocks can cause inconsistency. To enhance robustness, developers should ensure referenced objects are consistently valid, and consider wrapping such blocks in error handling to manage unforeseen exceptions gracefully .

You might also like