Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can someone explain a case statement to me

22 REPLIES 22
Reply
Message 1 of 23
jletcher
1349 Views, 22 Replies

Can someone explain a case statement to me

I looked at the help but you know it is no help for me LOL.

 

 If you have an example it would be nice I am on 2012 still.

 

 I am trying to get omething to work and it sounds like a case statement would work but seem not to understand it enough to get it to work LOL.

 

 

  Thanks Smiley Happy

22 REPLIES 22
Message 2 of 23
Anonymous
in reply to: jletcher
Message 3 of 23
jletcher
in reply to: Anonymous

Carthik,

 

  Thanks but I guess you did not see where I said the help does not help to understand it for me.

Message 4 of 23
falkmassmann
in reply to: jletcher

Maybe this helps:

 

http://www.dotnetperls.com/select-case-vbnet

 

 

The case statement is nothing more then I would say advanced If then Else statement.

You would check a variable for different values and state in every case that´s possible what to do.

Easy example would be a boolean which you would check for both values true and false.

 

Case True

   'some vb code for this case

Case False

   'some vb code for this case

 

Of course you could do this with If then Else as well, but if you have more values for example from 1 to 6 you would need to check for every CASE possible e.g. 1,2,3,4,5,6.

 

All the best

 

Falk

Message 5 of 23
jletcher
in reply to: falkmassmann

Falk,

 

  Thank you that gave some more understanding but still not grasping.

 

 

   You say it is like a If...Then statement but all examples I have seen only return a value.

 

 Can a case statement do things like turn on and of features?

 

Example:

 

 I have a shaft with sprockets can be one or it can be 2,3,4,5 even more

 

 I would like if numbers are odd sprocket is in center of shaft  but more then 1 sprocket will move to off center and turn on a pattern to array the sprockets.

But also have an option if sprocket offset is equal (true) turns on one pattern if False turns 2 other patterns on.

 

 Is this something a Case statement would be used for?

 

 Thanks...

Message 6 of 23
Anonymous
in reply to: jletcher

Hi,

 

 

 

"Case Statement" is best used when you have to explicitly check the value of a variable:

 

Sub Sprockets()
     Dim Sprocket As Integer
     Sprocket = 1

     'Explicitly check the value of the variable "Sprocket"
     Select Case Sprocket
          Case 1
               MsgBox "Sprocket = " & Sprocket
          Case 2
               MsgBox "Sprocket = " & Sprocket
          Case 3
               MsgBox "Sprocket = " & Sprocket
          End Select

     'You could also do a logic check on Sproket variable with select case

     Select Case Sprocket
          Case Is > 3
               MsgBox "Sprocket = " & Sprocket
          Case Is < 2
               MsgBox "Sprocket = " & Sprocket
     End Select

     'But to do these logic check, If Then Statements are prefered in my opinion

     If Sprocket > 3 Then
          MsgBox "Sprocket = " & Sprocket
     ElseIf Sprocket < 2 Then
          MsgBox "Sprocket = " & Sprocket
     End If

     'Therefore if you want to check if a value is odd or even I think it's best with If...Then Statement
     If CBool(Sprocket Mod 2) Then 'odd
          MsgBox "Sprocket is ODD"
     Else
          MsgBox "Sprocket is EVEN"
     End If

     'Finaly, you could mix both If...Then and Select Case statements
     Dim Size_State As String
     Size_State = "Big"
     If CBool(Sprocket Mod 2) Then 'odd
          Select Case Size_State
               Case "Big"
                    MsgBox "Sprocket is ODD and Big"
               Case "Small"
                    MsgBox "Sprocket is ODD and Small"
          End Select
     Else
          Select Case Size_State
               Case "Big"
                    MsgBox "Sprocket is EVEN and Big"
               Case "Small"
                    MsgBox "Sprocket is EVEN and Small"
          End Select
     End If

End Sub

 

Message 7 of 23
jletcher
in reply to: Anonymous

Thank you that gave me some understanding.

 

 Now what I see is it can only do messages with a case statement I cannot control features with it.

 

 Am I correct?

 

 Thanks

Message 8 of 23
Anonymous
in reply to: jletcher

No, no. You could execute what you want as with the If...Then statements.

I put "MsgBox" just for example, I could have put anything else.
Message 9 of 23
Anonymous
in reply to: jletcher

Show me what you tried (code) then maybe I could help more.
Message 10 of 23
jletcher
in reply to: Anonymous

Well I started with If... Then statements but seemed not to be able to get it to work so I started looking into case statements but nothing worked. I blamed that on my knowledge so that is why I asked.

 

 

  I have not tried the case statement yet that is what I am doing now thanks to you and others.

 

 As soon as I get something I will post with issues or post with IT WORKS LOL..

 

 Going to just try something simple and if it works I will add the rest..

 

 Thanks I will get back with you on the help Lord knows I need it...

 

 

Message 11 of 23
jletcher
in reply to: Anonymous

 Does the case statement work differnt in ilogic as to VB?

 

 I am trying to get it to work with ilogic and I get error right off the bat.

 

 Tells me Type " Interger " is not defined

 

Does this not define it?

 

Dim Number_Of_Sprockets As Interger
Number_Of_Sprockets = 1

 

Can I use a parameter that is linked with an assembly using ilogic?

 

 

 

Message 12 of 23
Anonymous
in reply to: jletcher

it is "Integer" not "Interger"
Message 13 of 23
jletcher
in reply to: jletcher

