Powered By Blogger

Monday, 14 March 2011

ReDim Statement (from msdn)

ReDim
Used at procedure level to reallocate storage space for an array variable.

ReDim [ Preserve ] name(boundlist)

Parts
Preserve
Optional. Keyword used to preserve the data in the existing array when you change the size of only the last dimension.
name
Required. Name of the variable. Must be a valid Visual Basic identifier. You can redimension as many variables as you like in the same statement, specifying the name and boundlist parts for each one. Multiple variables are separated by commas.
boundlist
Required. List of non-negative integers representing the upper bounds of the dimensions of the redefined array. Multiple upper bounds are separated by commas. The number of dimensions in boundlist must match the original rank of the array.
Each value in boundlist specifies the upper bound of a dimension, not the length. The lower bound is always zero, so the subscript for each dimension can vary from zero through the upper bound.

It is possible to use -1 to declare the upper bound of an array dimension. This signifies that the array is empty but not Nothing, a distinction required by certain common language runtime functions. However, Visual Basic code cannot successfully access such an array. If you attempt to do so, an IndexOutOfRangeException error occurs during execution.

Remarks
  • The ReDim statement can appear only at procedure level. This means you can redefine arrays inside a procedure but not at class or module level.
  • The ReDim statement is used to change the size of one or more dimensions of an array that has already been formally declared. ReDim cannot change the rank (the number of dimensions) of the array.
  • The ReDim statement cannot change the data type of an array variable or provide new initialization values for the array elements.
  • ReDim releases the existing array and creates a new array with the same rank. The elements of the new array are initialized to the default value for their data type unless you specify Preserve.

If you include the Preserve keyword, Visual Basic copies the elements from the existing array to the new array. When you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same size it already has in the existing array.

For example, if your array has only one dimension, you can resize that dimension and still preserve the contents of the array, because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve.

The following example increases the size of the last dimension of a dynamic array without losing any existing data in the array, and then decreases the size with partial data loss:

Dim IntArray(10, 10, 10) As Integer
' ...
ReDim Preserve IntArray(10, 10, 20)
' ...
ReDim Preserve IntArray(10, 10, 15)

The first ReDim creates a new array, copying all the elements from the existing array. It also adds 10 more columns to the end of every row in every layer. The elements in these new columns are initialized to the default value of the element type of the array.

The second ReDim creates another new array, copying all the elements that fit. However, five columns are lost from the end of every row in every layer. This is not a problem if you have finished using these columns. Reducing the size of a large array can free up memory that you no longer need.

You can use ReDim on a property that holds an array of values.

Example
This example uses the ReDim statement to allocate and reallocate storage space for array variables.

Dim I, MyArray() As Integer ' Declare variable and array variable.
ReDim MyArray(5) ' Allocate 6 elements.
For I = 0 To UBound(MyArray)
MyArray(I) = I ' Initialize array.
Next I
The next statement resizes the array without saving the contents of the elements.


ReDim MyArray(10) ' Resize to 11 elements.
For I = 0 To UBound(MyArray)
MyArray(I) = I ' Initialize array.
Next I

The following statement resizes the array but saves the contents of the elements.

ReDim Preserve MyArray(15) ' Resize to 16 elements.

Thursday, 10 March 2011

Get Geographical Location Geolocation by using JQUERY

Today I came across this post called “Get Geographical Location Geolocation by using JQUERY”. The API returns the geographical location of the queried IP address with some additional information such as:

{
'status':'ok',
'IP': '74.125.45.100',
'CountryCode': 'US',
'CountryName': 'United States',
'RegionName': 'California',
'ZipPostalCode': '94043',
'City': 'Mountain View',
'Latitude': '37.4192',
'Longitude': '-122.057'
}

// In case of an error
{
'status':'parent server not responding'
}

Update: the URL has been changed!

The JSON geolocation querying API’s address is:

http://iplocationtools.com/ip_query.php?output=json&ip=80.80.214.93  The URL above is dead, instead use this one: http://www.geoplugin.net/json.gp?jsoncallback=?

And the great thing is, you can identify your website visitor’s IP and Geo location by simply querying the API without any parameters like this:

http://iplocationtools.com/ip_query.php?output=json

Knowing your users’ IP and/or location, you might add a behavior to your website that is specific to some location. For example, offering some advertising to US only visitors, or popup with special offer to European users.

Anyway, here is a sample jQuery code to query the API:

// Build the URL to query
var url = "http://iplocationtools.com/ip_query.php?output=json&callback=?&ip=";

// Utilize the JSONP API
$
.getJSON(url, function(data){
if(data['status'] == 'ok'){
// Do something with the data
$
('#profile #ip')
.append(data['IP']);
$
('#profile #country')
.append(data['CountryName']);
}
});

Here we are not specifying any IP address in the url variable that is why it is getting current user’s data


Wednesday, 9 March 2011

Remove duplicates Items in array.

IN VB.NET

