-1

How to write the following code so that it will not return the error object reference not set .... Below is the code.

Private Quantity As String

Public Property Quantity1() As String
    Get
        Return Quantity.ToString()
    End Get
    Set(ByVal value As String)
        Quantity = value
    End Set
End Property
Loic
  • 123
  • 17
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Aug 06 '15 at 14:36
  • You don't have to call `ToString` on a `String` - redundant. – OneFineDay Aug 06 '15 at 14:49
  • Since this is `ASPX` properties would have to store and call values from a Session variable or other data persistant route. The values are wiped clean with each postback. – OneFineDay Aug 06 '15 at 15:45

3 Answers3

1

May be you needs to initialize your private vars with an initial value, or where are you assigning any value to them?

Private Quantity As String = ""
Loic
  • 123
  • 17
lem2802
  • 1,112
  • 7
  • 18
1

Instantiate your variables. For example...

 Private Quantity As String = ""

On another note, you do not have to add ToString to your strings as they are already Strings, it's a non-needed cast.

Loic
  • 123
  • 17
zaggler
  • 7,254
  • 6
  • 26
  • 47
1

First, remove the .ToString() calls from your properties. They are completely redundant since the variables you are returning are already strings.

Second, Strings in .net are reference types. Therefore, when you write something like Private Quantity As String, the Quantity is null (Nothing in vb.net), since it points to no string. If you wrote Private Quantity As String = String.Empty then it will not be null.

Loic
  • 123
  • 17
Zohar Peled
  • 73,407
  • 8
  • 53
  • 101