Just notice I spelled Integer wrong I changed it and have new error

 

  "value" is not a member of "Integer"

 

 Thanks

Message 14 of 23
Anonymous
in reply to: jletcher

Just write your code as you think and then post it. Then I'll help debug it as well as I can.

Message 15 of 23
jletcher
in reply to: Anonymous

Ok will do..

 

 You may not see it tell morning short day for me..

 

 unless I get lucky..

 

  Thanks.....

Message 16 of 23
jletcher
in reply to: Anonymous

Ok I have this so far.

 

Parameter("Sprocket_Count") = Number_Of_Sprocket


Select Case Sprocket_Count
Case  1
Feature.IsActive("Sprocket 1") = False
Feature.IsActive("Sprocket 2") = False
Feature.IsActive("Center Sprocket")  = True
Case  2
Feature.IsActive("Sprocket 1") = True
Feature.IsActive("Sprocket 2") = True
Feature.IsActive("Center Sprocket")  = False
Case  >= 3
Feature.IsActive("Sprocket 1") = False
Feature.IsActive("Sprocket 2") = False
Feature.IsActive("Center Sprocket")  = False
Feature.IsActive("Center Multi") = True


End Select

 

 It does what is needed so far.

 

 Question is there a way to link a and or andalso to it?

 

 If there is 1 sprocket no issue it would be centered on the keyway in the shaft if the count is 2 the sprocket is offset no issue If the count is 3 the sprocket is centered on the keyway and pattern is turned on that is equal no issue.

 

 The issue is when I have more then 4 sprockets the spacing may not be equal it may go 2" space then a 3" space . or if it has 6 it may not be equal spacing Is there a way for it to check two conditions and if meet do this?

 

 And do I have to make a line of code for each range or can it be lumped into 1 to 2 statements

 

 Like

 

 Case 1

   BLA BLA BLA

Case 2

 BLA BLA

Case 3,7,9

 BLA BLA BLA BLA

 

  Or is there a way to tell it after this number and this is true do this?

 

 I did put >= on the 3 just to test something but it will not work on even number of sprockets that was just to see what it would do..

 

Thanks..........

 

 

Message 17 of 23
jletcher
in reply to: jletcher

I tried to add this to handle my issue but "Mod 2" seems to stop it from working.

 

 

Parameter("Sprocket_Count") = Number_Of_Sprocket


Select Case Sprocket_Count
Case  1
	Feature.IsActive("Sprocket 1") = False
	Feature.IsActive("Sprocket 2") = False
	Feature.IsActive("Center Sprocket")  = True
	Feature.IsActive("Center Multi") = False
	Feature.IsActive("Center Pattern") = False
Case  2
	Feature.IsActive("Sprocket 1") = True
	Feature.IsActive("Sprocket 2") = True
	Feature.IsActive("Center Sprocket")  = False
Case  (Sprocket_Count Mod 2) >= 3
	Feature.IsActive("Sprocket 1") = False
	Feature.IsActive("Sprocket 2") = False
	Feature.IsActive("Center Sprocket")  = False
	Feature.IsActive("Center Multi") = True
	Feature.IsActive("Center Pattern") = True
Case (Sprocket_Count Mod 2) >= 4
	Feature.IsActive("Sprocket 1") = False
	Feature.IsActive("Sprocket 2") = False
	Feature.IsActive("Center Sprocket")  = False
	Feature.IsActive("Center Multi") = False
	Feature.IsActive("Center Pattern") = False

End Select

 

 Thanks

Message 18 of 23
jletcher
in reply to: jletcher

Ok I added a few things works so far..

 

 Only need to find out if you can have a 2 condition met do this.

 

Here is what I have so far..

 

Parameter("Sprocket_Count") = Number_Of_Sprocket


Select Case Sprocket_Count
Case  1
	Feature.IsActive("Sprocket 1") = False
	Feature.IsActive("Sprocket 2") = False
	Feature.IsActive("Center Sprocket")  = True
	Feature.IsActive("Center Multi") = False
	Feature.IsActive("Center Pattern") = False
Case  2
	Feature.IsActive("Sprocket 1") = True
	Feature.IsActive("Sprocket 2") = True
	Feature.IsActive("Center Sprocket")  = False
Case  3,5,7,9,11,13,15,17,19 
	Feature.IsActive("Sprocket 1") = False
	Feature.IsActive("Sprocket 2") = False
	Feature.IsActive("Center Sprocket")  = False
	Feature.IsActive("Center Multi") = True
	Feature.IsActive("Center Pattern") = True
Case 4,6,8,10,12,14,16,18,20  
	Feature.IsActive("Sprocket 1") = True
	Feature.IsActive("Sprocket 2") = True
	Feature.IsActive("Center Sprocket")  = False
	Feature.IsActive("Center Multi") = False
	Feature.IsActive("Center Pattern") = False

End Select

 

 Thanks.......

Message 19 of 23
Anonymous
in reply to: jletcher

"Mod 2" will give 0 or 1. It is the remaining of the division. It gives 0 if Even number or 1 if Odd. So your last case will never happen.
Message 20 of 23
jletcher
in reply to: Anonymous

Oh I see I thought it would carry like

 

Mod 2 = 1 would do 3,5,7,9 etc..

 

Mod 2 = 2 would do 4,6,8,10 etc.

 

 Thanks

 

 Any thought on the true/false for equal and none equal spacing?

 

 I tried

 

Case 2 andalso Sprocket_Equal_Spacing = True

 

 But it also did nothing..

 

 Many Thanks

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report