-
Apr 3rd, 2024, 07:15 AM
#1
Thread Starter
New Member
If statement return 'invalid unit'
Hi all,
Quick question with hopefully an easy answer, my knowledge just doesn't cut it.
Trying to VB.net script the following in my CAM-system, I want the script to return a value based on a machinename it sees.
I got this far that it is accepted as a correct script, but it returns an error 'Unit isn't correct'
If it sees it's machine Okuma then I need it to return a value of 328.29, if it sees Matsuura, then it should return 321.04.
Code:
If ("MachineName" = "Okuma MA-600 HIII" ) Then
Return 328.29
End If
If ("MachineName" = "Matsuura MAM700HG" ) Then
Return 321.04
End If
I currently have the following script that works, but this doesn't check for machinename, it basically just returns a certain value based on a parameter I input.
Code:
If (ParameterValue("'Positie bankvijs 1'") = 1) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 2) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 3) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 4) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 5) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 6) Then
Return 461.54
End If
If (ParameterValue("'Positie bankvijs 1'") = 7) Then
Return 416.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 8) Then
Return 338.54
End If
If (ParameterValue("'Positie bankvijs 1'") = 9) Then
Return 261.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 10) Then
Return 183.54
End If
Attached a screenshot to hopefully clarify a little bit.
This gives the Y-axis position of my vice through the parameter 'Positie bankvijs 1', depending on the number I input there it will shift the vice, but now I want it to account for the machine too, because they have different values.
Once I figure out how to make it respond to the machinename, I will have to adapt it to respond to borh MachineName and the input from the parameter.

Thanks in advance!
Last edited by dday9; Apr 18th, 2024 at 09:41 AM.
-
Apr 3rd, 2024, 07:26 AM
#2
Re: If statement return 'invalid unit'
Would it also require the ParameterValue("MachineName") else you're simply comparing two literal strings.
edit: missed an e in Parameter
-
Apr 3rd, 2024, 07:49 AM
#3
Thread Starter
New Member
Re: If statement return 'invalid unit'
The MachineName isn't retrieved from a parameter I create, hence why that doesn't include the ParameterValue, this is an internal existing thing, see screenshot added here, I can retrieve the MachineName bij rightclicking and selecting it.
Sorry, my knowledge of VB is really non-existant, just trying to tackle some long-running problems in my CAM-program.
Attachment 191021
-
Apr 3rd, 2024, 08:54 AM
#4
Re: If statement return 'invalid unit'
It doesn't quite matter that it isn't a parameter. What you wrote is comparing two literal strings, and since they aren't the same, the If statement will always return False. That doesn't seem likely to produce the error you are seeing. It won't do what you want, but it should work just fine, if incorrectly. So, it doesn't seem like an answer to your question, but a problem you need to solve. Along the way to a solution, you might have your problem go away.
So, machine name is some internal existing thing. You can't get to it by just writing "machine name", because something in quotes is just a literal string. There must be some other way to get that name into a variable, and I have no idea what that would be. You need to be able to get the value for the Machine Name into a variable. It will be a string, but how you get that, may prove to be a more difficult problem than the error you are getting.
My usual boring signature: Nothing
 
-
Apr 18th, 2024, 06:35 AM
#5
Thread Starter
New Member
Re: If statement return 'invalid unit'
Ok, so after some screwing around, I am this far that I can retrieve my machine-name as a parameter in my CAM-system.
I had to Publish the machine-name on my machinemodel, so it can be retrieved as parameter)
The following works:
Code:
If (Machinenaam = "ABC") Then
Return 321.04
ElseIf (Machinenaam = "DEF") Then
Return 328.29
End If
I have two machines, 1 is ABC, the other is DEF, and switching between these two indeed gives different Y-axis offset with returned values.
The following worked before:
Code:
If (ParameterValue("'Positie bankvijs 1'") = 1) Then
Return 321.04
End If
If (ParameterValue("'Positie bankvijs 1'") = 2) Then
Return 321.04
End If
Is there a way to combine these two, so that it checks for the number given on 'Positie bankvijs 1' and the machinename?
May seem like the return is allways the samen, but it's not, once the first parameter hits 6-7-8-9 it returns a completely different value, as can be seen in my original post.
Thanks in advance to anyone giving it a shot!
Last edited by jmcilhinney; Apr 18th, 2024 at 07:12 AM.
-
Apr 18th, 2024, 07:13 AM
#6
Re: If statement return 'invalid unit'
Please don't use colour tags to highlight code. Use code tags. Most importantly, they maintain indenting.
-
Apr 18th, 2024, 07:17 AM
#7
Re: If statement return 'invalid unit'
 Originally Posted by Vermeirken
