Results 1 to 9 of 9

Thread: Creating an object as nothing

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Creating an object as nothing

    Is there a way to make it so if the employee requested does not exist that the object returns nothing?

    Normally I create a Boolean property for Found and check it's value, but I was thinking this would work better, but I don't know what command to put into the else statement to make the "new" object equal nothing.

    Is there a way, or should I just create a Boolean for no such employee?


    VB Code:
    1. Sub FindEmp()
    2.  
    3.             Dim Emp As EmpCL.Emp = New EmpCL.Emp  (Me.txtEmpID.Text)
    4.  
    5.             If Emp Is Nothing Then
    6.                 MsgBox("Employee Not Found")
    7.             Else
    8.  
    9. End Sub
    10.  
    11. Public Class Emp
    12.     Public Name As String
    13.     Public ID As Integer
    14.  
    15.     Sub New(ByVal id As Integer)
    16.  
    17.         Dim db As New dbSettings.SQL
    18.         Dim cn As SqlConnection = New SqlConnection(db.CN)
    19.         Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblEmp where idEmp = @idEmp", cn)
    20.         cmd.Parameters.Add("@idEmp", id)
    21.         cn.Open()
    22.  
    23.         Dim dr As SqlDataReader = cmd.ExecuteReader
    24.  
    25.         If dr.Read Then
    26.  
    27.             Name = dr("Name")
    28.             ID = id
    29.         Else
    30.             'Me.Dispose() ??
    31.  
    32.         End If
    33.  
    34.  
    35.     End Sub
    36. End Class
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    return nothing if the name is not found in the database?

    then just type

    name = nothing

  3. #3

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Name? as in Me = Nothing ?? it doesn't like that. I need somthing inside the class that kills itself.

    VB Code:
    1. Sub New(ByVal id As Integer)
    2.  
    3.         Dim db As New dbSettings.SQL
    4.         Dim cn As SqlConnection = New SqlConnection(db.CN)
    5.         Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblScanners where idScanner = @idScanner", cn)
    6.         cmd.Parameters.Add("@idScanner", id)
    7.         cn.Open()
    8.  
    9.         Dim dr As SqlDataReader = cmd.ExecuteReader
    10.  
    11.         If dr.Read Then
    12.  
    13.             mName = dr("Name")
    14.             If dr("Active") Is DBNull.Value Then mActive = True
    15.             mID = id
    16.         Else
    17.             Me.Dispose()
    18.             Me = Nothing
    19.         End If
    20.  
    21.  
    22.     End Sub
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Originally posted by Dmyze
    Name? as in Me = Nothing ?? it doesn't like that. I need somthing inside the class that kills itself.

    VB Code:
    1. Sub New(ByVal id As Integer)
    2.  
    3.         Dim db As New dbSettings.SQL
    4.         Dim cn As SqlConnection = New SqlConnection(db.CN)
    5.         Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblScanners where idScanner = @idScanner", cn)
    6.         cmd.Parameters.Add("@idScanner", id)
    7.         cn.Open()
    8.  
    9.         Dim dr As SqlDataReader = cmd.ExecuteReader
    10.  
    11.         If dr.Read Then
    12.  
    13.             mName = dr("Name")
    14.             If dr("Active") Is DBNull.Value Then mActive = True
    15.             mID = id
    16.         Else
    17.             Me.Dispose()
    18.             Me = Nothing
    19.         End If
    20.  
    21.  
    22.     End Sub
    What you have here should work. Me = Nothing will assign null to the instantiated object (itself)

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't think Me can be the target of an assignment. Although lets look at the logic here, why do you want to set the object to nothing or dispose in the constructor? I'm not a pro-lifer or anything but it would seem that this code (the constructor) would only be executed when someone is trying to create an instance of the object. Anyway if you still desire to do this then the Dispose call will work but not the Me=Nothing (I don't think). You can probably trick it though:
    VB Code:
    1. Private Sub SetToNothing(obj As Object)
    2.   obj=Nothing
    3. End Sub
    4.  
    5. 'then call this instead of Me=Nothing
    6. SetToNothing(Me)

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually I tested it and it doesn't work. It seems that an object can not set itself to nothing. I guess it kind of makes sense considering it would require a reference to the object in order to use the object to set it to nothing. Its like some Paradox thing.

    Although if you dispose it there really isn't any need to set it to nothing the GC will still clean it up when there are no references to it, shoot this will happen even if you don't dispose it.

  7. #7

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    It just seemed to make sense to me, if you request an employee that does not exit, the object should fail to create itself. the only other option I can think of is to create an "exist" property and when it can't find an employee it just sets it's exist property to false.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yup Edneesis is right, it will not work.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Dmyze
    It just seemed to make sense to me, if you request an employee that does not exit, the object should fail to create itself. the only other option I can think of is to create an "exist" property and when it can't find an employee it just sets it's exist property to false.
    I think the best thing in this instance is still not to set it to nothing but if you want the same effect then throw an exception.

    Throw New ApplicationException("Employee not found!")

    Or make your one EmployeeNotFound Exception.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width