6 Replies - 350 Views - Last Post: 04 October 2012 - 07:18 PM Rate Topic: -----

#1 abenavente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 12

Simplifying Example Code Into a Function

Posted 03 October 2012 - 05:47 PM

Hi, have a bit of code I've been working on and I need help simplifying it. I know there's a way to make this code into a function because I have to reuse the same code throughout 10 other forms.

For i As Integer = 0 To 8
            If Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDKEY1 Then
                Select Case i
                    Case 0
                        guiInvSlot1.Image = My.Resources.Key_1_Inventory
                        guiInvSlot1.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 1
                        guiInvSlot2.Image = My.Resources.Key_1_Inventory
                        guiInvSlot2.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 2
                        guiInvSlot3.Image = My.Resources.Key_1_Inventory
                        guiInvSlot3.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 3
                        guiInvSlot4.Image = My.Resources.Key_1_Inventory
                        guiInvSlot4.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 4
                        guiInvSlot5.Image = My.Resources.Key_1_Inventory
                        guiInvSlot5.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 5
                        guiInvSlot6.Image = My.Resources.Key_1_Inventory
                        guiInvSlot6.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 6
                        guiInvSlot7.Image = My.Resources.Key_1_Inventory
                        guiInvSlot7.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 7
                        guiInvSlot8.Image = My.Resources.Key_1_Inventory
                        guiInvSlot8.Tag = Inventory.SLOTOCCUPIEDKEY1
                    Case 8
                        guiInvSlot9.Image = My.Resources.Key_1_Inventory
                        guiInvSlot9.Tag = Inventory.SLOTOCCUPIEDKEY1
                End Select
            ElseIf Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDKEY2 Then
                Select Case i
                    Case 0
                        guiInvSlot1.Image = My.Resources.Key_2_Inventory
                        guiInvSlot1.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 1
                        guiInvSlot2.Image = My.Resources.Key_2_Inventory
                        guiInvSlot2.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 2
                        guiInvSlot3.Image = My.Resources.Key_2_Inventory
                        guiInvSlot3.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 3
                        guiInvSlot4.Image = My.Resources.Key_2_Inventory
                        guiInvSlot4.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 4
                        guiInvSlot5.Image = My.Resources.Key_2_Inventory
                        guiInvSlot5.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 5
                        guiInvSlot6.Image = My.Resources.Key_2_Inventory
                        guiInvSlot6.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 6
                        guiInvSlot7.Image = My.Resources.Key_2_Inventory
                        guiInvSlot7.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 7
                        guiInvSlot8.Image = My.Resources.Key_2_Inventory
                        guiInvSlot8.Tag = Inventory.SLOTOCCUPIEDKEY2
                    Case 8
                        guiInvSlot9.Image = My.Resources.Key_2_Inventory
                        guiInvSlot9.Tag = Inventory.SLOTOCCUPIEDKEY2
                End Select
            ElseIf Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDSTOOL Then
                Select Case i
                    Case 0
                        guiInvSlot1.Image = My.Resources.stool
                        guiInvSlot1.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 1
                        guiInvSlot2.Image = My.Resources.stool
                        guiInvSlot2.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 2
                        guiInvSlot3.Image = My.Resources.stool
                        guiInvSlot3.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 3
                        guiInvSlot4.Image = My.Resources.stool
                        guiInvSlot4.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 4
                        guiInvSlot5.Image = My.Resources.stool
                        guiInvSlot5.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 5
                        guiInvSlot6.Image = My.Resources.stool
                        guiInvSlot6.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 6
                        guiInvSlot7.Image = My.Resources.stool
                        guiInvSlot7.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 7
                        guiInvSlot8.Image = My.Resources.stool
                        guiInvSlot8.Tag = Inventory.SLOTOCCUPIEDSTOOL
                    Case 8
                        guiInvSlot9.Image = My.Resources.stool
                        guiInvSlot9.Tag = Inventory.SLOTOCCUPIEDSTOOL
                End Select
            End If
        Next



I've tried making a sub in the class "Inventory" but I don't think you could have the form that currently accesses the class in the parameter. Ex.
 Public Sub reloadInventory(ByVal theFormCurrentlyAccessingThisSub As Form)
        ......
    End Sub


I'm sorry if this is a bit unclear. It's kind of hard to explain.