Is there a way to combine these two, so that it checks for the number given on 'Positie bankvijs 1' and the machinename?
Of course. That's basic Boolean logic, i.e. using AND and OR operators. If you don't remember Boolean logic from when you learned it at school, you should do a quick bit of reading on the subject but it works pretty much as you'd expect. We all use it all the time, if not exactly formally.
One thing to remember is that, while VB.NET does have And and Or operators, you should pretty much always use AndAlso and OrElse. The latter short-circuit while the former don't, so only use the former if you specifically need to avoid short-circuiting. You can read about those operators and how they work in the relevant documentation.
-
Apr 18th, 2024, 07:53 AM
#8
Re: If statement return 'invalid unit'
I suspect getting all the combinations of MachineName and ParameterValue("'Positie bankvijs 1'") is going to lead to messy if statements and a lot of code that can go wrong really fast. What I'm about to say I realize might be more advance than the OP is ready for, but I'm going to throw it out there anyways. Also keep in mind, some of this I'm shooting from the hip, and writing up w/o the use of an editor (I don't have VB.Net handy as I'm on a mac at the moment.) But here goes...
The way I'd solve the issue is a Dictionary(of String, Dictionary(of Integer, Decimal)) ... that's a dictionary of a dictionary. Let's start with the inner one first - Dictionary(of Integer, Decimal)
This first inner Dictionary contains the values you ultimately want to return. Ah let me back up... A Dictionary is a way of storing things by a key. It's essentially a Key-Pair value storage object. The nice thing about it is that you can indicate what the data type the keys are, as well as the data type of the values. In this case I'm saying the Key is an integer, and the values are doubles. To add an item to the Dictionary, you use the .Add method, which will take two parameters: one integer that is the key, and one decimal that is the value.
Code:
Dim myDictionary = new Dictionary(Of Integer, Decimal)
myDictionary.add(1, 321.04)
myDictionary.add(2, 321.04)
myDictionary.add(3, 321.04)
myDictionary.add(4, 321.04)
myDictionary.add(5, 321.04)
myDictionary.add(6, 461.54)
myDictionary.add(7, 416.04)
myDictionary.add(8, 338.54)
myDictionary.add(9, 261.04)
myDictionary.add(10, 183.54)
Now, once you have those in a Dictionary, accessing the items is as simple as: myDictionary(4) ... that will return "321.04"
Now to the outer Dictionary...
Dictionary(Of String, <the other Dictionary>)
Again, values can be what ever you'ver defined... in this case we plan to store our other dictionary in this one... concept is the same. Create a new Dictionary and add things to it.
Code:
Dim machineValues as Dictionary(Of String, Dictionary(Of Integer, Decimal)) = new Dictionary(Of String, Dictionary(Of Integer, Decimal))
'Using the myDictionary from above, we add it to this new. dictionary
machineValues.Add("Okuma MA-600 HIII", myDictionary)
At this point, you can then create a second Dictionary(Of Integer, Decimal) to then hold the values for "Matsuura MAM700HG" and add that to the same machineValues Dictionary.
Now we have the outer Dictionary that uses the Machine Name as its key.
I think you can now do this:
Code:
Dim myValue as Decimal = machineValues("Okuma MA-600 HIII")(1)
'or
Dim myValue as Decimal = machineValues(MachineName)(ParameterValue("'Positie bankvijs 1'"))
To get right to the value you want with out messy if statements or anything.
Worst case you have to get one dictionary out of hte other first:
Code:
Dim tempDict = machineValues(machineName)
Dim returnValue = tempDict(ParameterValue("'Positie bankvijs 1'"))
MessageBox.Show(returnValue)
-tg
-
Apr 18th, 2024, 09:17 AM
#9
Thread Starter
New Member
Re: If statement return 'invalid unit'
 Originally Posted by jmcilhinney
