|
-
Dec 5th, 2003, 04:23 PM
#1
Thread Starter
Addicted Member
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:
Sub FindEmp()
Dim Emp As EmpCL.Emp = New EmpCL.Emp (Me.txtEmpID.Text)
If Emp Is Nothing Then
MsgBox("Employee Not Found")
Else
End Sub
Public Class Emp
Public Name As String
Public ID As Integer
Sub New(ByVal id As Integer)
Dim db As New dbSettings.SQL
Dim cn As SqlConnection = New SqlConnection(db.CN)
Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblEmp where idEmp = @idEmp", cn)
cmd.Parameters.Add("@idEmp", id)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.Read Then
Name = dr("Name")
ID = id
Else
'Me.Dispose() ??
End If
End Sub
End Class
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Dec 5th, 2003, 04:43 PM
#2
Frenzied Member
return nothing if the name is not found in the database?
then just type
name = nothing
-
Dec 5th, 2003, 04:47 PM
#3
Thread Starter
Addicted Member
Name? as in Me = Nothing ?? it doesn't like that. I need somthing inside the class that kills itself.
VB Code:
Sub New(ByVal id As Integer)
Dim db As New dbSettings.SQL
Dim cn As SqlConnection = New SqlConnection(db.CN)
Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblScanners where idScanner = @idScanner", cn)
cmd.Parameters.Add("@idScanner", id)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.Read Then
mName = dr("Name")
If dr("Active") Is DBNull.Value Then mActive = True
mID = id
Else
Me.Dispose()
Me = Nothing
End If
End Sub
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Dec 5th, 2003, 10:51 PM
#4
Frenzied Member
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:
Sub New(ByVal id As Integer)
Dim db As New dbSettings.SQL
Dim cn As SqlConnection = New SqlConnection(db.CN)
Dim cmd As SqlCommand = New SqlCommand("SELECT * from tblScanners where idScanner = @idScanner", cn)
cmd.Parameters.Add("@idScanner", id)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.Read Then
mName = dr("Name")
If dr("Active") Is DBNull.Value Then mActive = True
mID = id
Else
Me.Dispose()
Me = Nothing
End If
End Sub
What you have here should work. Me = Nothing will assign null to the instantiated object (itself)
-
Dec 6th, 2003, 12:10 AM
#5
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:
Private Sub SetToNothing(obj As Object)
obj=Nothing
End Sub
'then call this instead of Me=Nothing
SetToNothing(Me)
-
Dec 6th, 2003, 12:17 AM
#6
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.
-
Dec 6th, 2003, 06:57 PM
#7
Thread Starter
Addicted Member
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
-
Dec 6th, 2003, 07:36 PM
#8
Frenzied Member
Yup Edneesis is right, it will not work.
-
Dec 6th, 2003, 11:38 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|