What is Visual Basic's equivalant to Java's "!"? - vb.net

I would like to know Visual Basic's equivalant to Java's "!"
Example of how it would work in Java:
If !code.DoesExist {
log.info("This code doesn't exist!");
}
Else {
log.info("This code does exist");
}
I hope you understand what I mean.
In my code I need to do the following:
If imgUrl.Contains("imgur") Then
ImagesFound += 1
Select Case ImagesFound
Case 1
imgBox.ImageLocation = imgUrl
End Select
ElseIf !imgUrl.Contains("") Then
'<some code here>
End If
I need it at the 7th line.
Note: I can't just use "Else" I need to specifically point out that if an image with imgur in the HTML source wasn't found, then this and that should happen.

As per suggestion, here is more elaborated answer :
Not Keyword : Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression.
For more info : https://msdn.microsoft.com/en-us/library/2cwcswt4.aspx
<> : Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true.
For more info : http://www.tutorialspoint.com/vb.net/vb.net_operators.htm

Related

How to shorten code when I do a variable assignment in a condition in Dart?

Here's the ugly long code:
var i;
if(true)
i = 1;
else
i = 0;
When I try this:
var i = (true ? 0 : 1);
it doesn't work resulting in an error on the following line. I guess I was a bit inattentive reading Dart's syntax specs, so can anybody show me the right way?
This looks perfectly fine from a syntax point of view. You can omit the parentheses.
I get a warning 'Dead code' at '1' with your example because of 'true'.
The Darteditor shows you a hint that you wrote code that may contain a bug because he knows your expression can never evaluate to 1 because of the hardcoded 'true'.
void main(List<String> args) {
var b = true;
var i = b ? 0 : 1;
}
doesn't produce a warning.

Need regular expression that will work to find numeric and alpha characters in a string

Here's what I'm trying to do. A user can type in a search string, which can include '*' or '?' wildcard characters. I'm finding this works with regular strings but not with ones including numeric characters.
e.g:
414D512052524D2E535441524B2E4E45298B8751202AE908
1208
if I look for a section of that hex string, it returns false. If I look for "120" or "208" in the "1208" string it fails.
Right now, my regular expression pattern ends up looking like this when a user enters, say "w?f": '\bw.?f\b'
I'm (obviously) not well-versed in regular expressions at the moment, but would appreciate any pointers someone may have to handle numeric characters in the way I need to - thanks!
Code in question:
/**
*
* #param searchString
* #param strToBeSearched
* #return
*/
public boolean findString(String searchString, String strToBeSearched) {
Pattern pattern = Pattern.compile(wildcardToRegex(searchString));
return pattern.matcher(strToBeSearched).find();
}
private String wildcardToRegex(String wildcard){
StringBuffer s = new StringBuffer(wildcard.length());
s.append("\\b");
for (int i = 0, is = wildcard.length(); i < is; i++) {
char c = wildcard.charAt(i);
switch(c) {
case '*':
s.append(".*");
break;
case '?':
s.append(".?");
break;
default:
s.append(c);
break;
}
}
s.append("\\b");
return(s.toString());
}
Let's assume your string to search in is
1208
The search "term" the user enters is
120
The pattern then is
\b120\b
The \b (word boundary) meta-character matches beginning and end of "words".
In our example, this can't work because 120 != 1208
The pattern has to be
\b.*120.*\b
where .* means match a variable number of characters (including null).
Solution:
either add the .*s to your wildcardToRegex(...) method to make this functionality work out-of-the-box,
or tell your users to search for *120*, because your * wildcard character does exactly the same.
This is, in fact, my preference because the user can then define whether to search for entries starting with something (search for something*), including something (*something*), ending with something (*something), or exactly something (something).

What is the equivalent of else do nothing using the conditional operator?

I want to know what is the equivalent of this if statement:
if (condition) {
// do something
}else{
// do nothing
}
Using the conditional operator:
(condition) ? // [do nothing] : {do nothing} "
It's not possible to "do nothing" using the conditional operator. You always have to have valid expressions on both sides, although both expressions can be casted to void.
This is one of the dis-advantage of ternary operator ( ?: ).
It needs expressions in all the three places. You cant skip any of them.
You can do some tweak on it, however its True-part and / or False-part can be assigned to the same as :
int big=100;
big= (10 > 100) ? 0 : big;
if (!condition) {
// do something
}else{
// do nothing
}
Take a look at the !before contition now :-)
The ! before the condition just switches the "if", to "if not"...
Is that what you have been searching for?
originalValue = (condition) ? newValue : originalValue
The compiler should then remove the unnecessary assign of originalValue to itself.
GCC had an extension to do give you something more to what you where looking for, something like
originalValue = condition ?: newValue;
So it may be available in clang also. You would have to ! the condition though.
you could do something like this
someBool ? [self someFunction] : (^{})(); //empty block
In normal code the answer is there is no equivalent - all sub expressions of an expression must have a value, but...
the following is not a recommendation
Something along the lines of the following should work in the general case:
condition ? ( (^{ do something })(), 0 ) : 0;
That is for the general case. If do something is a single, non-compound, statement; such as a method call; then the block can be dropped to give:
condition ? (do something, 0) : 0;
Again this is NOT recommended in real code!

