Homework2_RA

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.

https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/bb332338(v=msdn.10)?redirectedfrom=MSDN#msdnwcfhc_topic4

Application.Designer.vb

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.

https://www.informit.com/articles/article.aspx?p=1602817&seqNum=3

So

To start A C# program we use the Application.run().

In VB.Net we use the Global.Application1.FormToStart (the module also sets global variables)

Homework1_RA

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.

C#

private void Form1_Load(object sender, EventArgs e)
        {
           ...
            richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
        }

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.

A good explanation here:

https://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp

Some difference:

https://stackoverflow.com/questions/38868304/vb-net-to-c-sharp-with-event-handlers

Conclusion

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.

See also:

https://www.mobzystems.com/code/events-in-csharp-and-vbnet/

Homework 3_R

A frequency distribution

The frequency distribution of events is the number of times each event occourred in an experiment or study. There isn’t “best” number of bins, and different bin size can reveal different features of the data.

A frequency distribution provides a visual representation for the distribution observations within a particular test, with the goal to simplify the organization and presentation of data. Some of the graphs that can be used with frequency distributions are histograms, line charts, bar charts and pie charts. Frequency distributions are used for both qualitative and quantitative data.

Speaking of “univariate

Univariate is a term commoly used in statistics to descrive a type of data which consists of observations on only a single characteristic or attribute. So, in other words, the data has only one variable.

Information: From dataset to frequency distribution

A frequency distribution provides a snapshot view of the characteristics of a dataset. It paks information provided by dataset into ‘buckets’ or ranges, changing the amount of information that we can see.


Even if we gain knowlege, we lose information, so we can’t reconstruct a dataset given a distribution.

References:

Frequency distribution

Frequency distribution Wiki

Univariate

For further information

Homework 2_A

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’)

https://docs.microsoft.com/it-it/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference
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:

https://www.ict.social/vbnet/oop/reference-and-value-data-types-in-vbnet

C#

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;
        }
    }
}

Homework 1_A

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

C#

namespace Application1_C
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnAppears_Click(object sender, EventArgs e)
        {
            this.TxtGeneral.AppendText("Some text appears" + Environment.NewLine);
        }

        private void BntClear_Click(object sender, EventArgs e)
        {
            this.TxtGeneral.ResetText();
        }

        private void TxtGeneral_MouseEnter(object sender, EventArgs e)
        {
            this.TxtGeneral.BackColor = Color.Red;
        }

        private void TxtGeneral_MouseLeave(object sender, EventArgs e)
        {
            this.TxtGeneral.BackColor = default(Color);
        }
    }
}