How clear homemade object?

jenn5175

Freshman
Joined
Apr 4, 2002
Messages
35
Okay, this is going to be remedial for most of you. I have a combobox which needed to be associated with both a code(varchar) and the display name (varchar). So I made a new class:

Code:
public class cmboObject
  public _name as string
  public _code as string

  public sub new(byval name as string, byval code as string)
    _name = name
    _code = code
  end sub
  
  public readonly property Name() as string
     get
        return _name
     end get
  end property

  public readonly property Code() as string
     get
        return _code
     end get
  end property

  public overrides function ToString() as string
     return name
  end function

end class

I use the same comboboxes for multiple questions (the app is a survey showing one question at a time) so I made a variable to hold their choice once they move to the next question

Code:
Dim brochureMake1 as cmboObject
Dim brochureMake2 as cmboObject

Then when the comboboxes were filled from a database I used:

Code:
Dim cmboObjHolder as cmboObject
while dtr.read()
   cmboObjHolder = new cmboObject(dtr("field1"), dtr("field2"))
    cmboMake1.Items.Add(cmboObjHolder)
end while

Now when they got to the combobox and hit "Next Question" I would simply save that cmboObject:

Code:
brochureMake1 = cmboMake1.SelectedItem

This above code works great. However, once the survey is submitted, I need to clear all of the variables to get ready for the next person taking the survey. I have tried everything to reset brochureMake1 and brochureMake2 to emptiness (tries = "", = nothing, etc) but keep getting "NullCastException". I am thinking I need to make a "set" function and just set name and code to "" but I cannot get it right. Any suggestions?

Jenn
 
That's almost exactly what I tried already. Here's my attempt and the failed result. Let me know if I implemented it incorrectly:

Code:
'cmboObject.vb - class defination file

Public sub Empty()
  _name = ""
  _code = ""
end sub

Code:
'frmMain.vb - main form file


public sub ClearVariables()
   brochureMake1.Empty
   brochureMake2.Empty
  ...
end sub

When the survey first starts it calls ClearVariables() and everything is fine. However, once the survey is filled out once, when ClearVariables() is called again I get the following error (at the line calling Empty):

A managed NullReferenceException occurred at Application::Run+0xf

What am I doing wrong? I tried and it errors regardless of if anything was set in brochureMake1/2 or not.
 
NullReferenceException means that you tried to access a variable that is not initialized yet, not created. Check your code for anthing that you disposed of or forgot to create.
 
Back
Top