When to create a procedure(sub, function) - vb.net

I'm currently creating my application and I'd like to write an understandable code.
I'd like to know when to create a procedure. If it affects the performance of my application if I created much.
Here is my sample:
Sample Code:
With Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Greetings()
End Sub
Private sub Greetings()
MessageBox.Show("Hello!")
MessageBox.Show("To")
MessageBox.Show("My")
MessageBox.Show("World")
End
In the example above, assume that this sub will only be called 1 - 2 times in the whole application. I like to easily understand my code.
Versus
Without Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Hello!")
MessageBox.Show("To")
MessageBox.Show("My")
MessageBox.Show("World")
End Sub
In the example above, this piece of code too my time to understand.

In general, you should create subroutines when the logic is going to be used multiple times, especially if it's being used in multiple places.
If you ever have to change code, you want to only change it ONCE.
Performance with running basic code or calling functions is a non-issue. Do not even consider it.

Related

How can I let a code run permanent in the background, which waits for an input ? (Visual Basic)

I want to program a round based rpg like game in visual basic.
For that I want to make it possible, to show a picture of the current selected unit in the right bottom corner and add some information about the heal points and some other stats.
So I want to make a program part, which permanently asks, which of the units is selected and based on that let a other program part running, which changes the picture and the information about hp and this stuff, matching to the current unit.
But I am not able to run a program which runs in the background and doesn't freezes the main program while running (may I need a background worker ?)
Also I am not very sure, what for a program type I need to used for this (like a sub or something).
I don't know other types than sub's in vb, but I could derive it from my knowledge of java programming (functions and objects).
I know this is a bit much to ask, but I need to know, which program type I need to use and how I can let it run permanent in the background + how to transfer some information between these parts.
Would be nice, if someone could help me and explain a bit instead of just saying something like: you need a background worker use this link xy.
I tried to use a variable, which becomes a different number, when a other unit is clicked, unfortunately you cant see much of my code right now, since for this form, I didn't created much more than the graphical interface.
Public Class FormBattlescreen
Dim marked As Integer = 0
Private Sub Formsounds_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
FormMainCampagne.Close()
End Sub
Private Sub OwnSoldier1_Click(sender As Object, e As EventArgs) Handles OwnSoldier1.Click
marked = 1
End Sub
Private Sub OwnSoldier2_Click(sender As Object, e As EventArgs) Handles OwnSoldier2.Click
marked = 2
End Sub
Private Sub OwnSoldier3_Click(sender As Object, e As EventArgs) Handles OwnSoldier3.Click
marked = 3
End Sub
Private Sub EnemyOfficer1_Click(sender As Object, e As EventArgs) Handles EnemyOfficer1.Click
marked = 4
End Sub
Private Sub RunGame()
Select Case marked
Case 0 'nothing is selected//at the beginning
Case 1
PictureBoxStats.Image = My.Resources.Soldier2
Case 2
Case 3
Case 4
End Select
End Sub
End Class
Thank you, I appreciate your help.

How do i update different table from the same access database using visual basic .net

I added my Enrollment system access Database, into my Enrollment System vb.net form, as a data source. The Database has 2 tables in it, the accountTable and studentEnrollmentInformation. I dragged The accountTable's details and data grid view into my form designer. The following code automatically appeared in the code designer:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
The following code works for updating the accounTableDataGridView but it does not work for studentEnrollmentInformationDataGridView so i manually created one
for studentEnrollmentInformation.
Function updateStudent()
Me.Validate()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Function
This is the function that contains the update code, that i manually created for updating the studentEnrollmentDataGridView. Adding new Row works fine but when i try to update studentEnrollmentDataGridView the texts in the table disappears and does not update/save. I also had function for updating the accountTableDataGridView which works fine.
Function update() 'THIS FUNCTION CONTAINS PRE-MADE CODE TO MAKE UPDATING SHORTER IN WRITING CODE.
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Function
My Question is how do i update multiple Tables in my system? Updating the other table works fine but the other is not.
In the original auto-generated code, this is the line that retrieves the data in the first place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
When the form loads, the Account data is retrieved into a DataTable that is already bound. If you want to retrieve Student Enrollment data too, do it in the same place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Sub
Now you're populating both bound DataTables when the form loads. When it comes to saving, you do the same thing, i.e. add the code to save the changes to the other DataTable where you already have the code to save the first:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
You don't need any code to specifically save the changes from the DataTable because the whole point of UpdateAll is that it updates all DataTables in the DataSet.
As is always the case, if it doesn't seem to be working as you expect then you debug it. In that case, that would mean setting a breakpoint on the UpdateAll line and examining the exact state of the DataSet before and after the call, as well as possibly examing the sate of the database too.

