If Else Statement in vb.Net - vb.net

I wanted to have this condition like i have b2.text, c3.text, d4.text, e5.text, f6.text, g7.text, h8.text, i9.text and j10.text.
And if they are all equal to zero then statement else continue.
i tried
If (b2.Text = 0 & c3.Text = 0 & d4.Text = 0 & e5.Text = 0 & f6.Text = 0 & g7.Text = 0 & h8.Text = 0 & i9.Text = 0 & j10.Text = 0) Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End if
Unfortunately i remembered & function only accept two variable only :P
How can i do it?
Thank you

You don't want & for VB, you want AndAlso. And while it is probably a bit above your level, you might want to look into Linq.
If ({b2.Text, c3.Text, d4.Text, e5.Text, f6.Text, g7.Text, h8.Text, i9.Text, j10.Text}).All(Function(f) f = "0") Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End IF
As others have said, you should turn on Option Strict, so that your comparison to an int gets flagged. I made the string "0" above, but you could easily modify it to use length if that is what you actually need.

I don't know what language are you using but I'm pretty sure you can compare more than 2 values The solution may be like this.
if (b2.text == 0 && c3.text == 0 && d4.text == 0 && e5.text== 0 && f6.text == 0 && g7.text == 0 && h8.text == 0 && i9.text == 0 && j10.text == 0) {
// when conditions are true
}
else {
// else code here
}

Related

Dynamic variable assignment based on vector element values in Octave

I need to dynamically assign variable names to each row in a matrix based on the element values of the row so that I can later reference the appropriate row based on the variable rather than an index value.
I am having trouble identifying how to store the variable name along with its associated vector so that the vector can be later pulled up by simply typing in the variable name.
So far, using a simplified example I have:
A = [1 1 0; 1 0 1; 0 1 1];
s = cell(rows(A),1)
i = 1;
for i = 1:rows(A)
if (A(i,1) == 1 & A(i,2) == 1 & A(i,3) == 0) s(i,1) = struct("x1_y1_z0",{A(i,:)})
elseif (A(i,1) == 1 & A(i,2) == 0 & A(i,3) == 1) s(i,1) = struct("x1_y0_z1",{A(i,:)})
elseif (A(i,1) == 0 & A(i,2) == 1 & A(i,3) == 1) s(i,1) = struct("x0_y1_z1",{A(i,:)});
endif
i++
endfor
However, the resulting structue s lists x1_y1_z0 = 1 1 0 in a cell [1,1] and s.x1_y1_z0 does not return [1 1 0] as I would like.
Thanks very much for your guidance towards a working solution.

AutoIt - Split 1 variable into 4

I have one 4 digit variable i wish to split into 4 seperate variables fromt the range 0000 to 9999
Local $Data ="Element 1|Element 2|Element 3|Element 4"
Local $arr = StringSplit($Data, "|")
If IsArray($arr) Then
$Imax = Ubound($arr)
For $i = 0 to $Imax -1
Next
EndIf
This is what I got so far
I want it to do this:
Lets say the bigvar = 2345
$BigVar=2345 Then
$SmallVar1 = 2
$SmallVar2 = 3
$SmallVar3 = 4
$SmallVar4 = 5
Also the bigvar changes all the time so i need it to keep reading of that
LOL to all the overkill answers
#include <Array.au3>
Local $parts = StringSplit("1574", "")
_ArrayDisplay($parts)
String Approach
If you simply want to split it, you can go with
#include <Array.au3>
Func _Split($BigVar)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
Return $SmallVar
EndFunc
$Array = _Split("2345")
_ArrayDisplay($Array)
Now you can use
$Array[0] = 4 ;Amount of digits
$Array[1] = 2
$Array[2] = 3
$Array[3] = 4
$Array[4] = 5
If the Number might be 123 and you want to interpret it as 0123 therefore $SmallVar[1] being 0 not 1, this method might fit your needs:
#include <Array.au3>
Func _Split($BigVar, $Digits = 0)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
If $Digits = 0 Then Return $SmallVar
If $SmallVar[0] >= $Digits Then
For $i = 1 To $SmallVar[0] - $Digits
_ArrayDelete($SmallVar, $i)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndIf
For $i = 1 To $Digits - $SmallVar[0]
_ArrayInsert($SmallVar, 1, 0)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndFunc
$Array = _Split("123", 4) ;4 being the amount of digits
_ArrayDisplay($Array)
The code example above still works with this version, since digits is an optional parameter, and leaving it out, _Split will act as it did before.
Just use modulo 10 division to get the single integers.
#include <array.au3>
Global $BigVar=2345
Global $TmpVar=$BigVar
Global $aResult[StringLen(String($BigVar))]
For $i=UBound($aResult)-1 To 0 Step -1
$aResult[$i] = Int(Mod($TmpVar, 10))
$TmpVar /= 10
Next
_ArrayDisplay($aResult)
Now you got an array that has each number of the big integer stored in a separate field.

How to do this in Visual basic?

