Note that any C# will have a Program.cs file in its solution folder while VB.NET does not. On the other hand, VB.NET has the file Application.Designer.vb within the project folder. Try to research what these (automatically created) files are doing in your application and try to discover / reverse engineer the differences on how a C# and VB.NET program are started.
Something about this files
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NameFile
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
A program need a main method to start execute.
The Windows Service project template generates a project that contains two files: the service1.cs file that contains the service implementation and the program.cs file that instantiates and essentially hosts the Windows service.
With Application.Run(new Form1()); Program.cs file start from form1.
Option Strict On
Option Explicit On
Namespace My
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.Application1.Form1
End Sub
End Class
End Namespace
The Me identifier represents the instance of the current application. The OnCreateMainForm method establishes which window must be the startup one. In this case Form1 is the default name that Visual Studio assigns to the main window when a new project is created.
You might need to set custom actions for application events (such as Startup or Shutdown, which are usually handled in the ApplicationEvents.vb file) and Application.designer.vb is the right place.
Observe carefully the different way C# and VB.NET deals with events and the different ways to define the event handlers. Discuss in your blog what differences you can spot. Which way do you find easier or more comfortable and why?
VB.NET
Private Sub RichTextBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
...
End Sub
We need to subscribe the handler to the event, which you can do adding a Handles clause to the method.
In C# we need to create a delegate (pointer) to Handler and adding it to DragDrop list of “Event Handlers”.
You can think of a delegate as a pointer (or a reference) to a method. This is useful because the pointer can be passed around as a value. The central concept of a delegate is its signature, or shape. That is the return type and the input arguments. For example, if we create a delegate void MyDelegate(object sender, EventArgs e), it can only point to methods which return void, and take an object and EventArgs. Kind of like a square hole and a square peg. So we say these methods have the same signature, or shape, as the delegate.
VB.NET allows you to declare and use events in a much simpler, cleaner way (the whole delegate thing is accomplished behind the scenes) while in C#, you have to type it out yourself. However, in order to adhere to .NET conventions, you can declare your events in much the same way in VB.NET as you would in C#.
In my opinion, then, is more easier in VB.NET, but it’s more interesting in C# for understand how it works.
Search on the web how to drag drop the name of any file into a richtextbox on your startup form and try to implement this feature in your first program.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RichTextBox1.AllowDrop = True
End Sub
Private Sub RichTextBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim name As String = ""
For Each path In files
name = name & path
Next
Dim namePath As String = name.Substring(0, name.LastIndexOf("\"))
Dim nameFile As String = name.Substring(name.LastIndexOf("\") + 1)
RichTextBox1.Clear()
RichTextBox1.AppendText("Path: " & namePath & Environment.NewLine & "Name File: " & nameFile)
End Sub
Private Sub RichTextBox1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
End Class
Create or search some simple but illuminating example of code which clearly shows the different behaviors of reference value data types and value type data types.
VB.NET
In VB.NET we can see the difference even just by applying the parameter ByVal or ByRef(we can see that the defaut is ‘ByValue’)
Public Class Form1
Private Sub BtnValue_Click(sender As Object, e As EventArgs) Handles BtnValue.Click
'Value Tipe
Dim valueV As Integer = 1
Me.TxtValue.AppendText("Value before subroutine: " & valueV & Environment.NewLine)
Increment_v(valueV)
Me.TxtValue.AppendText("Value after subroutine: " & valueV & Environment.NewLine)
End Sub
Public Sub Increment_v(value As Integer)
value = value + 1
Me.TxtValue.AppendText("Value in subroutine: " & value & Environment.NewLine)
End Sub
Private Sub BtnRef_Click(sender As Object, e As EventArgs) Handles BtnRef.Click
'Ref Type
Dim valueR As Integer = 1
Me.TxtRef.AppendText("Value before subroutine: " & valueR & Environment.NewLine)
Increment_r(valueR)
Me.TxtRef.AppendText("Value after subroutine: " & valueR & Environment.NewLine)
End Sub
Public Sub Increment_r(ByRef value As Integer)
value = value + 1
Me.TxtRef.AppendText("Value in subroutine: " & value & Environment.NewLine)
End Sub
End Class
Another example in VB.NET, more interesting could be:
Public Class App2_VB
Private Sub BtnExample_Click(sender As Object, e As EventArgs) Handles BtnExample.Click
Dim a As Integer = 78
Dim student1 As New Student
student1.name = "Paperina"
student1.age = 25
Dim b As Integer = 11
Dim student2 As New Student
student2.name = "Pippo"
student2.age = 23
Me.TxtGeneral.AppendText("We have two objects Student:" & Environment.NewLine & "S1: " & student1.ToString &
Environment.NewLine & "S2: " & student2.ToString & Environment.NewLine)
Me.TxtGeneral.AppendText("And we have two values:" & Environment.NewLine & "a= " & a & " " & "b= " & b & Environment.NewLine)
a = b
student1 = student2
Me.TxtGeneral.AppendText(Environment.NewLine & "*****************************************" & Environment.NewLine)
Me.TxtGeneral.AppendText("Now i have assigned S1=S2 and a=b: " & Environment.NewLine & "S1: " & student1.ToString &
Environment.NewLine & "S2: " & student2.ToString & Environment.NewLine & "And we have two values:" &
Environment.NewLine & "a= " & a & " " & "b= " & b & Environment.NewLine)
b = 100
student2.name = "Valentino"
student2.age = 46
Me.TxtGeneral.AppendText(Environment.NewLine & "*****************************************" & Environment.NewLine)
Me.TxtGeneral.AppendText("Now i have assigned a new value in S2 and b = 100: " & Environment.NewLine & "S1: " & student1.ToString &
Environment.NewLine & "S2: " & student2.ToString & Environment.NewLine & "And we have two values:" &
Environment.NewLine & "a= " & a & " " & "b= " & b & Environment.NewLine)
End Sub
End Class
Public Class Student
Public age As Integer
Public name As String
Public Overrides Function ToString() As String
Return "Name: " & name & "-" & "Age: " & age
End Function
End Class
We can see that when, in the end, I assign the value at ‘student2‘, change also ‘student1‘ because we have a pointer at the same object. I got the idea for this code from this:
Also in C# we can pass a value with the keyword ref (See also: here), but I write only the second example in C#, cause the second is more funny.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Application2_C
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = 78;
Student student1 = new Student
{
name = "Paperina",
age = 25
};
int b = 11;
Student student2 = new Student
{
name = "Pippo",
age = 23
};
this.TxtGeneral.AppendText("We have two objects Student:" + Environment.NewLine + "S1: " + student1.ToString() +
Environment.NewLine + "S2: " + student2.ToString() + Environment.NewLine);
this.TxtGeneral.AppendText("And we have two values:" + Environment.NewLine + "a= " + a + " " + "b= " + b + Environment.NewLine);
a = b;
student1 = student2;
this.TxtGeneral.AppendText(Environment.NewLine + "*****************************************" + Environment.NewLine);
this.TxtGeneral.AppendText("Now i have assigned S1=S2 and a=b: " + Environment.NewLine + "S1: " + student1.ToString() +
Environment.NewLine + "S2: " + student2.ToString() + Environment.NewLine + "And we have two values:" +
Environment.NewLine + "a= " + a + " " + "b= " + b + Environment.NewLine);
b = 100;
student2.name = "Valentino";
student2.age = 46;
this.TxtGeneral.AppendText(Environment.NewLine + "*****************************************" + Environment.NewLine);
this.TxtGeneral.AppendText("Now i have assigned a new value in S2 and b = 100: " + Environment.NewLine + "S1: " + student1.ToString() +
Environment.NewLine + "S2: " + student2.ToString() + Environment.NewLine + "And we have two values:" +
Environment.NewLine + "a= " + a + " " + "b= " + b + Environment.NewLine);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application2_C
{
public class Student
{
public string name;
public int age;
public override string ToString()
{
return "Name: " + name + "-" + "Age: " + age;
}
}
}
Create, in both languages C# and VB.NET, a program which does the following simple tasks:
When a button is pressed some text appears in a richtexbox on the startup form
When another button is pressed the richtextbox is cleared
When the mouse enters the richtextbox, the richtext backcolor is switched to another color
When the mouse leaves the richtextbox, the richtext backcolor is reset to its original state
VB.NET
Public Class Form1
Private Sub BtnAppears_Click(sender As Object, e As EventArgs) Handles BtnAppears.Click
Me.TxtGeneral.AppendText("Some text appears" & Environment.NewLine)
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
Me.TxtGeneral.Clear()
End Sub
Private Sub TxtGeneral_MouseEnter(sender As Object, e As EventArgs) Handles TxtGeneral.MouseEnter
Me.TxtGeneral.BackColor = Color.Blue
End Sub
Private Sub TxtGeneral_MouseLeave(sender As Object, e As EventArgs) Handles TxtGeneral.MouseLeave
Me.TxtGeneral.BackColor = SystemColors.Window
End Sub
End Class