How to validate operators in a simple calculator vb program? - vb.net

So I've been trying to do a calculator program in Visual Basic (shouldn't be too bad, I think). So far I got everything down and tested the operators individually. What I'm trying to do is to have the equals sign be able to determine which operator to use and go with it according to the button pressed for operator. I thought maybe if/else or switch/case could work, but I'm either getting addition first (it's the first in the choices) or nothing at all
Maybe I'm validating the wrong variable perhaps. I thought maybe that as an example:
if btnEquals.Text = "(insert operator sign here)" Then
{insert operator statement here}
on an if/else or case would do it, but it's simply not. I have checked in SO for any similar issue in VB, but most are for another language. I know I have to do something to ensure that the buttons coincide with the logical statements
Basically: How do I make the "=" button in the calculator program do the right arithmetic operation when the "+", "-", "/", "*" buttons are pressed? I tried if/else and case/switch and it's not logically giving the right answers.
Edited to clarify for other users.
Thanks in advance!

Already found an easy way to do it and it worked, it was simpler than I thought and was pulling my hair for no reason. Just for future reference. I just added a type char variable and with each arithmetic button (+,-,*,/) assign the arithmetic symbols to the variable and then use if/else or case switch in the "=" button to validate that variable.
Anyone with higher rank, you can go ahead and close this one. Thank you!

Related

VIM equivalent of IntelliJ's expand/shrink selection?

How would one achieve the same result. I believe the keybinding for macOS Intellij is op+up/down and on windows it is alt+w/d.
Essentially the function highlights the current word, then, with successive presses, expands out to the full string/line/area in-between parenthesis/further out to the next set of parenthesis. Very useful for developing in LISP.
The closest I've gotten is this: https://vi.stackexchange.com/a/19028
Try this plug in: https://github.com/terryma/vim-expand-region
It expands selections based on Vim’s text objects.
Well this may seem comfortable but does not correspondent with the internal logic of vim itself.
See, in vim everything you enter is like a sentence. va{ for example: there is a verb v -> visually select and an object (or movement) { -> paragraph. In this case there is also a modifier a around. You can exchange stuff in this sentence and it will still work vaw, dil, cB and so on. The power of vim is greatly based on that concept.
Of course you can write a function that does vaw first, then S-v and lastly va{ but that will only work with visual selection. It will not work with c or d or anything. So I will recommend to get used to use different keys for different actions.
The visual selection is mostly not needed anyway. Change a paragraph? directly use ca} and so on.
I have found that VI/VA + WOBO (as many times as you need to expand) works similarly. Not as fast but its the same concept and you can even expand/shrink asymmetrically based on your WO's and BO's (Or OW's and OB's depending on how you look at it)

Dollar and exclamation mark (bang) symbols in VTL

I've recently encountered these two variables in some Velocity code:
$!variable1
!$variable2
I was surprised by the similarity of these so I became suspicious about the correctness of the code and become interested in finding the difference between two.
Is it possible that velocity allows any order of these two symbols or do they have different purpose? Do you know the answer?
#Jr. Here is the guide I followed when doing VM R&D: http://velocity.apache.org/engine/1.7/user-guide.html
Velocity uses the !$ and $! annotations for different things. If you use !$ it will basically be the same as a normal "!" operator, but the $! is used as a basic check to see if the variable is blank and if so it prints it out as an empty string. If your variable is empty or null and you don't use the $! annotation it will print the actual variable name as a string.
I googled and stackoverflowed a lot before I finally found the answer at people.apache.org.
According to that:
It is very easy to confuse the quiet reference notation with the
boolean not-Operator. Using the not-Operator, you use !${foo}, while
the quiet reference notation is $!{foo}. And yes, you will end up
sometimes with !$!{foo}...
Easy after all, shame it didn't struck me immediately. Hope this helps someone.

Whats wrong with this expression?

Comments: Switch(((IIf(([qty_req]-[qty_on_hand])<0,0,([qty_req]-[qty_on_hand])))=0) And ((([qty_on_hand]-[qty_req])/[qty_req])<=0.2),"Please check manually")
I have been struggling with this expression for too long. I keep getting the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables." I've tried breaking down the expression to see if there was a bracket I the wrong place but I can't figure this out.
Note: The word "Comments" is just the field name (I primarily use the Design View in MS Access).
Update - The goal behind this is to eventually add more conditions to this switch statement, but this first one isn't working so that's why it seems like it doesn't make sense to use a Switch. Also, in pseudo code, this is what the intention of this expression is:
Switch([TransferQTY]=0 And [Req is within 20% of Inventory], "Please check manually")
In regards to the first IIF statement:
IIf([Req-Inventory is negative, that means that we have enough on hand and don't need to send],0, [Req-Inventory])
I think it's simply a check like this:
IIf([qty_req]-[qty_on_hand]<0 And ([qty_on_hand]-[qty_req])/[qty_req]<=0.2,"Please check manually","") AS Comments
The first IIF is just strangely built and has some redundancy to it. The second might give you strange answers because you don't have parans around your numerator. As it's written it could be simplified to:
As for the first IIF, you stated
"IIf([Req-Inventory is negative, that means that we have enough on
hand and don't need to send],0, [Req-Inventory])"
in the context of the switch (psuedo-coded):
Switch([TransferQTY]=0 And [Req is within 20% of Inventory], "Please
check manually")
This is basically saying "If the quantity requested minus thequantity on hand is less than or equal to 0", so instead of an IIF to do the "Less than or equal to" bit, just use <=:
Switch(((qty_req - qty_on_hand) <= 0) AND (((qty_on_hand - qty_req)/qty_req) <= 0.2), "Please Check Manually")
This will work better because Access is balking about the complexity. This dramatically reduces the complexity and accomplishes the same thing.
Also, I've gone a little heavy handed with the parantheses here. You could remove the ones that delineate each of the conditions that the AND function is evaluating and it would be fine.
I've removed the bit here about not using switch that was in a previous version of this answer since OP stated that switch() will be used after this bit starts working.

The specified RegistryOptions value is invalid

What im trying to do is write a key to the registry but im stepping from one problem to another, first permissions problem, now this..
This is the line of code.
If PNGchk.Checked = True Then
My.Computer.Registry.Users.CreateSubKey(UserSID & "\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.png\UserChoice", True, Security.AccessControl.RegistryRights.FullControl).SetValue("Progid", "SIV.png", Microsoft.Win32.RegistryValueKind.String)
End If
You must have Option Strict Off for that code to even compile, so you might want to fix that to start with. Option Strict On would have flagged issues with that code right away. You should read the documentation or at least pay attention to Intellisense for that method because your second and third arguments make no sense. No overload that I can see has a Boolean parameter and if you want to use a RegistryRights value you do so within a RegistrySecurity object as far as I can see.
RegistryKeyPermissionCheck.ReadWriteSubTree worked for me.
Using clsid64 = view64.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.png\UserChoice", RegistryKeyPermissionCheck.ReadWriteSubTree)
clsid64.SetValue("StubPath", "SIV.png")
clsid64.Close()
End Using

How can I validate text box input?

I am creating a program and I need to validate my text boxes. For the program the user needs to put in a phrase. But I am not sure how to make sure that the user actually entered in a phrase, the phrase isn't (ex.) skldkfdl, or that there isn't a space.
Strings in Java
You could do a String.Trim() to get rid of trailing whitespaces first...
then do a String.IndexOf(" ") to check for a space.
If the function returns -1, it means there is no space in the string.
Running on the assumption that you're using VB.Net - Add an event handler for the event where you want to validate the text, such as when a "Submit" button is clicked. You may want to use a CancelEventHandler, so that you can cancel the click.
In the event handler, if you're looking for just simple validation, you can use if-statements to check some simple conditions, such as if you just want to check "if input.equals(password)".
Look here for an example of using CancelEventHandler
If you're looking for some more complex validation, you'll want to use regular expressions.
This page might help get you started
Checking to see if something is "a phrase", as in, proper English, would be very difficult. You would need to make sure that all of the words are in the dictionary, and then you would need to check for proper grammar, which is incredibly complex, given English grammar rules. You may want to simplify your approach, depending on your problem. For example, maybe just check that no weird characters are used, that there is more than one space, and that each word contains a vowel.