2011-09-30

Assing an array as object property in Visual Basic 2010

I am not yet completely sure about how correctly this is, but it took a lot of time for me figuring out how to assign an array as a property.
I found out that firstly I had to define an array variable inside a class

Private temperatureArray() As Double

Then I have to make a property, which returns element of the array


    Public ReadOnly Property temperatureVal(ByVal index As Integer) As Double
        Get
            Return temperatureArray(index)
        End Get
    End Property


And finally I have to assign the values to an array inside a function or sub


    Sub getTemp()
        ReDim tempVal(UBound(valuesArray)) 'valuesArray is some predefined array.
        For i = 0 To UBound(valuesArray)
            temperatureVal(i) = valuesArray(i)
        Next i
    End Sub

Simple textfield validating in Visual basic 2010

Simple way how to validate text fields.
If you have text field and you are using "_textChanged do something", then you probably know that it could be not enough. Depending on different specifications (usually such input is for numbers) and writing bad data like characters where should be numbers, it can make errors.
From my little experience, VB and similar program code is working based on "on error do something else".
In the following example I`m doing two things.
In the first I`m validating each keypress allowing just numbers, comma and decimal seperator,


Private Sub step1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles step1.KeyPress
        If (Not Char.IsNumber(e.KeyChar) AndAlso Not ".,-".Contains(e.KeyChar) AndAlso Not e.KeyChar = Microsoft.VisualBasic.Chr(Keys.Back)) Then
            e.Handled = True
        End If
    End Sub

And in the second - catching empty-string error. If someone deletes all content in the textbox, text is changed and passed string is empty, which does no good.


    Private Sub step1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles step1.TextChanged
        On Error GoTo 11
        If Not (step1.Text = String.Empty) Then
            parametrs.setStep() 'this pass error
        End If
11:
    End Sub

2011-09-17

First/last day of the month

2011.09.17.
Is the given date the first day of the month? Or maybe the last? Whatta silly question... of course not the first or last one! But sometimes it is useful to know or.. better let`s say, return "true" on that kind of questions.
I found that it could be done very easily with creating object from such string, returning parts, making new date value and done some comparing.
Ok, here are the steps-
$dateObj= new DateTime($datestring);
$currentDay=(int) $dateObj->format('d'); //make int not string. let`s get necessary date parts
$currentMonth=(int) $firstDate->format('m');
$currentYear=(int) $firstDate->format('y');

//let`s create another month value returning from the new date, where it is created from the day //before current date
$prevMonth = (int) date("m", mktime(0, 0, 0, $currentMonth, $currentDay-1, $currentYear));


//and finally - comparison
if ($currentMonth>$prevMonth)
{
return "true";  //if previous date is in the previous month, we have the first day of the month
//from date object parts you can make new date here based on your needs through mktime.
}
else
{
return "false";
}

and now - if we need to know is the date is the last day in the month.
$nextMonth = (int) date("m", mktime(0, 0, 0, $currentMonth, $currentDay+1, $currentYear));
if ($currentMonth<$nextMonth)

See the changes?

Avoiding injections

Very fast and useful way, how to avoid different injections in websites - if you know what type of variable can be.
For example - if you have any ID`s or other values which can be only integers - in the script you can always be sure that after such validation it will always be an integer just writing (int) before the variable - like -
$variable = (int) $_GET['variable'];
 Another example - if you have variable which could always be date - you could pass it as a string and build a date object from it
$date=$_GET['date'];
$dateObj = new DateTime($date);
//if you need to get back to string in any case
$dateString = $date->format("Y-m-d") //or other format
Dealing with the float type it is very similar like integer type. But it is always necessary to remember - what is the decimal seperator of the host and decimal seperator in your country. In my case, I have to remember, that the host`s decimal seperator is dot, but usually people in Latvia are using comma. So, I have to parse the given "float string", and replace comma with dot, if there`s any.

$value=(float)str_replace(",",".",$value);

Linear interpolation. Extended version.

I already wrote about the linear interpolation and related problems which were successfully solved. However, full convenience requires some monthly statistical capacities. Usually the data series, including interpolated ones, does not start and end with first and last day of the month, respectively and thus the first thing is to cut off the first and last incomplete month measurements. And only then it is possible to split the data series by month. Splitting occures in the loop splitting the corresponding date and getting the month and comparing it with the previously splitted month value. If the measurement is in the same month, the new two dimensional array is created with the date (year and month) in the first dimension, and (measured including interpolated) value in the second. And if the current month value differs from the detected month value in previous cycle, it is time to change the first - date dimension value to the next one. After creating such array, it is time to make some statistics for each of inner measurement array.
You can try the linear interpolator here and see the source code (not yet OOP, but i hope soon) - here.