*Experts* DiverDan Posted March 15, 2004 *Experts* Posted March 15, 2004 Here's my function: Private Function Adjust_Gauges4VoltageDrop(ByVal informationFrom As String, ByVal locationLine As Integer, ByVal conductorGauge As String, ByVal groundGauge As String, ByVal amperes As Double, ByVal numOfParallelConductors As Double, ByVal voltageDropPercent As Double) As String If INI_Info(121) = "without" Then Return "not required" Dim NECTable, conductorMaterial, tempGroup As String Dim voltage, numParallelConductors, Rwire As Double 'get variable info If informationFrom = "Calc" Then NECTable = usageTable conductorMaterial = cbWireMaterial.Text tempGroup = InsulationTemperatureGroup voltage = Val(tbVolt.Text) ElseIf informationFrom = "Grid" Then NECTable = lvData.Items(locationLine).SubItems(Col(40)).Text conductorMaterial = lvData.Items(locationLine).SubItems(Col(24)).Text tempGroup = Val(lvData.Items(locationLine).SubItems(Col(41)).Text) voltage = Val(lvData.Items(locationLine).SubItems(Col(5)).Text) End If 'check voltage drop value If voltageDropPercent <= Val(INI_Info(120)) / 100 Then Return conductorGauge & "," & groundGauge & "," & voltageDropPercent Else 'increase conductor gauge value by 1 conductorGauge = IncreaseGauge(conductorGauge) 'increase ground gauge if necessary Dim maxAmps As Integer = Get_MaxAmps("Conductor", NECTable, conductorMaterial, tempGroup, voltage, conductorGauge) 'Max Conductor Ampacity Dim parallelConductorCount As Double = numOfParallelConductors If parallelConductorCount = 0 Then parallelConductorCount = 1 GroundInfo = Split(Table250_122(maxAmps * parallelConductorCount, conductorMaterial), ",") groundGauge = convertGaugeFromAWG(INI_Info(62), GroundInfo(0)) 'Calculate Wire Resistance Rwire = Calc_Rwire(informationFrom, locationLine, conductorGauge) 'get voltage drop percentage Dim voltageDrop As Double = Rwire * amperes 'Amps(actual) voltageDropPercent = voltageDrop / voltage 'recurse Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) End If End Function It calculates the gauges and voltage drop percentage perfectly but returns nothing as its value??? What have I done wrong this time? Thanks Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
wyrd Posted March 15, 2004 Posted March 15, 2004 Easiest way to find this problem is using the debugger. Set a break point at the first line of the function (click the sidebar at the side of your editor and a red dot will appear), then run the app in debug mode. When it reaches that function, it'll stop and allow you to look at the source and all values set thus far (little debug window at the bottom left of your screen). You can then use the step commands (should be on your menu bar) to step through each line of code. Check & verifty each value as you go through the function, and I'm sure you'll notice something out of place. Quote Gamer extraordinaire. Programmer wannabe.
*Experts* DiverDan Posted March 15, 2004 Author *Experts* Posted March 15, 2004 (edited) Thanks wyrd, but that's not the problem. I can use Return "dan" and still get nothing??? Using the debugger and a MessageBox.Show shows that the function is calculating perfectly and (if I can every figure why it returns nothing) will return a csv string with the correct values. This is the way I call the function: Dim adjustedGauges() As String = Split(Adjust_Gauges4VoltageDrop("Calc", 0, tbConductorGauge.Text, tbGroundGauge.Text, RemoveSeperator(items(Col(6))), numOfParallelConductors, VoltageDrop / Volts), ",") If Not adjustedGauges(0) = "not required" Then 'adjustedGauges(0) = conductor gauge 'adjustedGauges(1) = ground gauge 'adjustedGauges(2) = voltage drop percent For i = 0 To UBound(adjustedGauges) MessageBox.Show("i = " & adjustedGauges(i)) Next End If i = nothing because nothing is received??? Thanks, Dan Edited March 15, 2004 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
wyrd Posted March 15, 2004 Posted March 15, 2004 Well, stepping through with the debugger should show exactly why it's returning nothing. But anyway, by inspection, it looks like it's returning nothing because your recursive call isn't returning anything; 'recurse Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) Quote Gamer extraordinaire. Programmer wannabe.
*Experts* DiverDan Posted March 15, 2004 Author *Experts* Posted March 15, 2004 In the If voltageDropPercent <= Val(INI_Info(120)) / 100 Then conditional if I put a MessageBox.Show(conductorGauge & "," & groundGauge & "," & voltageDropPercent) the correct answer will be displayed. Changing the Function to a Sub with a global variable will work, but it's not the correct way of accomplishing this task. Thanks, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
wyrd Posted March 15, 2004 Posted March 15, 2004 Reread my reply. ;) Try putting the keyword Return in front of this line; Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) Return Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) :P Quote Gamer extraordinaire. Programmer wannabe.
*Experts* DiverDan Posted March 15, 2004 Author *Experts* Posted March 15, 2004 Hi again wyrd, Return Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) will not work with a function as you can not return a function, only its computed results. However, Return informationFrom & "," & locationLine & "," & conductorGauge & "," & groundGauge & "," & amperes & "," & numOfParallelConductors & "," & voltageDropPercent returned the correct values. I really think the function is good...actually, very good. I think the way I called the function is not so very good and is producing the problem. As stated earlier, if I change the Function into a Sub and set a global variable instead of Return, the correct values will be produced. But this is not the correct manner to solve this task, a function (similar to a class) is the correct method and I have definately missed something in calling the function. Thanks, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
wyrd Posted March 15, 2004 Posted March 15, 2004 You can't return a function call in VB? Well anyway, just do this then (syntax may be off, I haven't done anything in VB for years); Dim str As String = Adjust_Gauges4VoltageDrop(informationFrom, locationLine, conductorGauge, groundGauge, amperes, numOfParallelConductors, voltageDropPercent) Return str From what I can tell, the problem is that the recursive function call is not returning its results. So basically your function recursively calls Adjust_Gauges4VoltageDrop(...) and then abruptly ends. Because of that (you'd get an error in C# since you didn't return anything) your function is giving you Nothing as the return value. If that doesn't work (it should, darnit), then you could just pass a string ByRef and use that to store the results (this is in response to your "global" solution). This would of course cause you to pass in another paremeter as a string, something like ByRef returnVal As String. Quote Gamer extraordinaire. Programmer wannabe.
dragon4spy Posted March 15, 2004 Posted March 15, 2004 There are many problems with your Function. I'm checking for you :D Quote Don't judge a man by his look. Don't judge a book by its cover. :D
dragon4spy Posted March 15, 2004 Posted March 15, 2004 (edited) 'check voltage drop value If voltageDropPercent <= Val(INI_Info(120)) / 100 Then Return conductorGauge & "," & groundGauge & "," & voltageDropPercent '<<<< Put break point here Put a break point on this return line, and see when your function is exectuted, will it stop at the break point or not? If nothing happen then it must be because voltageDropPercent > Val(INI_Info(120)) / 100 . :rolleyes: I suggest you to learn more about Debugger. I tell you I used to face this before when I started vb. :D Edited March 15, 2004 by dragon4spy Quote Don't judge a man by his look. Don't judge a book by its cover. :D
*Experts* DiverDan Posted March 15, 2004 Author *Experts* Posted March 15, 2004 I'm giving up... The recursive function works just fine. Shy of an atmospheric nuclear explosion, voltageDropPercent > Val(INI_Info(120)) / 100 is absolutely impossible with the formulae and computations that are taking place. Larger diameter conductors with the same amperes will always have less resistance (Rwire) and therefore the resulting voltage drop will also always be less with no exceptions (it's an OHM's Law thing - which isn't really a law by defination but actually a relationship). If I use a string variable, as suggested by wyrd, then the correct values are returned, so that is what I have done as I have no more time to spend on this function today. Thanks wyrd, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
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.