How to insert an index in string.format? - vb.net

I'm trying to add an index to string.format string, for example:
For i = 1 To Dataset.Tables(0).Columns.Count - 1
query_builder.Append(String.Format("#parameter{i}", i))
Next
What I'm trying to achieve is get in a similar result:
#parameter1
#parameter2
#parameter3 etc....
But I get this error:
Input string format not correct
why?

query_builder.Append(String.Format("#parameter{i}", i))
Should be
query_builder.Append(String.Format("#parameter{0}", i))
or
query_builder.AppendFormat("#parameter{0}", i)

You must specify a numeric value between brackets:
For i = 1 To Dataset.Tables(0).Columns.Count - 1
query_builder.Append(String.Format("#parameter{0}", i))
Next
{0} corresponds to the item at index 0 (first item) in the list of parameters to String.Format, which is variable i in your case.

Related

Label a set of objects with (A->Z,AA->ZZ, AAA->ZZZ) in VBA

I have a set which has an unknown number of objects. I want to associate a label to each one of these objects. Instead of labeling each object with a number I want to label them with letters.
For example the first object would be labeled A the second B and so on.
When I get to Z, the next object would be labeled AA
AZ? then BA, BB, BC.
ZZ? then AAA, AAB, AAC and so on.
I'm working using Mapbasic (similar to VBA), but I can't seem to wrap my head around a dynamic solution. My solution assumes that there will be a max number of objects that the set may or may not exceed.
label = pos1 & pos2
Once pos2 reaches ASCII "Z" then pos1 will be "A" and pos2 will be "A". However, if there is another object after "ZZ" this will fail.
How do I overcome this static solution?
Basically what I needed was a Base 26 Counter. The function takes a parameter like "A" or "AAA" and determines the next letter in the sequence.
Function IncrementAlpha(ByVal alpha As String) As String
Dim N As Integer
Dim num As Integer
Dim str As String
Do While Len(alpha)
num = num * 26 + (Asc(alpha) - Asc("A") + 1)
alpha = Mid$(alpha, 2,1)
Loop
N = num + 1
Do While N > 0
str = Chr$(Asc("A") + (N - 1) Mod 26) & str
N = (N - 1) \ 26
Loop
IncrementAlpha = str
End Function
If we need to convert numbers to a "letter format" where:
1 = A
26 = Z
27 = AA
702 = ZZ
703 = AAA etc
...and it needs to be in Excel VBA, then we're in luck. Excel's columns are "numbered" the same way!
Function numToLetters(num As Integer) As String
numToLetters = Split(Cells(1, num).Address(, 0), "$")(0)
End Function
Pass this function a number between 1 and 16384 and it will return a string between A and XFD.
Edit:
I guess I misread; you're not using Excel. If you're using VBA you should still be able to do this will the help of an reference to an Excel Object Library.
This should get you going in terms of the logic. Haven't tested it completely, but you should be able to work from here.
Public Function GenerateLabel(ByVal Number As Long) As String
Const TOKENS As String = "ZABCDEFGHIJKLMNOPQRSTUVWXY"
Dim i As Long
Dim j As Long
Dim Prev As String
j = 1
Prev = ""
Do While Number > 0
i = (Number Mod 26) + 1
GenerateLabel = Prev & Mid(TOKENS, i, 1)
Number = Number - 26
If j > 0 Then Prev = Mid(TOKENS, j + 1, 1)
j = j + Abs(Number Mod 26 = 0)
Loop
End Function

vb.net array.sort() parameters why 1 short?

I'm sorting an array using code like this:
Array.Sort(arr, 0, intEndingPosition, New myIComparer)
I want the sorting to start with index 0 and end with index intEndingPosition. However, the last element arr(intEndingPosition) was left out and did not get sorted. Why?
intEndingPosition is calculated beforehand like this:
Dim StringOfConcern As String
Dim OneChar(65534), FrqOne(65534) As String
Dim CntNewOnes, CntRptOnes As Integer
Dim c As Char
Dim i, j As Integer
Dim isNew As Boolean
StringOfConcern = TextBox1.Text
OneChar(0) = CStr(StringOfConcern(0))
FrqOne(0) = 1
i = 0
j = 0
For Each c In StringOfConcern.Substring(1)
isNew = True
For j = 0 To i Step 1
If CStr(c) = OneChar(j) Then
isNew = False
FrqOne(j) += 1
Exit For
End If
Next j
If isNew = True Then
i += 1
OneChar(i) = CStr(c)
FrqOne(i) = 1
End If
Next c
CntNewOnes = i + 1
CntRptOnes = 0
For i = 0 To CntNewOnes - 1 Step 1
If FrqOne(i) > 1 Then CntRptOnes += 1
Next i
The sorting follows here. The code in my original question is only illustrative. The actual sorting is:
Array.Sort(FrqOne, OneChar, 0, CntNewOnes - 1)
Array.Reverse(FrqOne, 0, CntNewOnes - 1)
Array.Reverse(OneChar, 0, CntNewOnes - 1)
Note the method declaration for Array.Sort
Public Shared Sub Sort (
array As Array,
index As Integer,
length As Integer,
comparer As IComparer
)
The third parameter is the number of elements in the range to sort (length) not the end index as you suggest.
So let's assume for a minute that your intEndingPosition is 4. This means you're expecting to sort 5 elements i.e. elements at indices 0, 1, 2, 3, 4. However, the number 4 is the length and not the end index thus you're only sorting elements at indices 0, 1, 2, 3.
This explains why you're observing that the elements being sorted is one shorter than you expected.
Put it simply the third parameter should specify the length of elements to sort and not the end index.
Another Example:
Consider the Substring method of the String class:
Public Function Substring (
startIndex As Integer,
length As Integer
) As String
Then assume we have this piece of code:
Dim temp As String = "testing"
Dim result As String = temp.Substring(0, 4)
result is now a string containing 4 characters as 4 in the Substring call indicates the length that should be retrieved as opposed to the end index.
Had 4 been the end index then you'd expect result to contain 5 characters.

