8 Replies - 6754 Views - Last Post: 14 April 2009 - 05:41 PM Rate Topic: -----

#1 du_jleon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 26-March 09

In VB, try catch

Post icon  Posted 26 March 2009 - 04:39 PM

I had a very bad VB teacher and now I am taking another course but I feel short changed because I did not get the basics down, though I passed the class.
I can’t remember how to do what is being suggested to me “It is best that if your method is not returning any data its best to declare your function/method as void in this case in VB. as Sub.”
Now my Try Catch is coming up with an error "Warning 2 Function 'SaveUserActivity' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."
 
' This function saves the SavePersonnel
	Public Shared Function SavePersonnel(ByVal Database As String, ByVal FirstName As String, ByVal LastName As String, ByVal PayRate As Integer, ByVal StartDate As DateTime, ByVal EndDate As DateTime)
		Try
			' Add your comments here
			Dim conn As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
			"Data Source=" & Database & "")
			conn.Open()
			Dim command As OleDbCommand = conn.CreateCommand()
			Dim strSQL As String
			' Add your comments here
			strSQL = "Insert into tblPersonnel (FirstName, LastName, PayRate, StartDate, EndDate) values ('" & _
					FirstName & "', '" & LastName & "', " & PayRate & ", '" & StartDate & "', '" & EndDate & "')"
			' Add your comments here
			command.CommandType = CommandType.Text
			command.CommandText = strSQL
			' Add your comments here
			command.ExecuteNonQuery()
			' Add your comments here
			conn.Close()
			SavePersonnel = True
		Catch ex As Exception
			SavePersonnel = False
		End Try
		‘Saying to add void here, but I am confused’
	End Function


Thanks,
du_jleon

Is This A Good Question/Topic? 0
  • +

Replies To: In VB, try catch

#2 du_jleon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 26-March 09

Re: In VB, try catch

Post icon  Posted 26 March 2009 - 04:52 PM

I had a very bad VB teacher and now I am taking another course but I feel short changed because I did not get the basics down, though I passed the class.
I can’t remember how to do what is being suggested to me “It is best that if your method is not returning any data its best to declare your function/method as void in this case in VB. as Sub.”
Now my Try Catch is coming up with an error "Warning 2 Function 'SaveUserActivity' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."
 
' This function saves the SavePersonnel
	Public Shared Function SavePersonnel(ByVal Database As String, ByVal FirstName As String, ByVal LastName As String, ByVal PayRate As Integer, ByVal StartDate As DateTime, ByVal EndDate As DateTime)
		Try
			' Add your comments here
			Dim conn As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
			"Data Source=" & Database & "")
			conn.Open()
			Dim command As OleDbCommand = conn.CreateCommand()
			Dim strSQL As String
			' Add your comments here
			strSQL = "Insert into tblPersonnel (FirstName, LastName, PayRate, StartDate, EndDate) values ('" & _
					FirstName & "', '" & LastName & "', " & PayRate & ", '" & StartDate & "', '" & EndDate & "')"
			' Add your comments here
			command.CommandType = CommandType.Text
			command.CommandText = strSQL
			' Add your comments here
			command.ExecuteNonQuery()
			' Add your comments here
			conn.Close()
			SavePersonnel = True
		Catch ex As Exception
			SavePersonnel = False
		End Try
		‘Saying to add void here, but I am confused.’
	End Function


Thanks,
du_jleon
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: In VB, try catch

Posted 26 March 2009 - 05:19 PM

I think you're getting Function confused with Sub

To my knowledge, a Function must always return something. A Sub does not.
Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • Posts: 1,081
  • Joined: 05-June 08

Re: In VB, try catch

Posted 26 March 2009 - 05:30 PM

as far as my understanding of it goes,

if you're not returning a value use a sub

if you want to return a value the use a function

public function myFunc() as string
dim returnstring as string = ""

blah blah blah

return returnstring

end function




you declare your function and put the return type at the end (the as part)
i declare returnstring as empty, that way if it doesn't get updated i'm still returning something and my program won't bitch.

As for returning void, wellll i dunno. Void is a type and you would still have to declare a variable of that type i think. Someone correct me if i'm wrong here

This post has been edited by Damage: 26 March 2009 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: In VB, try catch

Posted 27 March 2009 - 06:36 AM

where is this 'SaveUserActivity' used at?

also where is the error occurring, step through the program and find out.

This post has been edited by krum110487: 27 March 2009 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 TheBeege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 01-April 09

Re: In VB, try catch

Posted 01 April 2009 - 01:29 AM

View PostDamage, on 26 Mar, 2009 - 04:30 PM, said:

as far as my understanding of it goes,

if you're not returning a value use a sub

if you want to return a value the use a function

public function myFunc() as string
dim returnstring as string = ""

blah blah blah

return returnstring

end function




you declare your function and put the return type at the end (the as part)
i declare returnstring as empty, that way if it doesn't get updated i'm still returning something and my program won't bitch.

As for returning void, wellll i dunno. Void is a type and you would still have to declare a variable of that type i think. Someone correct me if i'm wrong here


Void means... well, void. It means there's nothing there, i.e. nothing is returned. This is true of most, if not all, languages. There are void pointers in C, but that's a whole separate issue...
Was This Post Helpful? 0
  • +
  • -

#7 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: In VB, try catch

Posted 01 April 2009 - 04:32 AM

Hi!

You say you get an error (or, I guess a warning) like this: "Warning 2 Function 'SaveUserActivity'..." and then you show us code for a function named SavePersonnel. Can you please show the code for function SaveUserActivity?

If you are not going to return anything use a Sub.
If you intend to return True / False you must declare your function as Boolean like this: Private Function SavePersonnel(ByVal Database As String, ByVal FirstName As String, ByVal LastName As String, ByVal PayRate As Integer, ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Boolean

If you want to use a function - in order to return somthing - then you should have a Return statement, like - in this case - Return True and Return False.

Regards
/Jens

This post has been edited by jens: 01 April 2009 - 04:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 microstoc  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: In VB, try catch

Posted 04 April 2009 - 08:46 PM

Sounds like the function SaveUserActivity is being exited before any value is assigned to SaveUserActivity

example:

Function SaveUserActivity as boolean

If (some test fails) then
'saveuseractivity has not been assigned a value here
Exit Function
Else
SaveUserActivity=true
Exit Function
End If

End Function
Was This Post Helpful? 0
  • +
  • -

#9 du_jleon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 26-March 09

Re: In VB, try catch

Posted 14 April 2009 - 05:41 PM

Thanks all that help it is work!!! :^: :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1