setting a class property to a null value...

B

Brad Pears

I have an integer type database field that allows nulls. The associated
class property for this database (SQL Server 2000) field is of course also
an integer.

In my code (vb.net 2005), I am setting this property to a text box's ".text"
value. Now, depending on whether this is a new record or the user is
updating the record, the text box will either have an integer value in it or
will be null.

So, in my code where I am setting the class property I am getting an error
when the text box's .text property is null...

here is the code...

objPM.PostMasterID = me.txtPMID.text

At run time this will fail with an "input string was not in the correct
format" error.

My question is, is there a way to code this to handle the possiblity of the
null value in the text box?

The class property field is coded as shown below. In the properties "set"
code, I tried checking "value" for null and if so, set the actual property
to 0 but that also fails.

Any ideas what I could do to handle this?

Thanks, Brad

Public Property PostMasterID() As Integer

Get

Return iPMID

End Get

Set(ByVal value As Integer)



iPCID = value

End If

End Set

End Property
 
B

Brad Pears

Ok, I tried that, and I am now getting the following error
on the following line where I am trying to set the property.

Conversion from string "" to type 'Integer' is not valid.

Here is the code that is failing.

"objPM.PostMasterID = me.txtPMID.text"

I also changed my class property code to the following as per your
suggestion...

Public Property PostMasterID() As Nullable(Of Integer)
Get
Return iPMID
End Get
Set(ByVal value As Nullable(Of Integer)
iPMID = value
End Set
End Property

Help!

Thanks, Brad
 
G

Guest

Ok, I tried that, and I am now getting the following error
on the following line where I am trying to set the property.

Conversion from string "" to type 'Integer' is not valid.

Here is the code that is failing.

"objPM.PostMasterID = me.txtPMID.text"

What you'll have to do is:

if String.IsNullOrempty(me.txtPMID.text) andalso IsNumeric(txtPMID.text)
then
objPM.PostMasteRID = nothing
else
objPM.PostMasteRID = me.txtPMID.text
end if
 
K

kimiraikkonen

Ok, I tried that, and I am now getting the following error
on the following line where I am trying to set the property.

Conversion from string "" to type 'Integer' is not valid.

Here is the code that is failing.

"objPM.PostMasterID = me.txtPMID.text"

If me.txtPMID.text was entered as an integer value, you wouldn't get
this error.

I think you can add a checking mechanisim for "txtPMID.text" to be
forced not to enter other than an integer with doing like:
 
B

Brad Pears

Ok, that's what I figured I'd wind up having to do, I was just hoping I
could place code on the actual class property to handle cases where it could
be a null. That way anytime the class is used, you don;t have to worry about
having to handle it in form code etc... but oh well...

Thanks!

Brad
 
C

Chris Dunaway

You should add Option Strict On to the top of your code file or set it
in the default properties for Vb.

txtPMID.text is a string, PostMasterID is an integer. You must
convert the string to an integer before you can assign it.

Chris
 
S

Scott M.

Ok, I tried that, and I am now getting the following error
on the following line where I am trying to set the property.

Conversion from string "" to type 'Integer' is not valid.

Here is the code that is failing.

"objPM.PostMasterID = me.txtPMID.text"

If me.txtPMID.text was entered as an integer value, you wouldn't get
this error.

I think you can add a checking mechanisim for "txtPMID.text" to be
forced not to enter other than an integer with doing like:

Not true. No matter what you enter into a text field. It is first and
foremost assigned to the String type. You would only get an implicit cast
if you were working with Option Strict Off, which is a bad way to work and
should be turned on in VS, so that all future projects start out with it on.
 
K

kimiraikkonen

If me.txtPMID.text was entered as an integer value, you wouldn't get
this error.

I think you can add a checking mechanisim for "txtPMID.text" to be
forced not to enter other than an integer with doing like:

Not true.  No matter what you enter into a text field.  It is first and
foremost assigned to the String type.  You would only get an implicit cast
if you were working with Option Strict Off, which is a bad way to work and
should be turned on in VS, so that all future projects start out with it on.

I just meant that if you assign an integer-required area with a string-
required area, you get a conversion error like OP described as:

"Conversion from string "" to type 'Integer' is not valid."

Some workarounds may overcome this issue.

Sorry if there was misunderstanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top