Char.IsSymbol("*") is false

I'm working on a password validation routine, and am surprised to find that VB does not consider '*' to be a symbol per the Char.IsSymbol() check.
Here is the output from the QuickWatch:
char.IsSymbol("*") False Boolean
The MS documentation does not specify what characters are matched by IsSymbol, but does imply that standard mathematical symbols are included here.
Does anyone have any good ideas for matching all standard US special characters?
Characters that are symbols in this context: UnicodeCategory.MathSymbol, UnicodeCategory.CurrencySymbol, UnicodeCategory.ModifierSymbol and UnicodeCategory.OtherSymbol from the System.Globalization namespace. These are the Unicode characters designated Sm, Sc, Sk and So, respectively. All other characters return False.
From the .Net source:
internal static bool CheckSymbol(UnicodeCategory uc)
{
switch (uc)
{
case UnicodeCategory.MathSymbol:
case UnicodeCategory.CurrencySymbol:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.OtherSymbol:
return true;
default:
return false;
}
}
or converted to VB.Net:
Friend Shared Function CheckSymbol(uc As UnicodeCategory) As Boolean
Select Case uc
Case UnicodeCategory.MathSymbol, UnicodeCategory.CurrencySymbol, UnicodeCategory.ModifierSymbol, UnicodeCategory.OtherSymbol
Return True
Case Else
Return False
End Select
End Function
CheckSymbol is called by IsSymbol with the Unicode category of the given char.
Since the * is in the category OtherPunctuation (you can check this with char.GetUnicodeCategory()), it is not considered a symbol, and the method correctly returns False.
To answer your question: use char.GetUnicodeCategory() to check which category the character falls in, and decide to include it or not in your own logic.
If you simply need to know that character is something else than digit or letter,
use just
!char.IsLetterOrDigit(c)
preferably with
&& !char.IsControl(c)
Maybe you have the compiler option "strict" of, because with
Char.IsSymbol("*")
I get a compiler error
BC30512: Option Strict On disallows implicit conversions from 'String' to 'Char'.
To define a Character literal in VB.NET, you must add a c to the string, like this:
Char.IsSymbol("*"c)
IsPunctuation(x) is what you are looking for.
This worked for me in C#:
string Password = "";
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Ignore any key out of range.
if (char.IsPunctuation(key.KeyChar) ||char.IsLetterOrDigit(key.KeyChar) || char.IsSymbol(key.KeyChar))
{
// Append the character to the password.
Password += key.KeyChar;
Console.Write("*");
}
// Exit if Enter key is pressed.
} while (key.Key != ConsoleKey.Enter);

Boolean ? : operation syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
I have seen code where it uses a syntax something like...
someValue = someBoolean ? valueOne : valueTwo;
Or something like this.
I've never used this and I'm not sure what it's called.
Please can someone explain how to use it or provide a link to a resource about it.
It's ternary opertaor.
It evaluates the someBoolean condition.
If it is true then pass the valueOne to someValue
If it is false then pass valueTwo to someValue
It is equal to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
This is a good link which explains about ternary operator
This is called ternary operator ( ?: )
1 ? 2 : 3
1 is the condition.
2 is executed when 1 it is true.
3 is executed when 1 is false.
Similar to: (Below is not a running code, 1,2,3 shows only placeholders for some expressions and statements.
if(1){ //condition
2 //true
}
else{
3 //false
}
You can shorten it as for :
int bigger;
(10<100) ? bigger=100 : bigger=10;
in short way:
int bigger = (10<100) ? 100 : 10 ;
NOTE:
Its precedence order is among the least and it is much slower then if-else and switch case statements.
It is a ternary operator (also known as the conditional operator). You can find explanation at this link.
Basically your expression is saying that if someBoolean is true someValue will get valueOne if not it will get valueTwo.
It is similar to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
which offers less visibility in your code. I recommend using this operator in case you want to assign a value which depends on one condition.
Note that it is an expression not specific to Objective-C, you can use it in C and C++ too.
The result of the assignment is valueOne is the condition is true, and valueTwo if the condition is false.
See it here on wikipedia. It also makes the case with other languages, just skip them and see the C syntax example.
Suppose user needs to answer some question and you change background color of your view to red if he was wrong, green if he was correct.
- (void)handleAnswer:(BOOL)correct {
UIColor *color = (correct) ? [UIColor greenColor] : [UIColor redColor];
self.view.backgroundColor = color;
}
It works same as the following
if (someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}