none
Structures and Objects RRS feed

  • Question

  • Hi,

    I am trying to define an object as a field for the structure. But getting an error..Can I get some help pls?

    Public Class car
        Public Property make As String
        Public Property miles As Integer
    End Class
    
    Public Structure employee
        Public name As String
        Public reporting_authority As String
        Public c1 As car
    End Structure
    
    Module Module1
    
        Sub Main()
            Dim e As employee
            e.name = "Sougata" : e.reporting_authority = "Jaideep"
            e.c1.make = "Ford" : e.c1.miles = 123456
            Console.WriteLine($"Name: {e.name}")
            Console.WriteLine($"Reports to: {e.reporting_authority}")
            Console.WriteLine($"Owns a {e.c1.make} car")
            Console.WriteLine($"Distance: {e.c1.make}")
            Console.ReadLine()
        End Sub
    
    End Module

    The error I ma getting is:

    Pls tell me where I am going wrong. Also, apart from the error, can someone explain why the green squiggly line is appearing only under "e.c1.make" and not "e.c1.miles"? If what I have done is wrong, Should it not appear under both?


    Sougata Ghosh

    Sunday, December 16, 2018 6:09 PM

Answers

  • You declared car as a class but you don't create an instance

    e.c1 = New car()

    • Marked as answer by sougata12 Sunday, December 16, 2018 7:06 PM
    Sunday, December 16, 2018 6:49 PM

All replies

  • You declared car as a class but you don't create an instance

    e.c1 = New car()

    • Marked as answer by sougata12 Sunday, December 16, 2018 7:06 PM
    Sunday, December 16, 2018 6:49 PM
  • Yes..I had understood that but was not able to figure out the syntax. So thank you for that.

    Sougata Ghosh

    Sunday, December 16, 2018 6:52 PM
  • Hello,

    You are not really gaining anything from using a structure, this would be more appropriate along with better naming and pluralizing of names. 

    Public Class Employee
        Inherits Car
        Public Property Car() As New Car
        Public Property Name() As String
        Public Property ReportingAuthority() As String
    End Class
    
    Public Class Car
        Public Property Make As String
        Public Property Miles As Integer
    End Class
    
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, December 16, 2018 7:06 PM
    Moderator
  • I know. I was just trying to understand Structures and how it interacts with other programming elements. So the query was more academic in nature. But thanks for your comment though.

    Sougata Ghosh

    Sunday, December 16, 2018 7:08 PM