BadProgrammer Posted March 13, 2009 Posted March 13, 2009 (edited) First I apologize if this has been answered elsewhere, but I have looked and have yet to find anything. I also apologize b/c i've only been programming in VB for less than 6 months and am probably doing things very wrong. Below is a snippet of code im trying to get to run: Dim ca1co As New ArrayList Dim ca1so As New ArrayList Dim chas As New ArrayList Dim spin As New ArrayList Dim ca1c As New ArrayList Dim ca1s As New ArrayList ' Control Arm 1 Chassis Points ca1co.Add(TextBox1.Text - TextBox33.Text) ca1co.Add(TextBox10.Text - TextBox32.Text) ca1co.Add(TextBox15.Text - TextBox31.Text) ' Control Arm 1 Spindle Points ca1so.Add(TextBox30.Text - TextBox33.Text) ca1so.Add(TextBox25.Text - TextBox32.Text) ca1so.Add(TextBox20.Text - TextBox31.Text) ' Call length function chas = ca1co spin = ca1so dlink1 = Length(chas, spin) dlink = dlink1 errorig1 = errorigsum(dlink) tstart = TextBox34.Text tend = TextBox35.Text For trav = tstart To tend Step tstart Dim chast As New ArrayList Dim spint As New ArrayList ' Link 1 Chassis Movement chast = ca1co spint = ca1so dlink = dlink1 dlinkt1 = chasmove(chast, spint, trav) errt1 = errmove(dlink, dlinkt1) 'Reset variables for more looping ca1c = ca1co ca1s = ca1so Next trav My problem is as i do the loop, the ca1co and ca1so values change, but I never call those variables anymore in the loops. I need a way to keep the ca1co and ca1so values the same. The chas, spin, ca1c, and ca1s values can and should all change throughout the loops. any ideas? I tried arraylist.clear in the line before 'next trav' but then the variables are gone in the next iteration of the loop. i tried to redim but i got an error about needing to give the array size?!?! i cant move forward in my code until i figure this issue out. Thanks! Edited March 14, 2009 by PlausiblyDamp Quote
Leaders snarfblam Posted March 13, 2009 Leaders Posted March 13, 2009 Don't take this the wrong way, but your code makes me want to cry a little. Treating textbox text like numbers and throwing cryptically named arraylists around is bound to make the program prone to chrashing spectacularly. Just so you know, there are and [Code] tags you can use to make the code more readable. As far as the code itself, I highly recommond enabling option strict. It helps identify potential problems with invalid type-casting (i.e. treating a string as a number when it could contain something else). I know you didn't ask for tips on general programming practices but it is hard to answer your question because your code is difficult to follow. Some of your code doesn't make much sense. You are assigning the value of ca1co to chast, then passing it into the charmove function, which may or may not be modifying it (I don't have that code). If you assign ca1co to chast, they both point to the same ArrayList and changing one changes the other. I also see you assigning a new ArrayList to every variable in the declaration, then immediately assigning a different ArrayList to the same variable (from another variable). It looks like you might have some misunderstanding about exactly how ArrayLists work (or you may even misunderstand how reference-type variables work, I don't know). I would post some code, but I don't really understand what the code is supposed to do. Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted March 14, 2009 Administrators Posted March 14, 2009 Where / how are the variables tstart, trav, dlink and dlink1 declared? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BadProgrammer Posted March 14, 2009 Author Posted March 14, 2009 dlink,tstart, and tend are all declared at the beginning. i just didn't include that in the code i posted. i will take all tips i can get on general programming. like i said, i'm no programmer. never took a class in it and im used to programming in Matlab as well. the ca1co and ca1so values were reassigned to chast and spint because i call the chasmove function 5 times in the full blown code so instead of writing that function 5 times with ca1co, ca1so, ca2co, etc. as the inputs, i just wrote the function once and passed it the chast and spint because i can set those variables equal to what it needs to be within the loop. chasmove doesn't modify the input variables. originally i wasn't assigning a new arraylist with each call. i put that in there trying to see if that would keep the chast and spint variables from changing, but it didn't, so i can take it out. the premise is in the For loop i move 5 links with the chasmove function (this is just a subtraction and sqrt function). once that function is complete, there are more functions that are called in other loops that move other points. when it's time for the next For iteration, it doesn't increment from the original 5 link points. it starts at the previous points. im still not sure if im describing this well. how/where do i find/use the vb and code tags to try and post the code up better. i didn't post it all originally b/c it's ALOT of code. thanks for your help so far and for being understanding. Quote
Leaders snarfblam Posted March 14, 2009 Leaders Posted March 14, 2009 [PLAIN]Simply place and around your VB code, like so: SomeFunction() 'SomeComment And it will come out like so:[/PLAIN] SomeFunction() 'SomeComment My biggest criticism would be your use of non-descriptive variable, control, and function names for exactly the problem we have here: I can't understand what is going on so I don't know how to help. Besides it being difficult to understand the overall gist of the code, it's hard to decipher what, particularly, each individual line of code does because it isn't clear what the variables represent, where their values come from, or what a function does (or why it was called). The code below is a lot closer to what I would hope for in terms of readability. I don't expect you to use the code, just notice how and where variables are declared, named, and used. Public Function Subtract(ByVal a As TextBox, ByVal b As TextBox) As Integer Return Integer.Parse(a.Text) - Integer.Parse(b.Text) End Function . . . Dim armChassisPoints As New ArrayList Dim armSpindlePoints As New ArrayList 'The comments you had here become unnecessary. It is self-commented with descriptive variable names armChassisPoints.Add(Subtract(chassisAStart, chassisAEnd)) armChassisPoints.Add(Subtract(chassisBStart, chassisBEnd)) armChassisPoints.Add(Subtract(chassisCStart, chassisCEnd)) armSpindlePoints.Add(Subtract(chassisAMid, chassisAEnd)) armSpindlePoints.Add(Subtract(chassisBMid, chassisBEnd)) armSpindlePoints.Add(Subtract(chassisCMid, chassisCEnd)) ' Call length function -- but do we need a comment to realize this? dlink1 = Length(armChassisPoints, armSpindlePoints) dlink = dlink1 errorig1 = errorigsum(dlink) loopStep = Integer.Parse(stepSizeInput.Text) loopEnd = Integer.Parse(loopEndInput.Text) 'There should be a comment explaining why the step value is the same as the starting value For index As Integer = loopStep To loopEnd Step loopStep 'Variables can be initialized on declaration, but I commented them out altogether 'because they aren't necessary in the posted code 'Dim chast As ArrayList = armChassisPoints 'Dim spint As ArrayList = armSpindlePoints ' Link 1 Chassis Movement dlink = dlink1 dLinkt1 = MoveChassis(armChassisPoints, armSpindlePoints, index ) errt1 = errmove(dlink, dlinkt1) 'Also commented out because they arent necessary as far as the posted code goes 'ca1c = armChassisPoints 'ca1s = armSpindlePoints Next index The code may look slightly more cluttered, but others (and you a year down the road) can see what it does a lot more clearly. Quote [sIGPIC]e[/sIGPIC]
BadProgrammer Posted March 15, 2009 Author Posted March 15, 2009 ok thanks. i understand your layout and will try to implement it. I have the comments in there b/c I'm showing this to someone else so the comments are there more for them and not so much for me, plus i used to get yelled at for not having comments back in the day so i insert them everywhere now. the reason the loop step size is the same as the initial value is b/c thsi is reading off of a GUI and I haven't put a textbox on the GUI yet where the user can enter the loop step size. i have a question concerning the armChassisPoints and armSpindlePoints. like i said I actually have 5 sets of chassis and spindle points which are all different. this is why i had declared the variables chast and spint. is there a way to call the chasmove function with 5 different armChassisPoints and armSpindlePoint values without having to write the chasmove function 5 times with specific inputs? i need the ca1c and ca1s variables because in the rest of the code i modify those values. this is were the problem is. for the main For loop, i need the armChassisPoints and armSpindlePoints to stay as they are defined, but i also need to assign those points to a different variable so that i can modify those points in the rest of the code. i guess it basically the loops (after everything is declared) needs to look like this: For index As Integer = loopStep To loopEnd Step loopStep ' Link 1 Chassis Movement dlink = dlink1 dLinkt1 = MoveChassis(armChassisPoints, armSpindlePoints, index ) errt1 = errmove(dlink, dlinkt1) ' Link 2 Chassis Movement dlink = dlink2 dLinkt2 = MoveChassis(armChassisPoints, armSpindlePoints, index ) 'this is the question i have concerning calling the function. the values of input here are different than the input for link 1. this would continue for the other 3 links errt2 = errmove(dlink, dlinkt2) ca1c = armChassisPoints ca1s = armSpindlePoints ' i know the variable names are not great here, but they aren't used for anything more than to know what value of xmove i need to use. For j = 1 To 1 If j = 1 Then xmove = 0.0001 ElseIf j = 2 Then xmove = 0.00001 End If ' Link 1 Wheel Attachment x Movement chas = ca1c spin = ca1s dlink = dlink1 dlinkt1 = spinxmove(chas, spin, xmove) errwheelt1 = errmove(dlink, dlinkt1) ' Link 2 Wheel Attachment x Movement chas = ca2c spin = ca2s dlink = dlink2 dlinkt2 = spinxmove(chas, spin, xmove) errwheelt2 = errmove(dlink, dlinkt2) Next j Next index what i was running into was that the ChasMove function was using the previous moved value i.e, say original value is 12.5, moved valued is 12.4 (12.5-.1), on the next iteration of the For loops the moved value should be 12.3 (12.5-.2) but what i'm getting is 12.2 (12.4-.2). again, i apologize for the lack on knowledge in this, but i'm trying to learn and figure out how to do it better. Thanks! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.