Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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

 

Dim brochureMake1 as cmboObject
Dim brochureMake2 as cmboObject

 

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

 

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:

 

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

Posted

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

 

'cmboObject.vb - class defination file

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

 

'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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...