Public Function RemoveDuplicateItems(ByVal itemList As String()) As String()
Dim tempArray As ArrayList = New ArrayList
For i As Integer = 0 To itemlist.Count - 1
If Not tempArray.Contains(itemList.GetValue(i)) Then
tempArray.Add(itemList.GetValue(i))
End If
Next
Return tempArray.ToArray(GetType(String))
End Function

Dim List As String() = {"Nokia", "Samsung", "LG", "Motorola", "Nokia", "Samsung"}
RemoveDuplicateItems(List)

MOST POPULAR WEBSITES ON THE INTERNET. - 2011

This summary is not available. Please click here to view the post.

Tuesday, 8 March 2011

Software Developers References: Passing lists to SQL Server 2005 with XML Paramete...

Software Developers References: Passing lists to SQL Server 2005 with XML Paramete...: "OverviewSQL Server 2005's XML capabilities make it a easier to pass lists to SQL Server procedures. BackgroundI recently needed to write a..."

Passing lists to SQL Server 2005 with XML Parameters

Overview

SQL Server 2005's XML capabilities make it a easier to pass lists to SQL Server procedures.

Background

I recently needed to write a stored procedure which took a list of ID's as a parameter. That's one of those things that seems like it would be really simple, but isn't. You'd think you could just pass in a comma delimited string of id's: @ids = '3,5,7,8' and use something like 'SELECT * FROM Products WHERE ID IN (@ids)'. Nope, it doesn't work. I still remember my surprise when I ran into that six or seven years ago.
There are a huge variety of workarounds for this issue - see Erland's comprehensive list ranging form SQL Server 6.5 to 2000. I've used several of these, and while they worked I never liked them. Probably the best method is to just use a SPLIT table valued function which splits your string and returns a table. It's clean, but all of your procedures depend on the existence of that function.
It was also possible to use OPENXML in SQL Server 2000. The syntax was obviously put together by C++ programmers (you have to prepare a document and work with an integer handle, which feels a lot like a pointer), and there were some limitations to be aware of, but it pretty much worked.
This time around, I decided to try this with SQL Server 2005's XML capabilities and see if it was any easier. It is.

Getting started with SQL Server 2005's XML Syntax

XML variables in SQL Server 2005 make it easy to "shred" XML strings into relational data. The main new methods you'll need to use are value() and nodes() which allow us to select values from XML documents.

DECLARE @productIds xmlSET @productIds ='<Products><id>3</id><id>6</id><id>15</id></Products>' SELECT ParamValues.ID.value('.','VARCHAR(20)')FROM @productIds.nodes('/Products/id') as ParamValues(ID)

Which gives us the following three rows:
3
6
15

Alright, just show me how to pass a list in a procedure parameter already!

Here's a proc which takes a single XML parameter. We first declare a table variable (@Products) and load the XML values into it. Once that's done, we can join against the @Products table as if it were any other table in the database.
CREATE PROCEDURE SelectByIdList(@productIds xml) AS DECLARE @Products TABLE (ID int) INSERT INTO @Products (ID) SELECT ParamValues.ID.value('.','VARCHAR(20)')FROM @productIds.nodes('/Products/id') as ParamValues(ID) SELECT * FROM ProductsINNER JOIN @Products pON Products.ProductID = p.ID
Now we can call it as follows:

EXEC SelectByIdList @productIds='<Products><id>3</id><id>6</id><id>15</id></Products>'

Which gives us the following:
ProductID ProductName SupplierID CategoryID QuantityPerUnit UnitPrice UnitsInStock UnitsOnOrder ReorderLevel Discontinued ID
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10 13 100 25 0 3
6 Grandma's Boysenberry Spread 3 2 12 - 8 oz jars 25 120 0 25 0 6
15 Genen Shouyu 6 2 24 - 250 ml bottles 15.5 39 0 5 0 15
In order to use this, you'll need to an XML string with your ID's. In our application, Steve was handling the application code, and I talked him into doing this via quick and dirty string concatenation. His method worked great:

public static string BuildXmlString(string xmlRootName, string[] values) { StringBuilder xmlString = new StringBuilder(); xmlString.AppendFormat("<{0}>", xmlRootName); for (int i = 0; i < values.Length; i++) { xmlString.AppendFormat("<value>{0}</value>", values[i]); } xmlString.AppendFormat("</{0}>", xmlRootName); return xmlString.ToString(); }

What's next?

This is a very simple use of XML in SQL Server. You can pass complex XML documents containing business objects to insert and update in your relational tables, for instance. If you're going to do that with a large amount of data, have a look at Ayende's clever use of SqlBulkCopy to handle that more efficiently.

Software Developers References: China Unicom readies mobile OS

Software Developers References: China Unicom readies mobile OS: "Squaring up to Symbian, asking Apple outsideBy Bill Ray • Get more from this authorPosted in Mobile, 1st March 2011 12:45 GMTFree w..."