Find Min/Max from 1 textbox - vb.net

I'm trying to find the Min/Max value of a textbox that has 1 variable and display it to the user, so the variable changes everytime the button is clicked. How would I find the maximum value of something that is constantly changing? The trick is that I can NOT use if statements or case statements. I'm totally at a loss here.

Ok, the things that limit you.
One variable
no if/case statements
The lesson seems to revolve around using Math.Max().
Math.Max(), as we can see on MSDN returns
the larger of two 32-bit signed integers.
The one variable we are going to use needs to exist outside of the button's click event. So, just make it a class variable.
This variable will essentially store the largest value. Math.Max() returns the largest of two values... see what I am getting at here? You can pass the current largest variable as a parameter to Math.Max() without any issues.
Example:
Dim max As Integer
max = Math.Max(1, 100)
'max would be 100
max = Math.Max(max, 10)
'max would be 100
max = Math.Max(max, 1000)
'max would be 1000

Related

How to Fix #Error When Calculating a DateDiff With an IIF Statement in SSRS

I'm creating a report in Reporting Services to display time-spans for classroom sessions in our program. One column that was requested is to show the total number of hours between the start and end time of one session. I used a DATEDIFF function showing the DateInterval in hours as the total hours in one time-span, but I get a #Error returned for the blank values that haven't been assigned a begin or end time.
I added an IIF statement to mark the values that are blank as "TBA", but that didn't work. Since I'm casting a string to a datetime value, I enclosed the DATEDIFF statement in a CStr function, but that also isn't working. Lastly, I did the simple thing and set the text box to display a Number value since it's returning the total number of hours, but of course, that didn't work either. My code is as follows:
=IIF(Fields!BeginTime.Value = "", "TBA", CStr(DateDiff(DateInterval.Hour, Fields!BeginTime.Value, Fields!EndTime.Value)))
It's supposed to show that "TBA" value in the blank spots, but I still get a #Error. The total hours are coming up fine for the values that aren't blank, however. I checked the value in the dataset to see if it's returning a blank or a NULL-labeled value, and it is returning a blank value.
I believe the issue is arising in the conditional statement. If Fields!BeginTime.Value is a date datatype, comparing it to "" will likely result in the error you're getting. I suggest using the IsNothing function or just checking if the field is nothing. The two options are as follows.
=IIF(IsNothing(Fields!BeginTime.Value), ...
Or...
=IIF(Fields!BeginTime.Value Is Nothing, ...
With that said, you shouldn't need to use the CStr as DateDiff will return an integer value. Your textbox should be set to Default or some text variant to prevent errors there. The expression will return either an integer or string so a number format on the textbox will not work.
Additionally, if you're having the same issue when there is no EndTime, you may want to add an OR to the conditional to fix that.
=IIF(IsNothing(Fields!BeginTime.Value) OR IsNothing(Fields!EndTime.Value), ...

Check one series value, change corresponding point colour in 2nd series

I have an automated pareto chart working well in Excel.
I want to change it to check if points in the cumulative percentage series have a value of less than 80% and colour the same number point in the count series if this is the case.
Tried this; doesn't throw any errors but also doesn't work.
I think it should be iterating through a list of values, checking if they are <80 then updating the point at the corresponding i in the count series.
Set percVals = paretoChart.Chart.SeriesCollection(percentSeries).Values
For Each i in percVals
If percVals(i) = <80 Then
With paretoChart.Chart.SeriesCollection(countSeries).Points(i)
.Format.Fill.ForeColor.RGB = RGB(0,0,225)
.Format.Line.ForeColor.RGB = RGB(0,0,225)
End With
End If
Next i
The "paretoChart" variable is set earlier in the code and is working fine throughout.
I've tried without the 'with' statement. No difference.
There is a similar question comparing the two values across series, but I don't think I can adapt that to work here. Any ideas appreciated!

What is the best/fastest method of filling an array with numbers between two values?

I've got a background thread which adds numbers to an array, this call happens often so i'm just curious if there's a faster method of adding the numbers to the array?
I need to add all the numbers between two values (values will be different depending on the situation), example 1 to 4 which would add 1,2,3 & 4.
However, when I make the call, it would be adding much larger arrays similar to 500 to 1000 etc which sometimes takes a little longer depending on how many numbers need to be added.
At the moment i'm using something similiar to this:
Dim ListOfNumbers As New List(Of Integer)
For i = 1 To 100000
If ListOfNumbers.Contains(i) = False Then
ListOfNumbers.Add(i)
End If
Next
Is there any other method that I could use which might be faster?
(Avoiding duplicate values in the array if possible)
I doubt this is driving your program's performance. Moreover, the best option may depend on how you plan to use these numbers, where you may find you actually can noticeably improve performance by using something like IEnumerable throughout your code, rather than an array.
With that in mind, I suggest this option:
int start = 1; int stop = 100000;
var list = Enumerable.Range(start, stop - start).ToArray()
Such that you can easily remove the ToArray() later if desired.
As for the existing code... You control both the List and the loop. Checking .Contains()is not necessary, and probably taking up a significant part of the execution time here. Just remove that check. You can also optimize some by pre-setting the size of the list:
Dim size As Integer = 100000
Dim ListOfNumbers As New List(Of Integer)(size)
For i = 1 To size
ListOfNumbers.Add(i)
Next

Adjusting number format using vb.net

I have this statement nested in an IF-Statement under a scenario. I would like it to format column 2 into a Accounting number without decimal places. How can this be done using VB.net? The current code gives me ex: 1,000,000.00 on an input of 1000000. I noticed excel has buttons for adding or subtracting decimal places. Could these be called within VB.net? Thanks!
Lo.ListColumns("Column2").DataBodyRange(CurrentRow).NumberFormat = "ACCOUNTING"
Try the following
Lo.ListColumns("Column2").DataBodyRange(CurrentRow).NumberFormat = "#0,000.00"
You may find help in this Article
From what i understand you want, you can do:
math.floor(*insert the value or variable you want here*)
What that does is it changes the number in the parameter to the biggest integer lower than the parameter.
Hope this works :)

VB.NET CHART- Display a subset of the total data points

VB 2008
.NET FRAMEWORK 3.5
MSCHART - FASTLINE CHART TYPE
Is it possible to have an MS Chart control contain 20,000 data points, but only show the last 100?
I know that I can select the last 100 from my datatable and use it as a datasource.
Chart1.DataSource = cMs2.dsData.Tables("readings").Select(wFilter, wSort).Take(100)
That's not what I want.
I know that I can populate an array or collection with the last 100 data points and use it as a data source.
Chart1.Series("readings").Points.DataBindXY(colCtr, colReadings)
That's not what I want.
I need to do 1 of 2 things:
Manually add data points and be able to show only the last 100 or last 1000 of them that just came in. This must be done without re-populating the chart. Just show a portion of the complete set of data points.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
Chart1.Series("readings").SHOWONLYTHELAST100DATAPOINTSWITHOUTCLEARING
Initialize a chart with a certain number of the total readings with a databind, then manually add new data points one at a time while removing the oldest data point. For example, initialize the chart with 100 data points, then add a new data point, remove the first data point, getting us back to 100. (I'm successfully doing this one, except the chart doesn't behave like I expect. The chart grows, remaining blank/empty where the 'removed' data points were. I do chart.update but it doesn't refresh it.) Note that I am allowed to take more time to initialize the chart (clear/populate), I don't have that time to do it as each new data point comes in.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
If Chart1.Series("readings").Points.Count > 100 Then
Chart1.Series("readings").Points.RemoveAt(0)
Chart1.Update()
End If
NOTE: Doing a process that causes me to have to clear and rebind the data to handle the addition of a single data point causes me problems because it takes too long. I'm just looking for the quickest, most efficient way to handle this. Thank you for taking the time to read this...!
You can manually set the Minimum and Maximum values of each axis by modifying the .Minimum and .Maximum values of the axes like
Chart1.ChartAreas(0).AxisX.Minimum = 100 'Example value
Chart1.ChartAreas(0).AxisX.Minimum = 200 'Example value
As discussed in the comments to your question you can either use your method #2 (add datapoint and delete datapoint index 0 and then select the new minimum and maximum X-Value from the series with LinQ:
Chart1.ChartAreas(0).AxisX.Minimum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Min
Chart1.ChartAreas(0).AxisX.Maximum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Max
or you can, as you have now done, just set the minimum value to a X-value some points before the last point.