none
Structure and arrays RRS feed

  • Question

  • 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?


    Sougata Ghosh



    • Edited by sougata12 Saturday, December 15, 2018 9:09 AM
    Saturday, December 15, 2018 7:42 AM

Answers

  • You have not initialized it because you did not use NEW constructor it did not call the declares..

      Dim e1 As New employee

    ps it should be public:

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

    Plus I am still not sure that is correct. Should the next dim be public.

    OH.. that should be Sub NEW not intitialize?

    Wait...

    Time to use a class:

        Private Class employee
            Public name As String
            Public reporting_authority() As String
    
            Sub New()
                reporting_authority = {"Jaideep", "Shilpa"}
            End Sub
    
        End Class

    You may want to pass the values to the class in the new constructor etc.

    Explain exactly what you want to accomplish.




    • Edited by tommytwotrain Saturday, December 15, 2018 9:49 AM
    • Marked as answer by sougata12 Saturday, December 15, 2018 11:01 AM
    Saturday, December 15, 2018 9:31 AM
  • Replace initialize by New()

    like :

    Public Sub New(name As String)
        Me.name = name
        reporting_authority = {"Jaideep", "Shilpa"}
    End Sub

    that you can call :

     Dim e1 As employee = New employee("Sougata")

    • Marked as answer by sougata12 Saturday, December 15, 2018 11:01 AM
    Saturday, December 15, 2018 9:43 AM