'Char' is not a member of 'String'" in VB.NET

Scenario is described below:
In txtDiscountRate.Text it has a value which is "0.996010500406591".
In my coding I did this:
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 6) 'means considering round till 6th digit
It is giving the value 0.99601 because of 6th digit after decimal is 0. But I want to put a condition in the decimal value.
So if on 6th digit after decimal is ( 0 or 1 or 2 or 3 or 4 ) and 7th digit after decimal is available, then it will round until 7th position.
Or else it will round until 6th position.
I got a solution (shown in the code below) from this site for this problem. I tried to implement but the below code throws an error:
CInt(Str(1).Char(5)) is showing error this error -> " 'Char' is not a member of 'String'".
This is my whole code so far:
Dim str() As String = Split(CStr(Dec), ".")
If CInt(Str(1).**Char(5)**) < 5 Then 'It's Char number 5 since it's a zero-based index. So the first number = Index 0.
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 7)
Else
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 6)
End If
Correct solution is needed.
I think that following function will help you and it is your solution.
Function GetDecimalValue(ByVal value As String) As Decimal
Dim valueFromDot As String = value.Substring(IIf(value.IndexOf(".") < 0, 0, value.IndexOf(".") + 1))
If (valueFromDot.Length > 5) Then
If (CInt(valueFromDot(5).ToString()) < 5 And CInt(valueFromDot(6).ToString()) > 0) Then
GetDecimalValue = Math.Round(CDec(value), 7)
Else
GetDecimalValue = Math.Round(CDec(value), 6)
End If
Else
GetDecimalValue = CDec(value)
End If
End Function

read comma separated integers in INI file

I have a INI file that looks like this !
[columnNumber]
Number1=2,4
this is my code
the ini file is read using function defined here
the columberNumber is taken as string "2,4" i want to split this and pass it into my select case object mytmp looping for all values in Number1 pass it to mytmp in select
columnNum = ReadIni(file, "columnNumber", "Number1")
mytmp = columnNum
x = Split(mytmp, ",")
For k = 0 To UBound(x)
'mytmp1 = Split(array_colnum, ",")
'mytmp2 = Search(array_col)
'mytmp1 = x(k)
'mytmp2 = x(k)
Next k
this is my select case
select case i...<does something>
select case mytmp
Could somebody guide me in doing this!
Updated : I want to put in select case i the values got from [columnNumber] my ReadIni function reads from [columnNumber section] the number1={2,4} i want to split this and store in a variable, the variable is read from select case variable
It's not completly clear what exactly you trying to do...
what is "I" variable? what it should contain?
mytmp = "2,4"
x = Split(mytmp, ",")
For k = LBound (x) To UBound (x)
'## Get value and store in variable "I".
i = x (k)
Select Case i
Case "2"
Response.Write "yep, it's 2"
Case "4"
Response.Write "yep, it's 4"
End Select
Next

Verify Gamefield VB.NET

So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. The problem is that when I'm verifying the columns and lines I need the program not to get out of the game field.
Here's how my code looks like now:
Public Sub avisinhos(ByVal line, ByVal column)
If mat(line, column) = 0 Then
mat(line, column) = -1
numbandeiras = numbandeiras + 1
End If
For auxlinha = -1 To 1
For auxcolumn = -1 To 1
Next
Next
End Sub
How do I create a IF function to verify that I don't get out of the game field?
Best regards, joao.
pseudo code
int linestart = -1;
int lineend = 1;
int colstart = -1;
int colend = 1;
Assuming a 10 x 10 grid (zero based)
if line < 2 linestart = 0
if line > 8 lineend = 0
if column < 2 colstart = 0
if column > 8 colend = 0
For auxlinha = linestart To lineend
For auxcolumn = colstart To colend
// check
Next
Next
Personally though I wouldn't bother with the loops, they add very little to nothing
HasMineAbove = (line > 1) and (gamefield[line -1,column] = MinePresentValue
would be my approach, do it all in one.
Not to mention the huge potential confusion when auxlinha and auxcolumn are both zero...
I'm not sure exactly what your code is saying. It's a bit cryptic since you're using abbreviations and all lowercase names. You might want to try camelCasing and spelling out the words more completely, intellisense is your friend. =)
But coding style aside, if you are trying to loop through a limited range of values, you can keep your values bounded by using the modulus operator (%). For example, if you need to keep you values between 0-7 and you end up with a value of 12, just take the modulus of 8 to loop back to within range with a value of 4:
12 % 8 = 4
9 % 8 = 1
15 % 8 = 7
24 % 8 = 0
I realize this doesn't answer your specific question, but it's a handy technique might find useful.