Ok i just want to know if value x is not in my array
Heres what i have been trying
Im using VB.net
and just need to know when x isnt in the array so i can take an action. thankx
Dim L, Path(0) As Integer
Open = cleara(Open)
sealed = cleara(sealed)
Open(0) = Agent
sealed(0) = Agent
Finds adjacent nodes
L = Agent
Do Until sealed(sealed.GetLength(0) - 1) = Targ Or Open.GetLength(0) = 0
'Agents(0) = L
H = Find_H(L, Targ, Open)
'T = Find_T(L, Targ, Open)
ReDim F(T.GetLength(0) - 1)
For lp As Integer = 0 To F.GetLength(0) - 1
F(lp) = H(lp) '+ H(lp)
Next
L = Find_lowest(F, Open)
Open = Remove_from(Open, L)
sealed = Add_to(sealed, L)
Ad = Find_adjacent(L, Targ)
For lp As Integer = 0 To Ad.GetLength(0) - 1
Ok here is where my problems is
What i need to do is
find out if ad is in seal if yes ignore it
If ad isnt in sealed the if it is in open compare T values
if ad isn in sealed or open then add it to open and set L as parent of ad
The below was a way to test and see if the values were loading into the arrays right
If Walk(Ad(lp)) <> -1 Then
Parents(Ad(lp)) = L
Open = Add_to(Open, Ad(lp))
For lp2 As Integer = 0 To sealed.GetLength(0) - 1
For lp3 As Integer = 0 To Open.GetLength(0) - 1
If lp3 < Open.GetLength(0) - 1 Then
If Open(lp3) = sealed(lp2) Then
Open = Remove_from(Open, sealed(lp2))
End If
End If
Next
Next
End If
Next
G.Graphics.DrawRectangle(Pens.White, Grid(Targ))
TempDrawing()
Loop
This is suppost to be an a* program but i have been having trouble main with my heuistics if you can tell me what im doing wrong it would also be a great help
Here is how this work so far
Add my location to open
Create H,G,F for items in open list
Find lowest F
Find adjacent nodes
Loop through nodes
If node is not walkable then ignore
If in sealed ignore (this is where im stuck at)
If not in sealed and is walkable then if in open compare G scores else add to open
try this
If Array.IndexOf(yourArray, x) == -1 Then
'Do something
End If
This need to be for -
For lp as integer = 0 to array.getlength(0) - 1
^ error. Needs to array.GetLength()
Also, if a number is found, you can print it and break from the loop. There is no need to iterate further in the loop.
You need to be careful, the variable that is used as a flag( i.e., g) is different from the variable actually setting a value to it. ( i.e., G )
You forgot to assign initial value to g outside of the for loop.
Try this.
Dim g as int
g = 0
For lp as integer = 0 to array.Length() - 1
If X = array(LP) Then
g = 1
Exit For
End If
next
If g = 0 Then
X is not in array
End If
Use the Enumerable.Except method to get all points in one enumerable collection that are also present in a second collection.
Here is an example taken from the MSDN documentation of the method:
' Create two arrays of doubles.
Dim numbers1() As Double = {2.0, 2.1, 2.2, 2.3, 2.4, 2.5}
Dim numbers2() As Double = {2.2}
' Select the elements from the first array that are not
' in the second array.
Dim onlyInFirstSet As IEnumerable(Of Double) = numbers1.Except(numbers2)
Dim output As New System.Text.StringBuilder
For Each number As Double In onlyInFirstSet
output.AppendLine(number)
Next
' Display the output.
MsgBox(output.ToString())
' This code produces the following output:
'
' 2
' 2.1
' 2.3
' 2.4
' 2.5
Related
Im trying to fill multiple matrices according to inputs from a userform.
Right now i have to create 5 matrices but i want this to be able to change in the future, so i want to create the variables "on the fly" in my loop. I basically want to Create Matrix1, Matrix2, Matrix3 ... Matrixn, but i can't figure how to do it. Any Ideas ?
Following you can see the script --> The two last lines are totally wrong (i know it) but i just added them in hope that you understand what i want to do :)
PS: And most important i should be able to then look to Matrix1(i,j),..., Matrixn(i,j) values easily
Looking Forward your help <3
'FicheCalcul ----------------------------------------------------------------------------------------------------
Dim Temp4 As String
Dim Temp5 As String
Dim MatrixTemp(3 - 1, 5 - 1) As Double 'm-1,x-1
For i = 1 To 5 'n
For j = 0 To 3 - 1 'm-1
For k = 0 To 5 - 1 'X-1
Temp4 = j & "C" & i
Temp5 = Temp4 & k
If UserForm1.Controls(Temp5) = False Then
MatrixTemp(j, k) = ""
Else
If UserForm1.Controls(Temp5) = True Then
MatrixTemp(j, k) = k - 2 ' -2 -1 0 1 2
End If
End If
Next k
Next j
Dim ("Matrix"&i)(3-1,5-1) As Double
("Matrix"&i) = MatrixTemp
Next i
You cannot have variable variable names. Therefore you need to use arrays.
Option Explicit
Sub example()
Dim MatrixTemp(4, 5) As Double
'do your matrix stuff here
MatrixTemp(0, 0) = 1
MatrixTemp(0, 1) = 2
MatrixTemp(0, 2) = 3
Dim Matrix(4) As Variant 'create an array of 5 matices
Matrix(0) = MatrixTemp 'fill your temp matrix into the first matrix
Debug.Print Matrix(0)(0, 2) 'outputs 3
End Sub
I'm quite a beginner in programming, and I want to make it shorter: code:
For y as integer = 1 to 5
'Code'
Next
Using sw As New StreamWriter(filepath)
sw.WriteLine("[TxtStringNumP1]")
sw.WriteLine(TxtStringNumP1.Text)
sw.WriteLine("[TxtStringNumP2]")
sw.WriteLine(TxtStringNumP2.Text)
sw.WriteLine("[TxtStringNumP3]")
sw.WriteLine(TxtStringNumP3.Text)
sw.WriteLine("[TxtStringNumP4]")
sw.WriteLine(TxtStringNumP4.Text)
sw.WriteLine("[TxtStringNumP5]")
sw.WriteLine(TxtStringNumP5.Text)
End Using
MsgBox("Ok", vbInformation)
You can make this bit shorter:
For y as integer = 1 to 5
For y = 1 to 5
And you can then rely on the fact that your control have names that differ only by a number, and there is some collection of them that knows them by name:
For y = 1 To 5
Dim n = "TxtStringNumP" & y 'formulate the name
Dim c = Me.Controls.Find(n, True) 'find all controls with a matcing name, returns a collection of controls
sw.WriteLine($"[{n}]") 'write the name we formulated
sw.WriteLine(c(0).Text) 'write the text of the first control. All controls have a Text property, we don't need to convert it to textbox
Next
I have a problem with my university project
It's a little game, 6 buttons for each players and 2 players so 12 buttons
There is number in each buttons, if a player has his 6 buttons at 0, he can't play
I have try some Public Function and i'm actually working with a very simple one but i think this is not the problem
My function is here
And in my form, the problem is here, i've tried many things but i don't know how do to that ... I read my lesson and I'm searching on the internet, i have no idea ..
If possible is True you don't re-enable the button.
You can simplify things.
Public Function PeutJouer(ByVal joueur As Integer) As Boolean
Dim sum As Integer
Dim start As Integer = (joueur - 1) * 7
For i As Integer = start To start + 5
sum += tableau(i)
Next
Return sum <> 0
End Function
Then
Btn1P1.Enabled = PeutJouer(1)
Did you show all the relevant code? You are declaring Dim tableau(12) As Integer but the array is never filled with values. Probably tableau should be declared at the form level and not locally in this function. If you already have both, remove the local declaration, because it hides the one at form level. You also need to return the result from the function. I don't see this in your function.
Note that this
If x <> 0 Then
booleanVariable = True
Else
booleanVariable = True
End If
can be simplified to
booleanVariable = x <> 0
i.e., the condition is an expression yielding the Boolean result True or False already and you can use this value directly. When working with numeric values you don't write If x + y = 1 Then r = 1 Else If x + y = 2 Then r = 2 .... You simply write r = x + y.
I have a procedure which involves ranking values. My code (stripped down to important parts) looks like this:
Dim myArray() as variant
ReDim myArray(1 to 4, 1 to x)
for i = 1 to x
myArray(1,i) = a
myArray(2,i) = b
myArray(3,i) = c
next i
for j = 1 to x
myArray(4,j) = Application.Rank_Eq(myArray(3,j), Application.Index(myArray,3,0), 1)
next j
for k = 1 to x
myFunction(myArray(4,k))
next k
Debugging it, the for j = 1 to x loop works fine if I just return, say, the value of j or the value of myArray(3,j) but it breaks out of the loop at j=1 when I use the Application.Rank_Eq() formula.
Have I done something really stupid that I just can't see, or is this an Excel issue?
EDIT:
I tried using the following to debug:
myIndex = Application.Index(myArray,3,0)
for k = 1 to x
MsgBox myIndex(k,1)
a = Application.Rank_Eq(myIndex(1,k), editedRows, 1)
next k
This appears to run ok - i.e. each value of myIndex(k,1) is returned. However, if I add MsgBox a before next k, then it breaks. This suggests it's something to do with the value being returned by Rank_Eq, no?
Not sure it's part of the issue - but I had to access the Rank_Eq method through the WorksheetFunction object, not the Application object.
Secondly, you'll notice that this function needs a Double and a Range for the first 2 arguments. Currently you are supplying a Variant and whatever the value is from your Index() method.
Try casting the Variant to a Double like so for the first argument:
CDbl(myArray(3, j))
For the second argument, I have no idea from your question how the array gets populated so I can't guess where the Range argument needs to refer to...
I'm trying to teach myself some basic VBA in Excel 2010 and I've come across a problem I can't google myself out of. The objective is to create a button which when pressed, automatically does linest for me and writes the values into an array. So far, this is my code.
Private Sub CommandButton1_Click()
Dim linest As Variant
Dim linestArray(4,1) as Variant
Dim i As Integer, j as Integer
linest = Application.LinEst(Range("U49:U51"), Range("T49:T51"), True, True)
For i = 0 To 4
linestArray(i,0) = accessing values of linest variable fyrst column
Cells(68 + i, 21) = linestArray(i,0)
Next
For j = 0 To 4
linestArray(j,1) = accessing values of linest variable second column
Cells(68 + j, 22) = linestArray(j,0)
Next
End Sub
How do I access the values of variable linest so I can store them to an array and print them? Thank you.
EDIT: I figured it out. Variable linest is already an array! I feel pretty dumb. Sorry, this can be ignored.
New code:
Dim linestArray As Variant
linestArray = Application.LinEst(Range("U49:U51"), Range("T49:T51"), True, True)
For i = 0 To 4
For j = 0 To 1
Cells(68 + i, 21 + j) = linestArray(i + 1, j + 1)
Next
Next
The output of any such formula is a Variant array. So you've got that part right.
For a general approach to these Application. (use WorksheetFunction. instead, it's much faster) type functions is...
Type the function in Excel (as an array formula, Ctrl-Shift-Return, if need be)
The output is an N x M matrix of some sort (N =1 , M =1) for most cases
When you do Var = Application.Linest(xyx), the answer gets stored in Var
Get the size of the Var using Ubound(Var, 1), Ubound(Var, 2) to get number of rows and columns (note that these are base 0 type arrays)
Usually, the size will be one x one. In that case, your variable is stored in Var(0,0) i.e. a zero base multidimensional variant array, the top left element.
Hope this helps.