Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I have a requirement where I should increment the value of J for each line of execution.
Here is my code, But I want something better than this
VB
For i As Int16 = 0 To ds.Tables(0).Rows.Count - 1

              ' For J = 33 To i
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LIDOT").ToString())
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LIPRODCO").ToString())
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LITIRSIZ").ToString())
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LITIRDPT").ToString())
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), "replace price1")
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LIRR").ToString())
              J += 1



              If ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "BEAD" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")

              ElseIf ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "TREAD" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")

              ElseIf ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "SIDEWALK" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")

              ElseIf ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "OTHER" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "TREADLIFE" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LIREMARE").ToString().ToUpper() = "INTERIOR" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")

              End If
              J += 1

              If ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "LEFT FRONT" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")

              ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "LEFT REAR" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "RIGHT FRONT" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "RIGHT REAR" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "TRAILER" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "Yes")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "OTHER" Then
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
                  J += 1
                  pdfFormFields.SetField("untitled" + Convert.ToString(J), "No")
              End If
              J += 1
              pdfFormFields.SetField("untitled" + Convert.ToString(J), ds.Tables(0).Rows(i)("LIAPPNBR").ToString())
              J += 1
              'Next
              K = J
          Next




Instead of adding J+=1 at each line of code, I want something nice and less code. This may seem weird for you. But the requirement is like that, I have to do this way by incrementing J for every line of statement executed.
Could someone help me with this please?

P.S: I am not bothered about the down votes. I only want to learn better way of programming
Posted
Comments
Praveen Kumar Upadhyay 23-Jul-15 9:21am    
I am only concern with your conditional statements. I don't know on what basis you are changing SetField() 3rd parameter to "Yes" or "No".

ElseIf ds.Tables(0).Rows(i)("LITIRPOS").ToString().ToUpper() = "TRAILER" Then
in this block, why in 2nd lasts statement SetField() 3rd parameter is "Yes" while other statements of this condition is "No"
sudevsu 23-Jul-15 9:51am    
Because there are checkboxes in the PDF for each field. So I am checking only if the value matches to the field
Praveen Kumar Upadhyay 23-Jul-15 10:28am    
That's okay, but I did not understand why 2nd last or 3rd last setField() 3rd parameter is changing to "Yes" or "No", while other statement of the condition are same.
Here I am not able to understand the change of "Yes" or "no" in a condition. Please read my above comment once again.
sudevsu 23-Jul-15 16:01pm    
it is not confusing Praveen. I have set of check boxes like Tread, Sidewalk, Other, Bead etc., Now if database value is Sidewalk it going to check Sidewalk and rest all should be unchecked. Simple. Similarly another set of checkboxes in the PDF are Left Front, Trailer, Left Rear etc.,

You are looking for the VB.net equivalent of C#'s ++ operator.

Unfortunately there (currently) isn't one.

So write yourself a helper function such as
VB
Function Increment(ByRef value As Integer) As Integer
    Dim Tmp As Integer = value
    value += 1
    Return Tmp
End Function
which I found at this post[^] - response from Maciej Pakulski

And use it like
VB
pdfFormFields.SetField("untitled" + Convert.ToString(Increment(J)), ds.Tables(0).Rows(i)("LIDOT").ToString())

Note that because Tmp is being returned from the function it is the original value of J that is passed to the Convert but J is still incremented.

As an aside, I'm not a fan of Convert - I prefer the built-in ToString() method on the object. I.e.
VB
pdfFormFields.SetField("untitled" + Increment(J).ToString(), ds.Tables(0).Rows(i)("LIDOT").ToString())


[EDIT]
I had another look at this and you would probably be better off with a function more like
VB
Function DeriveFieldName(ByRef value As Integer) As String
    Dim Tmp As Integer = value
    value += 1
    Return "untitled" + Tmp.ToString()
End Function
Then you can have much tidier code such as
pdfFormFields.SetField(DeriveFieldName(J), "Yes")
 
Share this answer
 
v2
Comments
sudevsu 23-Jul-15 13:54pm    
This helped me to achieve what I need. I just changed a bit. But idea was from your solution. Thank a lot.
Put your data in a static dictionary, and simply loop through the dictionary.

XML
Dictionary<string, string> BEADdata = new Dictionary<string, string> {
  {"untitled1", "Yes" },
  {"untitled2", "No" },
  {"untitled3", "No" }
};

Dictionary<string, string> TREADdata = new Dictionary<string, string> {
  {"untitled1", "No" },
  {"untitled2", "Yes" },
  {"untitled3", "No" }
}


Even better, store this data in a config file, so it can be maintained independently of the application.
 
Share this answer
 
Comments
sudevsu 23-Jul-15 16:03pm    
I really don't know the concept of static dictionary so I am not trying the solution. Will surely keep in mind for future .
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900