VB.NET/Access DataGridView not displaying table contents

I'm attempting to display an Access Database table in a DataGridView on the most simple form you could make. However, when I start the application I don't see any of the table data. See below.
I'm happy to update with any additional information needed.
VB:
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MOKANDataSet.SalaryHistory' table. You can move, or remove it, as needed.
Me.SalaryHistoryTableAdapter.Fill(Me.MOKANDataSet.SalaryHistory)
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Here is the application at runtime.
Overview of project in Visual Studio
I was curious and went directly to the executable in the bin folder. Upon execution, I got "The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine. I'm using Win 7 64 Bit, but the Microsoft Office suite I have is 32-bit. I got the 32-bit AccessDatabase Engine referenced here

Menu Strip Control "Window"

I am working on a project and am just stymied by this. It should be really straight forward. I have included the code so you can see the other Menu Strip Items.
The user has the ability to open as many "Child" forms into the mdiParent form. I would like the "Window" function on the menu strip to populate with the Bank Name found on the Child form so if a user had 10 bank forms open, they could find a specific form by clicking Window and seeing the bank name (the name of the text field which I would like to pull is Bank.lblbank.text) This functionality was found in the 2007 and older versions of many of the Microsoft Suite products.
If I hadn't seen my professor do this in class, I would think it was a bit of proprietary Microsoft Office coding that us mere mortals cannot access. Unfortunately, he whipped it out and I didn't get it captured.
Obviously, I am not asking the right questions on the search engines because I cannot find a clear answer. Does anyone have any advice? This functionality isn't necessary but a little something I want to add. I've worked on this way too long and just want a little bit of success.
Let me know if a zip copy of the project or screen prints would be helpful. I'm happy to send them your way.
Appreciate everyone looking at this post and their feedback. Thank you for your time!
Lauren
Public Class Loan_Evaluator
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub NewLoanToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewLoanToolStripMenuItem.Click
Dim NewBank As New Bank
NewBank.MdiParent = Me
NewBank.Show()
End Sub
Private Sub VerticalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VerticalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub
Private Sub CascadeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CascadeToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub
Private Sub HorizontalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HorizontalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub WindowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WindowToolStripMenuItem.Click
End Sub
End Class
Select the main MenuStrip control, not the ToolStripMenuItem. So you'd click on MenuStrip1, not WindowToolStripMenuItem, for example. Now change the MdiWindowListItem() property to "Window" (or whatever menu item you want to be populated with open MdiChildren). Done.

Public Labels(Pointers)

I am working with VB 2010 for an application which requires me to go back to a sub. After some digging, I found out GoSub is no longer supported. I tried using Goto but apparently you can't use it outside the sub the label is in. I tried calling the sub put the parameters were not known to me.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
I do not want to use the RunWorkerAsync because the worker will already be working. Please advise me on this matter.
It sounds like you are trying to solve a problem using procedural logic with an object-oriented tool.
If I understand you correctly, the sub is calling another section of code, and needs to return to the sub after it's finished. Make the called code itself a function or sub, and call it from the primary sub routine.