Of course. That's basic Boolean logic, i.e. using AND and OR operators. If you don't remember Boolean logic from when you learned it at school, you should do a quick bit of reading on the subject but it works pretty much as you'd expect. We all use it all the time, if not exactly formally.
One thing to remember is that, while VB.NET does have And and Or operators, you should pretty much always use AndAlso and OrElse. The latter short-circuit while the former don't, so only use the former if you specifically need to avoid short-circuiting. You can read about those operators and how they work in the relevant documentation.
Like I said in my OP, I have practically zero knowledge of VB, just trying to get some stuff working, meaning I didn't see any of this stuff in school 25 years ago 
Appreciating any help anyone is willing to give or try!
@techgnome: I tried your suggestions, to come up with following below:
Code:
Dim myDictionary = new Dictionary(Of Integer, Decimal)
myDictionary.add(1, 321.04)
myDictionary.add(2, 321.04)
myDictionary.add(3, 321.04)
myDictionary.add(4, 321.04)
myDictionary.add(5, 321.04)
myDictionary.add(6, 461.54)
myDictionary.add(7, 416.04)
myDictionary.add(8, 338.54)
myDictionary.add(9, 261.04)
myDictionary.add(10, 183.54)
Dim machineValues as Dictionary(Of String, Dictionary(Of Integer, Decimal)) = new Dictionary(Of String, Dictionary(Of Integer, Decimal))
'Using the myDictionary from above, we add it to this new. dictionary
machineValues.Add("MATSUURA MAM 700HG", myDictionary)
Dim myValue as Decimal = machineValues("Machinenaam")(ParameterValue("'Positie bankvijs 1'"))
Dim tempDict = machineValues("Machinenaam")
Dim returnValue = tempDict(ParameterValue("'Positie bankvijs 1'"))
This gives me an error 'The given key was not present in the dictionary'
-
Apr 18th, 2024, 03:31 PM
#10
Re: If statement return 'invalid unit'
That's because you put "MachineNAme" in quotes... so it's going to litterally look in the keys for "MachineNAme" ... if you look, that's not what I did... I'm assuming you have a variable called machineNAme and it contains a string that is... well the machine name.... this is what some of hte other posters were talking about in regards to this:
If ("MachineName" = "Okuma MA-600 HIII" )
Putting quotes around something makes it a literal string ... it doesn't make the variable a string, that should already be a string.
It's like trying to say if "A" = "B" ... when what you want is If A = "B"
-tg
-
Apr 18th, 2024, 08:59 PM
#11
Re: If statement return 'invalid unit'
 Originally Posted by Vermeirken
Like I said in my OP, I have practically zero knowledge of VB, just trying to get some stuff working, meaning I didn't see any of this stuff in school 25 years ago 
You don't need any knowledge of VB to understand Boolean logic. I didn't touch VB until I was in my 30s but I learned Boolean logic in maths class in school when I was about 13 or 14 I think. Also, it's mainly common sense anyway. As I said, we all use Boolean logic all the time in our everyday lives. We just don't think of it formally, e.g. (A and B) is true if and only if A is true and B is true and (A or B) is true if and only if A is true or B is true. What's not to understand?
-
Apr 19th, 2024, 02:26 AM
#12
Thread Starter
New Member
Re: If statement return 'invalid unit'
Code:
Dim myDictionary = new Dictionary(Of Integer, Decimal)
myDictionary.add(1, 321.04)
myDictionary.add(2, 321.04)
myDictionary.add(3, 321.04)
myDictionary.add(4, 321.04)
myDictionary.add(5, 321.04)
myDictionary.add(6, 461.54)
myDictionary.add(7, 416.04)
myDictionary.add(8, 338.54)
myDictionary.add(9, 261.04)
myDictionary.add(10, 183.54)
Dim machineValues as Dictionary(Of String, Dictionary(Of Integer, Decimal)) = new Dictionary(Of String, Dictionary(Of Integer, Decimal))
'Using the myDictionary from above, we add it to this new. dictionary
machineValues.Add("MATSUURA MAM 700HG", myDictionary)
machineValues.Add("Okuma MA-600 HIII", myDictionary)
Dim myValue as Decimal = machineValues(Machinenaam)(ParameterValue("'Positie bankvijs 1'"))
Dim tempDict = machineValues(Machinenaam)
Dim returnValue = tempDict(ParameterValue("'Positie bankvijs 1'"))
I'm getting what you are saying now, with the quotes, it's literally comparing the two texts between the quotes, without the quotes it will search for the variable Machinenaam.
I adjusted this in my code, it will accept this, but gives me a notification "Unit isn't correct"
Does this mean it's not correctly outputting the required values?
Code:
If (Machinenaam = "MATSUURA MAM 700HG") Then
Return 250
End If
If (Machinenaam = "Okuma MA-600 HIII") Then
Return 150
Else
Return 0
End If
Just to check if it correctly finds the variable I made this, and this works, it switches between 250 and 150 when I switch machines.
-
Jun 5th, 2024, 09:08 AM
#13
Thread Starter
New Member
Re: If statement return 'invalid unit'
Anyone any idea as to why it gives an incorrect unit?
If not, then I will close this topic and give up or try to find another way to get it.
Thanks in advance!
-
Jun 5th, 2024, 05:36 PM
#14
Re: If statement return 'invalid unit'
Where is it saying that? Which line?
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|