Oops I didn't know how to use the code format sorry :dontgetit:

Here's easier read of the first block of code.
Mentor Edit: Re-positioned the formatted code to back to the start.

This post has been edited by AdamSpeight2008: 03 October 2012 - 06:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Simplifying Example Code Into a Function

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 288
  • Joined: 22-August 11

Re: Simplifying Example Code Into a Function

Posted 03 October 2012 - 07:14 PM

its basically

 
public Function ThisIsMyFunction()
        '   do your code here
        Return someVariable
    End Function


I'd recommend re-reading this section of your book. Most of the one's I've seen explain this pretty well.

This post has been edited by torind_2000: 03 October 2012 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 abenavente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 12

Re: Simplifying Example Code Into a Function

Posted 03 October 2012 - 07:24 PM

View Posttorind_2000, on 03 October 2012 - 07:14 PM, said:

its basically

 
public Function ThisIsMyFunction()
        '   do your code here
        Return someVariable
    End Function


I'd recommend re-reading this section of your book. Most of the one's I've seen explain this pretty well.


Thanks but that's not quite what I was asking. I know how to create functions, I just wanted to know if I could have a form be a parameter of a function. And not just a basic form, but I want the currently active form as the parameter. The second block of code I had in my OP kind of explained what I was going for a little bit better.
Was This Post Helpful? 0
  • +
  • -

#4 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 288
  • Joined: 22-August 11

Re: Simplifying Example Code Into a Function

Posted 03 October 2012 - 07:48 PM

maybe this is what you're looking for then.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: Simplifying Example Code Into a Function

Posted 03 October 2012 - 08:22 PM

I don't know if I follow you 100%, but if your function is in the class the class instance is known by the Me keyword, so I don't know why you would need to pass that as a parameter - it is available directly in the function. Also in a Select Case where diff values have the same response you can use this:

Select Case {Int}
  Case 1 To 5
  Case 6, 7, 8
  Case Else
    'if needed
End Select

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Simplifying Example Code Into a Function

Posted 04 October 2012 - 05:57 AM

Along with _HAWK_'s post I would like to offer my observation.

It appears you are looking at 8 inventory items indexes of 0 to 8

First Suggestion for what I'm about to suggest, rename your guiInvSlot controls from 1 to 9 to 0 to 8. It will just make things easier to read
guiInvSlot0
guiInvSlot1
...
guiInvSlot8

Now your number matches the index and we can use that index to find the control you want to access.

It appears that if you are on .currentInventory(4) and it equals .SLOTOCCUPIEDKEY1 then you want guiInvSlot4.Image to be set to Key1 image and the .Tag to be set to .SLOTOCCUPIEDKEY1.

So... Instead of all these select case statements you could try:
'Remember guiInvSlot controls need to be renamed guiInvSlot0 to guiInvSlot 8
Dim pbToChange as Picturebox
For i As Integer = 0 To 8
  pbToChange = CType(Me.Controls("guiInvSlot" & i), Picturebox)
  If Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDKEY1 Then
    pbToChange.Image = My.Resources.Key_1_Inventory
    pbToChange.Tag = Inventory.SLOTOCCUPIEDKEY1
  ElseIf Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDKEY2 Then
    pbToChange.Image = My.Resources.Key_2_Inventory
    pbToChange.Tag = Inventory.SLOTOCCUPIEDKEY2
  ElseIf Inventory.currentInventory(i) = Inventory.SLOTOCCUPIEDSTOOL Then
    pbToChange.Image = My.Resources.stool
    pbToChange.Tag = Inventory.SLOTOCCUPIEDSTOOL
End If

So what is this code doing?
first loop, i = 0
Cast the control named guiInvSlot0 me.controls("guiInvSlot"& i) to a picturebox so we can access the picturebox properties of the control.
if Inventory.currentInventory(0) = Inventory.SLOTOCCUPIEDKEY1 then we want to set guiInvSlot0's image and tag properties.

Now are there even better ways of doing what you're wanting to achieve? Yes, but with what you've got started, this should greatly reduce the code you have above.
Was This Post Helpful? 0
  • +
  • -

#7 abenavente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 12

Re: Simplifying Example Code Into a Function

Posted 04 October 2012 - 07:18 PM

Thanks for the replies! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1