Is this a valid code?

A

amiga500

Hello,

I made this function so that it can return 1 or 2 where 1 either equal
PieIX or Mendonta or 2 equal either PieIX or Mendonta. Let me go
further in details...in the program it shows all Inventory information
in the datagrid and one column in the datagrid there is a combo box
where I can choose either PieIX or Mendonta and depending on which of
those I select first would determine wether 1 is pieIx and 2 Mendonta
or 1 is Mendonta or 2 is PieIX. For example as below:

ItemNumber AlbumTitle Fulfillment
BO 2222 Best of Poco Lives Mendonta
BO 3333 BoulderDash PieIX

The function below should return 1 for mendonta and 2 for pieIX but if
I decided to do this instead:

ItemNumber AlbumTitle Fulfillment
BO 2222 Best of Poco Lives PieIX
BO 3333 BoulderDash Mendonta

Now this function below should return 1 for PieIX and 2 for Mendonta,
I have this problem what if the function causes a malfunction and then
returns both 1 and 2 PieIX or 1,2 Mendonta, etc, so I just wanted an
outside eye to tell me if there is any bug in my coding? Thanks in
advance.


Below is my code:

Private Function getFulfillment(ByVal strFulfillment As String) As
Integer
Dim intCounter As Integer = Nothing
Dim blAgain As Boolean = False

tryAgain:
If strFulfillment = "Lasalle" Then Exit Function

If strFulfillment = "" Then Exit Function

If blAgain = True Then
blAgain = False
If objTypes(0).strName = objTypes(1).strName Then Return 1
If objTypes(0).strName = "" And objTypes(1).strName <> ""
Then Return 2
If objTypes(0).strName <> "" And objTypes(1).strName = ""
Then Return 1

End If

If objTypes(bytNextFulfillment).strName = "" Then

If objTypes(0).strName = "Pie-IX" And objTypes(1).strName
= "Pie-IX" Then GoTo COMPLETE
If objTypes(1).strName = "Mendota" And objTypes(1).strName
= "Mendota" Then GoTo COMPLETE

intNewValue = intNewValue + 1
If intNewValue > 2 Then intNewValue = 2
If bytNextFulfillment > 1 Then bytNextFulfillment = 1

If objTypes(bytNextFulfillment).strName = "" Then
objTypes(bytNextFulfillment).bytType = intNewValue
objTypes(bytNextFulfillment).strName = strFulfillment
bytNextFulfillment = bytNextFulfillment + 1

End If
End If

If objTypes(0).strName = "Pie-IX" And objTypes(1).strName =
"Pie-IX" Then objTypes(1).strName = "" : bytNextFulfillment = 0 :
blAgain = True : GoTo tryAgain
If objTypes(0).strName = "Mendota" And objTypes(1).strName =
"Mendota" Then objTypes(1).strName = "" : bytNextFulfillment = 0 :
blAgain = True : GoTo tryAgain
COMPLETE:
If bytNextFulfillment > 1 Then bytNextFulfillment = 1

For intCounter = 0 To 1
If objTypes(intCounter).strName = strFulfillment Then
Return objTypes(intCounter).bytType
Next intCounter

End Function
 
M

Morten Wennevik [C# MVP]

Hi,

Your code does not compile, so I can't say for sure if your real code will work, but I can say you won't ever get both 1 and 2 or 1,2 from this function. Once you return a value (return 1 or return 2), that's it. Of, and Exit Function is equivalent of Return whatever value 'getFullfillment' holds, which would be the default Integer value 0, I believe. Thenagain, VB.Net is not my area, so I may be wrong.

I would probably not use Labels and Goto statements, and it looks to me like you are overcomplicating things quite a bit, but I see nothing particularly wrong with your code.

objTypes is undefined, bytNextFulfillment is undefined, intNewValue is undefined.
 
A

amiga500

You cannot take my code as is and compile it there as so many other
functions and variables out there are needed to get this code working.
I just wanted to know if you follow the steps, one by one (as if you
are the computer) and see the logic if it works.
 
A

amiga500

But you said I over complicate things with this code. I am just
curious, what other method you would approach where 1 is either PieIX,
Mendonta or 2 is either PieIx, Mendota and what ever the value of 1 is
it cannot be the same in value of 2?
 
G

Guest

First of all, the GOTO statement is so 1990, dont use it, bad coding!
Instead think of functions where you use GOTO.

What you want to do is check wether a string = "Pie-IX" or "Mendota" where
pie-ix=1 and mendota=2?

Private Function checkString(ByVal str as string) As Integer
{
if str="Pie-IX" then
return 1
else'if str="Mandota"
return 2
end if
}

Figa
 
A

amiga500

But that is the thing Pie-IX isn't necessarly 1 and Mandota 2.

For example I can chose this list

1) PieIX
2) PieIX
3) Mandota
4) Mandota