All replies

  • You have not initialized it because you did not use NEW constructor it did not call the declares..

      Dim e1 As New employee

    ps it should be public:

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

    Plus I am still not sure that is correct. Should the next dim be public.

    OH.. that should be Sub NEW not intitialize?

    Wait...

    Time to use a class:

        Private Class employee
            Public name As String
            Public reporting_authority() As String
    
            Sub New()
                reporting_authority = {"Jaideep", "Shilpa"}
            End Sub
    
        End Class

    You may want to pass the values to the class in the new constructor etc.

    Explain exactly what you want to accomplish.




    • Edited by tommytwotrain Saturday, December 15, 2018 9:49 AM
    • Marked as answer by sougata12 Saturday, December 15, 2018 11:01 AM
    Saturday, December 15, 2018 9:31 AM
  • Replace initialize by New()

    like :

    Public Sub New(name As String)
        Me.name = name
        reporting_authority = {"Jaideep", "Shilpa"}
    End Sub

    that you can call :

     Dim e1 As employee = New employee("Sougata")

    • Marked as answer by sougata12 Saturday, December 15, 2018 11:01 AM
    Saturday, December 15, 2018 9:43 AM
  • Yeah it works but I have a follow up question. If I am not using an array I see that I don't have to use the NEW keyword.

    Structure employee
        Public name As String
    End Structure
    
    Module Module1
    
        Sub Main()
            Dim e1 As employee
            e1.name = "Sougata"
            Console.WriteLine($"Employee Name: {e1.name}")
            Console.ReadLine()
        End Sub
    
    End Module

    The above works fine. So can you please also explain what is the role of the NEW keyword? In other words why are the arrays values of "Jaideep" and "Shilpa" not getting attached to the instance e1 that I am creating without using the NEW keyword?


    Sougata Ghosh


    • Edited by sougata12 Saturday, December 15, 2018 10:02 AM
    Saturday, December 15, 2018 9:56 AM
  • @Tommytwotrain: You asked what I exactly want to do? 

    I wanted to study how to handle a situation when a field in a STRUCTURE has same values for different/all instances. I tried out the code block given in my question, got stuck. 

    You also mentioned that "Plus I am still not sure that is correct. Should the next dim be public.". What I found is that it cannot be PUBLIC because it is a local variable declaration...so DIM should be fine. Any difference in opinion is welcome.

    Finally, you mentioned "Time to use a class". What I want to understand is, IS there any benefit of using a CLASS in this case?



    Sougata Ghosh

    Saturday, December 15, 2018 10:12 AM
  • @Tommytwotrain: You asked what I exactly want to do? 

    I wanted to study how to handle a situation when a field in a STRUCTURE has same values for different/all instances. I tried out the code block given in my question, got stuck. 

    You also mentioned that "Plus I am still not sure that is correct. Should the next dim be public.". What I found is that it cannot be PUBLIC because it is a local variable declaration...so DIM should be fine. Any difference in opinion is welcome.

    Finally, you mentioned "Time to use a class". What I want to understand is, IS there any benefit of using a CLASS in this case?



    Sougata Ghosh


    "why are the arrays values of "Jaideep" and "Shilpa" not getting attached to the instance e1 that I am creating?

    You did not use public in your structure declare so when you assign the names in the sub initialize it does not reference back to the main structure and the parent.

    You can pass the names to the class in the new constructor (as Castor begins to show).

    Where do you want to declare things? Or you can allow both ways.

    "Finally, you mentioned "Time to use a class". What I want to understand is, IS there any benefit of using a CLASS in this case?"

    Yes. You learn to use them.

    THe only reason to use a structure these days is to save memory if for some reason your run on a calculator or a realtime 3d space mission simulation or something.

    Describe what you want. I will make you an example if Castor does not but I am no expert and still learning...

    PS When you use the NEW constructor it calls that sub routine. Your original structure did not do that. You did not call initialize so  the names where never put into the array.

    I don't know if structures use NEW the same way. THat is another reason to use a class.

    " same values for different/all instances"

    How many values is it? < 10 or >10000 ?

    I think you can do it like I showed in my last example? But the must be lots of ways.

    Look at the top of your form code what does it say? CLASS !!!!

    Anything you can do in a form you can do in a class. :) So it works how you think.

    You might want to make a new post now with your latest and ask how to make a class for what you need.


    Saturday, December 15, 2018 10:19 AM
  • ok.thanks. The solution that both Castor and you have given works fine. The exercise that I am doing is more about understanding structures and its limitations and does not have a end goal so to say.  But just to understand the underlying concepts more clearly, one last question:

    "You did not use public in your structure declare so when you assign the names in the sub initialize it does not reference back to the main structure and the parent."

    I changed the structure declaration as follows:

    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
    I have declared my structure to be public but if I don't use the NEW keyword the error is still present. So am I understanding you correctly or is it something else that you meant with the statement highlighted in bold font? Also I am closing this thread since the original question has been answered but would be great if the above is clarified.


    Sougata Ghosh


    • Edited by sougata12 Saturday, December 15, 2018 11:01 AM
    Saturday, December 15, 2018 10:56 AM
  • ok.thanks. The solution that both Castor and you have given works fine. The exercise that I am doing is more about understanding structures and its limitations and does not have a end goal so to say.  But just to understand the underlying concepts more clearly, one last question:

    "You did not use public in your structure declare so when you assign the names in the sub initialize it does not reference back to the main structure and the parent."

    I changed the structure declaration as follows:

    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
    I have declared my structure to be public but if I don't use the NEW keyword the error is still present. So am I understanding you correctly or is it something else that you meant with the statement highlighted in bold font? Also I am closing this thread since the original question has been answered but would be great if the above is clarified.


    Sougata Ghosh



    " but if I don't use the NEW keyword the error is still present. So am I understanding you correctly or is it something else that you meant with the statement "


    Yeah. I later added in the PS that when you use NEW it basically calls the sub New sub routine of the class. I am not sure with structures what happens? Where did you get initialize? So by using new the names are put into the array because vb calls the sub new with that way of declaring.

    I dont know for structures if it is the same?

    But you should use a class anyway. Basically all you have to do is put the word class instead of structure?

    Castor et al?

    Saturday, December 15, 2018 11:22 AM
  • "Initialize" is subroutine name that I have chosen..it is not a VB.net keyword......I am fine with a class but its just that I am trying to understand Structures thoroughly.

    Sougata Ghosh


    • Edited by sougata12 Saturday, December 15, 2018 11:25 AM
    Saturday, December 15, 2018 11:24 AM
  • "Initialize" is subroutine name that I have chosen..it is not a VB.net keyword......I am fine with a class but its just that I am trying to understand Structures thoroughly.

    Sougata Ghosh



    If no one else answers you should ask the question again now with the new specifics (ie this works this does not) and I will stay out.

    I dont have a good explanation of the details other than thats how new works.

    The same thing happens with a string declared like this:

        Dim tom as string

    gives a warning but

        Dim tom as string = ""

    does not.

    I dont know the details of why exactly.

    Saturday, December 15, 2018 11:53 AM
  • Thanks. Will do.

    Sougata Ghosh

    Saturday, December 15, 2018 12:32 PM