.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: How To Convert An ArrayList To A String Array

Here’s a quick helper post for those looking to convert an ArrayList into a String array. I ran into this problem because I code in VB.NET and the C# code is different.

In VB:

'if you have an ArrayList named data
 Dim str() As String = data.ToArray(GetType(String))

In C#:

//if you have an ArrayList named “data”
string[] strings = (string[])data.ToArray(typeof(string));Hope this helps!

December 19, 2007 Posted by | ASP.NET, C#, Tips & Tricks, VB.NET | 10 Comments

ASP.NET: How To Name A Variable Or Property Using .NET’s “Reserved” Words

Let’s say you want to give your class a boolean property named Error that gets turned on or off if something goes awry in your code. You would think that you can’t do this because the word “Error” is reserved by .NET. However, there is a way. All you have to do is put square brackets around the word! For instance:

Public Property [Error]() As Boolean

Easy, no?

December 13, 2007 Posted by | ASP.NET, Tips & Tricks | 2 Comments

Javascript: jQuery. Interesting.

I do not know Javascript at all. I know, I know, you say, “How can you consider yourself a real developer and not know any Javascript?” Well, I don’t not know Javascript; I can work within it. I just couldn’t create a document using it. This is probably due to the paradigm of “Javascript is evil” being pounded into my head during my nascent developer years.

Regardless, I would at some point like to learn. For those who are like me and don’t know any Javascript, maybe this is the place to start: jQuery. While it is in no way close to actual Javascript syntax, it is somewhat similar to .NET syntax, which should probably help reduce the learning curve a bit. Basically it’s a library that enables you to circumvent common Javascript pitfalls while providing you with the ability to code more while actually writing less. It has some built in functions and abilities, and appears as though it will simplify the application of true AJAX.

Sound interesting? I thought so.

December 11, 2007 Posted by | AJAX, Javascript, Tips & Tricks | | 1 Comment

ASP.NET: Issues with Generics

The introduction of Generics into .NET 2.0 was a huge improvement over the previous methods of aggregating objects. There are many, many new things you can do with Generics that could not be done previously, in addition to making harder tasks easier and more efficient. For example, I can take a Generics list of my own objects and bind them to a GridView and be able to use the objects’ properties as bound datafields! Something not possible before. However, they do, of course, come with problems of their own. For instance, for the novice to intermediate user, the concept of predicates and actions is enough to make your (my) head spin. And it seems like this is the most efficient way to find things, even though to me, it makes no sense. I suppose until I decide to really hunker down and attempt to learn how to use these features, I’ll stick with my For Each [whatever] in MyGenericsList ... Next code.

Here is where I come across my problem, though. One of my business objects has a Generics list of another object (AllPrices as List(Of Price)) consisting of prices and options. This is bound to a GridView, which displays all the properties. On update, I have all the properties of the selected Price object update, some numbers are automatically determined by various variables and business calculations, and then the object is matched up back to its appropriate item in my AllPrices Generics collection, and the GridView is rebound. This works wonderfully for all but one situation, which has me vexed and confused. Here’s how I match the item and recalibrate my AllPrices Generics list (in my RowUpdating event):

' "newPrice" contains all my price changes. it is of type PriceInfo.Price
Dim updatedprice As New PriceInfo.Price
		For Each updatedprice In Prod.Prices.AllPrices
			If updatedprice.PriceID = newPrice.PriceID Then
				updatedprice = newPrice
				updatedprice.UpdatePrice() 'updates the database
				Prod.Prices.AllPrices(Prod.Prices.AllPrices.IndexOf(updatedprice)) = updatedprice 'update AllPrices ****
				Exit For
			End If
		GridView1.EditIndex = -1
		BindGridView() 'rebinds the AllPrices Generics list to the GridView

What this code does is finds the matching object in the list by PriceID, updates it with the changes (updatedprice = newPrice), updates the database, and then re-inserts the object into the AllPrices Generics list. However, in one situation, I get an “out of index” error at the line with four asterisks (****), even though the database updates correctly. Logically, this doesn’t make sense to me, since 1) it is finding the object in the collection (it can’t get to that line unless it does), 2) it is updating the database correctly (I’ve verified this in my database), it just isn’t updating the AllPrices list!

What the crap?

Any help is much appreciated. Also, if you’ve experienced similar issues with Generics, please post them! Or if you have some normal, realistic way to use actions and predicates! 🙂

Status: Solved! (12/11/07)
Solution: I was making things way too complex, apparently. I was running two loops (the For loop and the IndexOf() loop) and trying to match objects to one another. Rather than include all this ridiculous complexity, I created a simple counter variable (Dim i As Integer) outside the For loop and just incremented it after the conditional statement runs (i += 1), and replaced Prod.Prices.AllPrices(Prod.Prices.AllPrices.IndexOf(updatedprice)) = updatedprice with Prod.Prices.AllPrices(i) = updatedprice. Now everything is gravy. Thanks to Nick Berardi for the tips!

December 11, 2007 Posted by | ASP.NET, Errors, Generics, GridView | Leave a comment