Doubt on Structures and Arrays: Version 2: Pls help

  • Thread starter Thread starter sougata12
  • Start date Start date
S

sougata12

Guest
Say, there is a small organization which is founded by two promoters “JAIDEEP” and “SHILPA”. They have hired 2 employees whose names are “Joy” and “Sougata”. Since it is a small organization each of the employees report to both the promoters.



My aim to create a structure named EMPLOYEE with only two fields: Name and Reporting_Authority. The Reporting_Authority field will be an array and since each employee reports to both promoters, I want to initialize this array with the names of the two promoters “JAIDEEP” and “SHILPA”. The idea is that as and when we create an instance, we need to only assign the names. Since the reporting authorities are fixed for every employee, the values that were initialized should get attached to the instance being created automatically. Only the name field should be populated separately. For example when I create instance “e1” and assign “e1.Name=Sougata”, then assignment for {e1.reporting_authority(0)=”Jaideep”} and {e1.reporting_authority(1)=”Shilpa”} should happen automatically. The same thing repeats when instance e2 is created. To that end I wrote the following block of code but getting an error shown below:

Public Structure employee
Public name As String
Public reporting_authority() As String
Public Sub initialize()
Dim reporting_authority() As String = {"Jaideep", "Shilpa"}
End Sub
End Structure

Module Module1

Sub Main()
Dim e1, e2 As New employee

e1.name = "Sougata" : e2.name = "Joy"
Console.WriteLine($"Employee Name: {e1.name}")
Console.WriteLine($"Reports to:")
For Each item As String In e1.reporting_authority
Console.WriteLine($" {item}")
'Expecting the reporting authority names to be attached directly to the instance e1 and be fetched here
Next
Console.WriteLine("------------------")
Console.WriteLine($"Employee Name: {e2.name}")
Console.WriteLine($"Reports to:")
For Each item As String In e2.reporting_authority
Console.WriteLine($" {item}")
Next

Console.WriteLine("------------------")
Console.ReadLine()
End Sub

End Module







Question 1:
I have to use the NEW keyword in the line "Dim e1,e2 as NEW employee" else I get an error. Why do I need to use the NEW keyword to instantiate a structure which is a value type data? I am under the impression that NEW is to be used for instantiating classes?

Question 2: I am getting the following error when I run the code. Please help me with this:
1371399.png

Request you guys to help me on this.


Regards,

Sougata


Sougata Ghosh

Continue reading...
 
Back
Top