How to do the "a++" and "b++" in Visual basic?
What is the another codes for there in Vb?
The names there are just example.
int a = 0;
int b = 0;
{
if (ans1.Text == "James")
{
a++;
}
else
{
b++;
}
if (ans2.Text == "Ryan")
{
a++;
}
else
{
b++;
}
if (ans3.Text == "Mac")
{
a++;
}
else
{
b++;
}
t1.Text = a.ToString();
t2.Text = b.ToString();
}
Like this:
a += 1
b += 1
(...)
Like this
DIM a as integer = 0
DIM b as integer = 0
If ans1.Text = "James" Then
a += 1
Else
b += 1
End If
If ans2.Text = "Ryan" Then
a += 1
Else
b += 1
End If
If ans3.Text = "Mac" Then
a += 1
Else
b += 1
End If
t1.Text = a.ToString()
t2.Text = b.ToString()
Your question has already been answered but I think it would be useful to see how you could simplify your code:
Dim correctAnswers As Integer = 0
Dim totalQuestions As Integer = 3'you need to modify this is you add more questions
'increment the number of correct answers for each one we find
correctAnswers += If(ans1.text = "James", 1, 0)
correctAnswers += If(ans2.text = "Ryan", 1, 0)
correctAnswers += If(ans3.text = "Mac", 1, 0)
'show the number of correct and incorrect answers
t1.Text = correctAnswers.ToString()
t2.Text = (totalQuestions - correctAnswers).ToString() 'show the number of incorrect questions
Neither the postfix nor prefix ++ are defined in Visual Basic.
Your only realistic option is to use a = a + 1 (or, in later BASICs, a += 1) instead (note the lack of a ; for a statement terminator). But note that this will not evaluate to the previous value of a and the entire construct is not an expression in the C / C++ sense. You could build a function to mimic a++ but that would be too obfuscating.

checking if string is numeric is not working in vb

I am trying to check if a string is numeric but in vain. here is my code:
If Val(fnumField.Text.Trim) > 0 Or fnumField.Text.Trim = "0" Or Val(phnField.Text.Trim) > 0 Or phnField.Text.Trim = "0" Or Val(ophnField.Text.Trim) > 0 Or _
ophnField.Text.Trim = "0" Or Val(treeField.Text.Trim) > 0 Or treeField.Text.Trim = "0" Then
messageBox.Show("number")
Else
messageBox.Show("not number")
EndIf
The problem I have is that when I run the program and insert a character string like "abcd" in one of the textfields like "fnumField", it still executes the code in the "if" and not in the "else". What am I not doing right?
try this:
If (Val(fnumField.Text.Trim) > 0 Or fnumField.Text.Trim = "0") And (Val(phnField.Text.Trim) > 0 Or phnField.Text.Trim = "0") And (Val(ophnField.Text.Trim) > 0 Or
ophnField.Text.Trim = "0") And (Val(treeField.Text.Trim) > 0 Or treeField.Text.Trim = "0") Then
messageBox.Show("number")
Else
messageBox.Show("not number")
EndIf

Confused with conditional and logical operators - VB.net

I'm kind of new to VB.net, and since I just finished a C# course, the lack of parentheses creates a lot of confusion on how to write certain combinations of operators.
The C# equivalent of the line I am trying to reproduce in VB would be like this :
if ( (a == 0 && b != null) || (a == 1 && c != null) )
I'm have no idea how to write this in VB, I've tried many combinations of And, Or, AndAlso, OrElse, etc. but I can't achieve the desired result.
I can't find any clear example of C# v.s. VB.net comparison on operators, and the notes I have aren't helpful either.
Can someone help me figure this out?
The equals operator is == in C# and = in VB.
if ( (a == 0 && b != null) || (a == 1 && c != null) )
statement; // One single statement only
or
if ( (a == 0 && b != null) || (a == 1 && c != null) ) {
statement; // Any number of statements
}
This online conversion tool will convert it to VB for you:
If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing) Then
statement
End If
C# && translates to AndAlso in VB.
C# || translates to OrElse in VB.
With these operators the evaluation stops as soon as the result is determined. This is known as "short-circuit" evaluation. E.g. in a && b the result is known to be false if a is false, and b will not be evaluated. This is especially important when the evaluation has side effects, like performing database queries, raising events or modifying data. It is also useful in conditions like these person != null && person.Name == "Doe" where the second would throw an exception if the first term evaluates to false.
The equivalent of the VB And and Or Boolean operators that do not use short-circuit evaluation are & and | in C#. Here all the terms will always be evaluated.
If (a = 0 Or b = 0 And c = 0) Then
statement
End If
if (a = 0 | b = 0 & c = 0) {
statement;
}
The vb.net equivalent would be
If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing ) Then
Note in c#, it should be a == 0 and not a = 0
Checkout this post with a comprehensive comparison.
if ( (a = 0 && b != null) || (a = 1 && c != null) )
Is equivilent to:
if ( ( a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing) )