Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

class/subclass 101 question?

I am getting confused with very basic.
This proves I lack the fundamental knowdge on the class.
I have been coding in Java/c# and trying to work a new project in vb.net.
The vb.net is confusing me.

Do I need to instantial sub class?


Public class Person
  Public Property FullName as String
 Public Property ContactInfo as Address
End
Public class Address
Public Property  StreetName as string
Public Property  PhoneNo as Phone
End Class

Public Structure Phone
Public  Mobile as string
Public Home as string
End Structure

Main()
dim myPerson as new Person
myPerson.FullName ="John Smith"
myPerson.Address.StreeName = "101 one street"
myPerson.Address.Phone.Mobile ="123"

End Main


1) Do I need instantiate Address object?
2) I get an error, expression is a value. I think because I set it to the property.
myPerson.Address.Phone.Mobile ="123"
3) Is it OK to use the Structure like this?
0
dkim18
Asked:
dkim18
  • 3
  • 2
  • 2
2 Solutions
 
dkim18Author Commented:
1) Do I need instantiate Address object?

I think it is coming back to me.
I don't need to instantiate the subclass.
0
 
Julian HansenCommented:
You are trying to access an Address property in your Person class which does not exist. You also need to change your properties that are classes to have the new operator before the variable name - so yes you do need to instantiate the objects.

Also a class is closed by End Class - which you are missing for Person

Mutable structure - no use a class instead

Module Module1
    Public Class Person
        Public Property FullName As String
        Public Property ContactInfo As New Address
    End Class
    Public Class Address
        Public Property StreetName As String
        Public Property PhoneNo As New Phone
    End Class

    Public Class Phone
        Public Mobile As String
        Public Home As String
    End Class
    Sub Main()
        Dim myPerson As New Person
        myPerson.FullName = "John Smith"
        myPerson.ContactInfo.StreetName = "101 one street"
        myPerson.ContactInfo.PhoneNo.Mobile = "123"
    End Sub

End Module

Open in new window

0
 
käµfm³d 👽Commented:
You are trying to access an Address property in your Person class which does not exist.
To expand upon julianH's comment, you are trying to access the property, but you are doing so by way of the type name of the property rather than the name of the property. ContactInfo is the name of the property; Address is the type of the ContactInfo property. When working with instances, you access properties via their name, not their type.

I'm not sure how I feel about the "New" in the automatic property definition. It does work (and it's the first I've seen of such usage), but it feels like it could lead to problems. For example, in  julianH's example, he uses "New" to ensure that the ContactInfo property is intialized, but the FullName goes uninitialized. This could lead to subtle bugs, I think. My suggestion would be to either provide an initializer for FullName as well:

e.g.

Public Class Person
    Public Property FullName As String = ""
    Public Property ContactInfo As New Address
End Class

Open in new window


...or just go with a default constructor in which all properties are appropriately initialized.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dkim18Author Commented:
Thanks.
I had a lot of mistakes when I was writing this.
Sorry about that.

THe problem was I didn't instantiate them. I forgot NEW.
 Public Property PhoneNo As New Phone
I was trying to use the Property auto implement feature and left out New on some of these class data types.

I hear that you should use Property instead of Field instead and class instead of Structure.
Do you feel strong about that too?

I guess the answer is why not?

I need to store some double/integer values and I probably assign the values and never modify them. I want to create an object which I can access without much operation.
Do I still use properties instead of fields?
0
 
Julian HansenCommented:
@kaufmed - thanks for pointing it out - did not included initialiser because there were so many other issues that required focus - I was not aiming at production code but more to explain why the code posted would not work.

I find that introducing too much "new" stuff on a question like this - the focus on the key concepts can get lost. The code will compile without initialising fullname but it won't if ContactInfo is not.
0
 
dkim18Author Commented:
he uses "New" to ensure that the ContactInfo property is intialized, but the FullName goes uninitialized. This could lead to subtle bugs,

what kind of bug might arise?
0
 
käµfm³d 👽Commented:
Well, if you didn't properly initialize FullName when an instance is created, someone else using your class might not realize the property isn't initialized by the constructor (or a field initializer, if you go that route). The first time he does something like:

instance.FullName.Substring(0, 4);

Open in new window


WHAM!  NullReferenceException. I'm not saying that using New on the automatic property is necessarily evil. Just make sure you are consistent with your initializations so you don't end up in a situation similar to what I just described.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now