How to use a Function's Return value

What's the point and how does it work?

Page 1 of 1

3 Replies - 9536 Views - Last Post: 09 March 2009 - 02:23 PM Rate Topic: -----

#1 jase81  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 02-March 09

How to use a Function's Return value

Post icon  Posted 08 March 2009 - 07:34 PM

So the program I am writing validates the controls on the page on button_click.

I want to write a function which checks which step the program is at (0-7) then return something depending on whether or not it validated.

My plan was to return the control which was not valid.

	Private Function ValidateInputs() As Control

		Select Case wizardStep

			Case 0
				If Trim(txtContractName.Text) <> "" Then
					Return txtContractName
				End If

				If Trim(txtContractRef.Text) = "" Then
					Return txtContractRef
				End If

				If dtpContractEnd.Value < dtpContractStart.Value Then
					Return dtpContractStart
				End If

			Case 1


		End Select

		Return btnNext

	End Function



Now I've just started, but before I go any further I need some help.

1. could I return the names off all the controls which did not validate?
2. should I be returning a control type or would something like a string be better?
3. SOLVED
4. is this a stupid way to do it/what would be a better idea?

This post has been edited by jase81: 08 March 2009 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to use a Function's Return value

#2 krum110487  Icon User is offline

  • D.I.C Regular

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

Re: How to use a Function's Return value

Posted 09 March 2009 - 01:45 PM

View Postjase81, on 8 Mar, 2009 - 06:34 PM, said:

So the program I am writing validates the controls on the page on button_click.

I want to write a function which checks which step the program is at (0-7) then return something depending on whether or not it validated.

My plan was to return the control which was not valid.

	Private Function ValidateInputs() As Control

		Select Case wizardStep

			Case 0
				If Trim(txtContractName.Text) <> "" Then
					Return txtContractName
				End If

				If Trim(txtContractRef.Text) = "" Then
					Return txtContractRef
				End If

				If dtpContractEnd.Value < dtpContractStart.Value Then
					Return dtpContractStart
				End If

			Case 1


		End Select

		Return btnNext

	End Function



Now I've just started, but before I go any further I need some help.

1. could I return the names off all the controls which did not validate?
2. should I be returning a control type or would something like a string be better?
3. SOLVED
4. is this a stupid way to do it/what would be a better idea?

This idea is fine, but sometimes less is more.

if you are looking to have a validate function, just have it return true or false

because either the "form" will continue (be validated) or it wont, you can then have the validation function do what it needs like set focus or pop up a message box.

if you want to return the controls, You could return an array of the controls, but I think it would be easier to just handle the validation within the function itself.

other than that, I believe there is a validation control, which will do it in real time, I can't remember the exact name, but it will put a red "!" or a green triangle... I wish I had VB.net in front of me so I could be more specific.

I would personally go with true or false with the function.
Was This Post Helpful? 1
  • +
  • -

#3 jase81  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 02-March 09

Re: How to use a Function's Return value

Posted 09 March 2009 - 02:20 PM

View Postkrum110487, on 9 Mar, 2009 - 12:45 PM, said:

This idea is fine, but sometimes less is more.

if you are looking to have a validate function, just have it return true or false

because either the "form" will continue (be validated) or it wont, you can then have the validation function do what it needs like set focus or pop up a message box.

if you want to return the controls, You could return an array of the controls, but I think it would be easier to just handle the validation within the function itself.

other than that, I believe there is a validation control, which will do it in real time, I can't remember the exact name, but it will put a red "!" or a green triangle... I wish I had VB.net in front of me so I could be more specific.

I would personally go with true or false with the function.


Hi, thanks for the advice!

I'm using the errorprovider which is the little red exclamation mark. It's working pretty well.
I also tried using an ArrayList of controls which worked better than having a function return them.

I'll have a go with booleans as you suggested. I'm not making as much progress as I should be considering it's not really an important part of the project.

Cheers!
Was This Post Helpful? 0
  • +
  • -

#4 krum110487  Icon User is offline

  • D.I.C Regular

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

Re: How to use a Function's Return value

Posted 09 March 2009 - 02:23 PM

AH, ErrorProvider! I wont forget the name of that next time :-P
good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1