Friday 25 May 2007

Check For Leap Year in VB.Net

When dealing with dates in VB.NET, it's a good idea not to perform manual checks or calculations, which may be inaccurate depending on the quality of the code. Instead, it's advisable to rely on the functionality of classes provided by .NET.

For instance, if you need to determine if a given year is a leap year, you can use VB.NET's IsLeapYear function. Here's an example of how you can use the function:

Private Sub LeapYearCheck()
Dim bLeapYear AsBoolean
bLeapYear = Date.IsLeapYear(Now.Year)
MessageBox.Show(bLeapYear)
bLeapYear = Date.IsLeapYear(2004)
MessageBox.Show(bLeapYear)
End Sub


In the example, we define a Boolean variable, bLeapYear, to hold the result of whether a given year is a leap year. we then set the value of bLeapYear to the IsLeapYear property of the Date class and pass to it the current year, which we obtain using the Year property of the Now class. We show the value of bLeapYear in a MessageBox. The result is False because 2007 is not a leap year. After that,we follow the same steps for 2004, which is a leap year. In that case, MessageBox shows True.


PHP,Java, DHTML, C, C++ and many more languages, for help visit
Programming Fourm

Methods To Recurse Through An Array

I was wondering in how many ways can we iterate through an array in PHP. So, I figured out a few, here's it...

Our Array, which we will be iterating on,

$arr = array('PHP','Perl', 'JavaScript','AJAX', 'Python','ASP', 'C#');


1. Using a simple for loop


// using for loop
for($i=0;$i<count($arr);$i++)
{
print("$arr[$i]\n");
}

2. Using foreach

// using foreach
foreach($arr as $val)
{
print("$val\n");
}


3. Using a while loop


// using while loop
$i=0;
while($val=$arr[$i++])
{
print("$val\n");
}


4. Using the array_walk function


// using array_walk function
function print_item($item,$key)
{
print("$item\n");
}

array_walk($arr,'print_item');


5. Using an user function

// using a function, and recursively calling it
function print_recurse(&$a)
{
printf("%s\n",array_pop($a));
// check whether array has any elements, or else it'll become an infinite recursion
if(count($a)>0)
print_recurse($a);
}

print_recurse($arr);

Wednesday 28 February 2007

Using shift operator for faster division and multiplication

Multiplications

12 * 2 = 12 << 1
12 * 4 = 12 << 2
12 * 8 = 12 << 3
12 * 16 = 12 << 4
12 * 32 = 12 << 5
12 * 64 = 12 << 6
12 * 128 = 12 << 7
12 * 256 = 12 << 8

Divisions

12 / 2 = 12 >> 1
12 / 4 = 12 >> 2
12 / 8 = 12 >> 3
12 / 16 = 12 >> 4
12 / 32 = 12 >> 5
12 / 64 = 12 >> 6
12 / 128 = 12 >> 7
12 / 256 = 12 >> 8

Example


<?php
$a = 12 << 1;
print $a;
?>



Community discussing C and its derivatives like Win32, C++, MFC, C# tutorials - www.cfanatic.com

Sending files to browser in JSP

Sending files to browser in JSP

<%
// fetch the file
String filename = "companySecret.txt";
String filepath = "C:\\";

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + '"');

java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);

int i;
while ((i=fileInputStream.read()) != -1)
{
out.write(i);
}

fileInputStream.close();
out.close();
%>



Community discussing C and its derivatives like Win32, C++, MFC, C# tutorials - www.cfanatic.com

Hello World!

Like all programming langues where we write a HelloWorld program when we start programming in that language, here's the HelloWorld to this Web Application Blog where you'll find lots of sample code snippets, tips n tricks for many poupular web development languages like PHP,Perl,Python,ASP,ASP.Net,Ruby,JSP,etc.