making a function

making a function to avoid long repeating code

Page 1 of 1

4 Replies - 846 Views - Last Post: 18 September 2008 - 04:54 PM Rate Topic: -----

#1 xavierci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-August 08

making a function

Posted 09 September 2008 - 07:18 AM

I'm trying to convert this into a function or sub so I only have to call the code from one place with a small line.
This is currently written in 6 different places all with the same code.
Can I turn this in to a function?

everytime I inclosed it in a function it asks me to declare all the labels and pictureboxes?!

If (Middle = GameMode + 1) Then
			Bet = 1
			Label6.Text = Bet * Unit
			If (Sound = 1) Then
				My.Resources.cash.Read(bStr, 0, My.Resources.cash.Length)
				My.Computer.Audio.Play(bStr, AudioPlayMode.Background)
			End If
		End If

		If (Middle = GameMode + 2) Then
			Bet = 2
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 3) Then
			Bet = 3
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 4) And (Mode = 1) Then
			Bet = 4
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 4) And (Mode = 2) Then
			Bet = 5
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 5) And (Mode = 1) Then
			Bet = 6
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 5) And (Mode = 2) Then
			Bet = 8
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 6) And (Mode = 1) Then
			Bet = 9
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 6 And Mode = 2) Then
			Bet = 13
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 7) And (Mode = 1) Then
			Bet = 13
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 7) And (Mode = 2) Then
			Bet = 22
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 8) And (Mode = 1) Then
			Bet = 20
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 8) And (Mode = 2) Then
			Bet = 36
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 9) And (Mode = 1) Then
			Bet = 30
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 9) And (Mode = 2) Then
			Bet = 55
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 10) And (Mode = 1) Then
			Bet = 45
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 10) And (Mode = 2) Then
			Bet = 83
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 11) And (Mode = 1) Then
			Bet = 68
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 11) And (Mode = 2) Then
			Bet = 124
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 12) And (Mode = 1) Then
			Bet = 102
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 12) And (Mode = 2) Then
			Bet = 186
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 13) And (Mode = 1) Then
			Bet = 153
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 13) And (Mode = 2) Then
			Bet = 279
			Label6.Text = Bet * Unit
		End If

		If (Middle = GameMode + 14) And (Mode = 1) Then
			Bet = 229
			Label6.Text = Bet * Unit
		ElseIf (Middle = GameMode + 14) And (Mode = 2) Then
			Bet = 419
			Label6.Text = Bet * Unit
		End If

		If (Middle >= GameMode + 15) Then
			PictureBox2.BackColor = Color.Red
			Label6.Text = "QUIT"
		End If



Is This A Good Question/Topic? 0
  • +

Replies To: making a function

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: making a function

Posted 09 September 2008 - 11:20 AM

how are you declaring the function? Let's see that code..
Was This Post Helpful? 0
  • +
  • -

#3 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: making a function

Posted 10 September 2008 - 04:45 AM

There is no way labels and pictureboxes prompts for declaration if you are declaring the function inside current form. So my guess is that you are declaring the function outside the scope of controls. So check for the same or post the code so that we can suggest you what changes to make...
Was This Post Helpful? 0
  • +
  • -

#4 Poppa Mintin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 13-September 08

Re: making a function

Posted 15 September 2008 - 05:52 PM

Ok hereís the general idea of how to write a function.
You have to find a Name for the function, make the name
describe the function but donít pick a reserved word.

Private function NAME (ByVal GM as Integer, _
	ByVal MD as Integer, ByVal UT as Integer, _
	ByVal MO as Integer, ByVal SD as Integer) as Integer

Put all your code here, but use the GM, MD variables instead of 
the ones in the main code, you can use whatever variable 
names you wish in the function, Iíve used GM, MD etc so that 
you can follow the idea. 
These variables are automatically declared, but you have to 
declare each separately as Iíve done, even though, as in this 
case, theyíre all integers. Oh.. note also that the Ďas Integerí 
after the closing bracket is the declaration for NAME, which is 
the variable you will pass back to the calling line.

A function MUST return a result, so the last line of a function 
will be something like this:-.

NAME = The result you want for Bet. 

End Function



Call the function in each of the places you need it thus:-

Bet = NAME (GameMode, Middle, Unit, Mode, Sound)

You can change Label6 text and Me.Anything inside the function.
If you want the function to change the value of any or all of the
parameters use ByRef in stead of ByVal where you want that to
happen.
If you do want all the variables to be able to be changed, youíd
be better to write it as a subroutine but in that case you canít
get a result returned, and youíd call it thus:-

NAME (GameMode, Middle, Unit, Mode, Sound, Bet)

Oh... and of course, the parameters have to be in the same
ORDER as they are declared !

When the program gets to this, it will branch off and do itís thing
then return to the next line following the call. Notice you have
to include Bet in the parameter list this time. OhÖ one last thing,
in the declaration of the function (or subroutine), you will see Iíve
used a space and underscore character twice, this is to reduce
the length of the declaration so that you can read it in stead of
having it all in the one line (which is how the computer will see it.)

Poppa
Was This Post Helpful? 0
  • +
  • -

#5 xavierci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-August 08

Re: making a function

Posted 18 September 2008 - 04:54 PM

I kinda head back to my vb.net book and this some tutorials and thing I figured it out
This kinda gave me the result that I needed even though I have to have the code x3 because of the total variable.
Going to try and see if I can't get that fixed somehow. But thanks for your reply. Help me get more insight.

There are 3 different total values at the moment - total1 total2 and total3. how can I make it so that depending on which value the function receives the total changes to the right one???

Function dozen(ByVal part As Integer) As Integer

		If (part = GameMode + 1) Then
			Bet = 1
			Total = (Bet * Unit)
		End If

		If (part = GameMode + 2) Then
			Bet = 2
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 3) Then
			Bet = 3
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 4) And (Mode = 1) Then
			Bet = 4
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 4) And (Mode = 2) Then
			Bet = 5
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 5) And (Mode = 1) Then
			Bet = 6
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 5) And (Mode = 2) Then
			Bet = 8
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 6) And (Mode = 1) Then
			Bet = 9
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 6) And (Mode = 2) Then
			Bet = 13
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 7) And (Mode = 1) Then
			Bet = 13
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 7) And (Mode = 2) Then
			Bet = 22
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 8) And (Mode = 1) Then
			Bet = 20
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 8) And (Mode = 2) Then
			Bet = 36
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 9) And (Mode = 1) Then
			Bet = 30
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 9) And (Mode = 2) Then
			Bet = 55
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 10) And (Mode = 1) Then
			Bet = 45
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 10) And (Mode = 2) Then
			Bet = 83
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 11) And (Mode = 1) Then
			Bet = 68
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 11) And (Mode = 2) Then
			Bet = 124
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 12) And (Mode = 1) Then
			Bet = 102
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 12) And (Mode = 2) Then
			Bet = 186
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 13) And (Mode = 1) Then
			Bet = 153
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 13) And (Mode = 2) Then
			Bet = 279
			Total = Total + (Bet * Unit)
		End If

		If (part = GameMode + 14) And (Mode = 1) Then
			Bet = 229
			Total = Total + (Bet * Unit)
		ElseIf (part = GameMode + 14) And (Mode = 2) Then
			Bet = 456
			Total = Total + (Bet * Unit)
		End If

		Return Bet
		Return Total
	End Function


This post has been edited by xavierci: 18 September 2008 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1