VB.net syntax for C# code - Chris

21-Mar-07 05:42:36
Not sure if this is OT

I periodically, once every week or so, get an error when trying to read an
XML doc stored in cache. I get an 'object not set to a reference' I have a
gut feeling it happens when the app is under strain and the moment between
testing the cache and returning the cached xml doc it expires. I have read
several other people have the same problem. I have seen some C# code which
theoretically makes sense to me, but I don't understand the syntax and how
you would write it in VB.net. And I thought I had got me head round

Dim cacheitem as object = Cache(key) as XMLDocument

but with no luck. Here is the C# code in full.


Object cacheItem = Cache[key] as DataTable;
if(cacheItem == null)
{
cacheItem = GetData();
Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1),
TimeSpan.Zero);
}
return (DataTable)cacheItem;

Regards, Chris.
reply
 
 

VB.net syntax for C# code - DavidAnto

21-Mar-07 07:46:00
(via Instant VB):

Dim cacheItem As Object = TryCast(Cache(key), DataTable)
If cacheItem Is Nothing Then
cacheItem = GetData()
Cache.Insert(key, cacheItem, Nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)
End If
Return CType(cacheItem, DataTable)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
reply
 
Inserting NewLine (line break) into resource string
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class