Structure and arrays

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

sougata12

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

Module Module1

Sub Main()
Dim e1 As employee

e1.name = "Sougata"
Console.WriteLine($"Employee Name: {e1.name}")
Console.WriteLine($"Reports to:")
For Each item As String In e1.reporting_authority
Console.WriteLine($" {item}")
Next

Console.ReadLine()
End Sub

End Module

In the above code I have declared a structure named employee which has 2 fields: Name and Reporting Authority. Now assuming that it is a very small organization all the employees report to two persons namely: Jaideep and Shilpa. Since the reporting authority is same for all instances of the structure I decided to initialize the reporting authority array inside the structure. But I am unable to print the array and getting the following error. What I am not understanding is why it says that a null reference can result because value has not been assigned before? Havent I assigned the values of "Jaideep" and "Shilpa" to "Reporting_Authority"? Any help please?

1371376.png



Sougata Ghosh

Continue reading...
 
Back
Top