2
Answers

Properties in vb.net, need help

in a class, i wrote this code 

 Public Property Changeaheight() As Integer


        Get
            Return newValue
        End Get

        Set(ByVal value As Integer)

            newValue = value

        End Set
    End Property

and int shows me an error int the newValue parameter. this kind of error: 
newValue' is not declared. It may be inaccessible due to its protection level

Answers (2)
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
You either need to declare newValue as a Private Integer field as Satyapriya has done or (in VB 2010 or later), you can replace the whole lot with the following:

 Public Property Changeaheight as Integer

This is what's called an auto-implemented property and the VB.NET compiler replaces it with something similar to the previous code. However, the Private field is not directly accessible in this case and you always have to get or set the field via the Property. For example:

Changeaheight = 10
Console.WriteLine(Changeaheight) ' prints 10


0
Satyapriya Nayak

Satyapriya Nayak

NA 53k 8m 12y
Dim newValue As Integer Public Property Changeaheight() As Integer Get Return newValue End Get Set(ByVal value As Integer) newValue = value End Set End Property