in this scenario PieIX IS ONE and Mandota is TWO

but if it is like this:

1) Mandota
2) PieIX
3) PieIX
4) Mandota

then Mandota is ONE and PieIX is two and all the Mandotas there in 1
and 4 is equal to 1 and PieIX in 2,3 equal to 2 the same goes to the
preview list 1,2 for PieIX is 1 and 3,4 for Mandota is 1 but you can
never have PieIX, Mandota to be 1 or PieIX, PieIX to be 1 or Mandota,
PieIX to be 2, or Mandota, Mondota to be 2, etc.
 
A

amiga500

I have did further revision to the code because I noticed some
serious bugs on them and it is like this now:

Private Function getFulfillment(ByVal strFulfillment As String) As
Integer
Dim intCounter As Integer = Nothing
Dim blAgain As Boolean = False

tryAgain:
If strFulfillment = "Lasalle" Then Exit Function

If strFulfillment = "" Then MessageBox.Show("Fulfillment is
null.", "", MessageBoxButtons.OK, MessageBoxIcon.Error) : Exit
Function

If blAgain = True Then
blAgain = False
If objTypes(0).strName = objTypes(1).strName Then Return 1
If objTypes(0).strName = "" And objTypes(1).strName <> ""
Then Return 2
If objTypes(0).strName <> "" And objTypes(1).strName = ""
Then Return 1

End If

If objTypes(bytNextFulfillment).strName = "" And
objTypes(0).strName <> strFulfillment Then

intNewValue = intNewValue + 1
If intNewValue > 2 Then intNewValue = 2
If bytNextFulfillment > 1 Then bytNextFulfillment = 1

If objTypes(bytNextFulfillment).strName = "" Then
objTypes(bytNextFulfillment).bytType = intNewValue
objTypes(bytNextFulfillment).strName = strFulfillment
bytNextFulfillment = bytNextFulfillment + 1

End If
End If

If bytNextFulfillment > 1 Then bytNextFulfillment = 1


If objTypes(0).strName = objTypes(1).strName Then
objTypes(bytNextFulfillment).strName = "" : bytNextFulfillment = 0 :
blAgain = True : GoTo tryAgain

COMPLETE:

For intCounter = 0 To 1
If objTypes(intCounter).strName = strFulfillment Then
Return objTypes(intCounter).bytType
Next intCounter

End Function

I just need an outside eye to go through the steps of these codes one
by one, please ignore the fact I use GoTo, it is the only time I use
it here and I am content with it. Thanks in advance. (It is not like
the program will seize to function in the future versions of OS
because I use GoTo).
 
M

Mythran

I have did further revision to the code because I noticed some
serious bugs on them and it is like this now:

Private Function getFulfillment(ByVal strFulfillment As String) As
Integer
Dim intCounter As Integer = Nothing
Dim blAgain As Boolean = False

tryAgain:
If strFulfillment = "Lasalle" Then Exit Function

If strFulfillment = "" Then MessageBox.Show("Fulfillment is
null.", "", MessageBoxButtons.OK, MessageBoxIcon.Error) : Exit
Function

If blAgain = True Then
blAgain = False
If objTypes(0).strName = objTypes(1).strName Then Return 1
If objTypes(0).strName = "" And objTypes(1).strName <> ""
Then Return 2
If objTypes(0).strName <> "" And objTypes(1).strName = ""
Then Return 1

End If

Why are you setting blnAgain = False here? Right before the GoTo, you are
setting it back to True and between here and there, you aren't using the var
again...so kinda pointless. I didn't step through the logic, that just
jumped out at me :p


Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top