bugnuker
asked on
need help converting vb.net code to c#.net code
hello.
I am willing to shell out 500 points for the person who can convert this VB.NET 1.1 code to C#.NET 2.0 code that will work.
Here is the code:
-------------------------- ---------- ---------- --
'************************* ********** ********** ********** ********
' ConstantsManager
' Description: Manages the constants that live in the
' applicationConstants.confi g file. These get stored
' in memory and when the file changes, the file is
' removed from memory and re-read back into memory
'
' NOTE: Constants file must live in the webroot
' on the web server for the web application to run.
'
' Author: Rachael Schoenbaum
' Create date: 12/4/2002
'************************* ********** ********** ********** ********
Imports System, System.Web.Caching, System.Xml, Microsoft.VisualBasic
Public Class ConstantsManager
' Cache object that will be used to store and retrieve items from
' the cache and constants used within this object
Protected Friend Shared myCache as System.Web.Caching.Cache = _
System.Web.HttpRuntime.Cac he()
Private Shared applicationConstantsFile As String = "ApplicationConstantsFile"
Public Shared applicationConstantsFileNa me As String = _
String.Replace(System.AppD omain.Curr entDomain. BaseDirect ory, "/", "\") & _
"applicationConstants.conf ig"
Private Shared xmlFile As New XmlDocument()
Private Shared constantIdentifier As String = "constant"
Private shared constantKey As String = "cacheDependencyKey"
Private Shared thisVersion As Integer = getConstant("THIS_VERSION" )
Public Shared Function getConstant(ByRef key As String) As Object
Dim tmpObj As Object
If Not (myCache(constantIdentifie r & key) Is Nothing) Then
tmpObj = CType(myCache(constantIden tifier & key), Object)
Else
tmpObj = pullConstantFromFile(key)
'Create the cache dependencies and insert the object into the cache
If Not IsNothing(tmpObj) Then
If myCache(constantKey) Is Nothing Then
myCache.Insert(constantKey , now)
End If
myCache.Insert(constantIde ntifier & key, tmpObj, _
New CacheDependency(applicatio nConstants FileName))
End If
End If
Return tmpObj
End Function
Private Shared Function pullConstantFromFile(ByRef key As String) As Object
Dim obj As Object = 0
If myCache(applicationConstan tsFile) Is Nothing Then
PopulateCache()
End If
'Attempt to find the element given the "key" for that tag
Dim elementList As XmlNodeList = xmlFile.GetElementsByTagNa me(key)
'If the key is found, the element list will have a count greater than
'zero and we retrieve the value of the tag...
If elementList.Count > 0 Then
Dim node As XmlNode
'If there is only 1 element in the list, then the element doesn't
'have a version number and can be returned as is.
If elementList.Count = 1 Then
node = elementList.Item(0)
'If there is more than 1 element in the list, find the element for
'the version of the application
Else
node = xmlFile.SelectSingleNode(" descendant ::" & key & _
"[@version=" & thisVersion & "]")
End If
'Retrieve the value behind the node that matched the key/version
If Not (node Is Nothing) Then
obj = node.InnerText()
End If
'If the value is a numeric, convert it to a number; otherwise
'convert it to a string (we don't store values other than strings
'and numbers).
If IsNumeric(obj) Then
obj = CType(obj,Integer)
Else
obj = CType(obj, String)
End If
End If
Return obj
End Function
Private Shared Sub PopulateCache()
'With a try around the entire event, the object attempts to load the XML
'file and store it in the cache with a dependency on the XML file itself.
'This means that any time the XML file changes, it is removed from the
'cache. When the "getConstant" method is called again, the XML file won't
'exist in memory and the PopulateCache will be re-called.
Try
xmlFile.Load(applicationCo nstantsFil eName)
myCache.Insert(application ConstantsF ile, xmlFile, _
New CacheDependency(applicatio nConstants FileName))
Catch e As Exception
System.Diagnostics.Debug.W riteLine(" Error: " & e.Message)
End Try
End Sub
End Class
-------------------------- ---------- ---------- ---------- ---------- -
thanks
I am willing to shell out 500 points for the person who can convert this VB.NET 1.1 code to C#.NET 2.0 code that will work.
Here is the code:
--------------------------
'*************************
' ConstantsManager
' Description: Manages the constants that live in the
' applicationConstants.confi
' in memory and when the file changes, the file is
' removed from memory and re-read back into memory
'
' NOTE: Constants file must live in the webroot
' on the web server for the web application to run.
'
' Author: Rachael Schoenbaum
' Create date: 12/4/2002
'*************************
Imports System, System.Web.Caching, System.Xml, Microsoft.VisualBasic
Public Class ConstantsManager
' Cache object that will be used to store and retrieve items from
' the cache and constants used within this object
Protected Friend Shared myCache as System.Web.Caching.Cache = _
System.Web.HttpRuntime.Cac
Private Shared applicationConstantsFile As String = "ApplicationConstantsFile"
Public Shared applicationConstantsFileNa
String.Replace(System.AppD
"applicationConstants.conf
Private Shared xmlFile As New XmlDocument()
Private Shared constantIdentifier As String = "constant"
Private shared constantKey As String = "cacheDependencyKey"
Private Shared thisVersion As Integer = getConstant("THIS_VERSION"
Public Shared Function getConstant(ByRef key As String) As Object
Dim tmpObj As Object
If Not (myCache(constantIdentifie
tmpObj = CType(myCache(constantIden
Else
tmpObj = pullConstantFromFile(key)
'Create the cache dependencies and insert the object into the cache
If Not IsNothing(tmpObj) Then
If myCache(constantKey) Is Nothing Then
myCache.Insert(constantKey
End If
myCache.Insert(constantIde
New CacheDependency(applicatio
End If
End If
Return tmpObj
End Function
Private Shared Function pullConstantFromFile(ByRef
Dim obj As Object = 0
If myCache(applicationConstan
PopulateCache()
End If
'Attempt to find the element given the "key" for that tag
Dim elementList As XmlNodeList = xmlFile.GetElementsByTagNa
'If the key is found, the element list will have a count greater than
'zero and we retrieve the value of the tag...
If elementList.Count > 0 Then
Dim node As XmlNode
'If there is only 1 element in the list, then the element doesn't
'have a version number and can be returned as is.
If elementList.Count = 1 Then
node = elementList.Item(0)
'If there is more than 1 element in the list, find the element for
'the version of the application
Else
node = xmlFile.SelectSingleNode("
"[@version=" & thisVersion & "]")
End If
'Retrieve the value behind the node that matched the key/version
If Not (node Is Nothing) Then
obj = node.InnerText()
End If
'If the value is a numeric, convert it to a number; otherwise
'convert it to a string (we don't store values other than strings
'and numbers).
If IsNumeric(obj) Then
obj = CType(obj,Integer)
Else
obj = CType(obj, String)
End If
End If
Return obj
End Function
Private Shared Sub PopulateCache()
'With a try around the entire event, the object attempts to load the XML
'file and store it in the cache with a dependency on the XML file itself.
'This means that any time the XML file changes, it is removed from the
'cache. When the "getConstant" method is called again, the XML file won't
'exist in memory and the PopulateCache will be re-called.
Try
xmlFile.Load(applicationCo
myCache.Insert(application
New CacheDependency(applicatio
Catch e As Exception
System.Diagnostics.Debug.W
End Try
End Sub
End Class
--------------------------
thanks
ASKER
This is almost working.
I am getting errors on somelines.
i'll paste the errors here maybe you can help
or i can send the .cs file (but if you've done the conversion, you should also have VS and can make the class to see if it will build.)
--------errors------------ -----
Error 1 The best overloaded method match for 'ConstantsManager.pullCons tantFromFi le(ref string)' has some invalid arguments C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 42 22 C:\...\UconnDemo\
Error 2 Argument '1' must be passed with the 'ref' keyword C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 42 43 C:\...\UconnDemo\
Error 3 The name 'now' does not exist in the current context C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 46 49 C:\...\UconnDemo\
Error 4 Cannot apply indexing with [] to an expression of type 'method group' C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 68 24 C:\...\UconnDemo\
Error 5 'System.Xml.XmlNode.InnerT ext' is a 'property' but is used like a 'method' C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 79 28 C:\...\UconnDemo\
Error 6 The name 'IsNumeric' does not exist in the current context C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 84 17 C:\...\UconnDemo\
Error 7 'System.Web.HttpRuntime.Ca che' is a 'property' but is used like a 'method' C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 22 89 C:\...\UconnDemo\
Error 8 The best overloaded method match for 'ConstantsManager.getConst ant(ref string)' has some invalid arguments C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 34 38 C:\...\UconnDemo\
Error 9 Argument '1' must be passed with the 'ref' keyword C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 34 50 C:\...\UconnDemo\
Error 10 Syntax error, value expected C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap p_Code\Con tantsManag er.cs 22 95 C:\...\UconnDemo\
-----end errors---------
thanks for the help so far!
-shane
I am getting errors on somelines.
i'll paste the errors here maybe you can help
or i can send the .cs file (but if you've done the conversion, you should also have VS and can make the class to see if it will build.)
--------errors------------
Error 1 The best overloaded method match for 'ConstantsManager.pullCons
Error 2 Argument '1' must be passed with the 'ref' keyword C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
Error 3 The name 'now' does not exist in the current context C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
Error 4 Cannot apply indexing with [] to an expression of type 'method group' C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
Error 5 'System.Xml.XmlNode.InnerT
Error 6 The name 'IsNumeric' does not exist in the current context C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
Error 7 'System.Web.HttpRuntime.Ca
Error 8 The best overloaded method match for 'ConstantsManager.getConst
Error 9 Argument '1' must be passed with the 'ref' keyword C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
Error 10 Syntax error, value expected C:\Documents and Settings\support\My Documents\Visual Studio 2005\WebSites\UconnDemo\Ap
-----end errors---------
thanks for the help so far!
-shane
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bill-
This works in VS2005 expect for Information.IsNothing and Information.IsNumeric
do not work
I am new to C# and I dont know of another way for the Information.IsNumeric.
I am sure for the Information.IsNothing I can do a = null
i'll look on google to try and find the soultion, unless you post first with it.
as soon as i get it working from either googling the answer, or your post, i'll award the 500 pts.
thanks for all your help today.
turly and expert.
i tried conversion tools, i tried re-writing on my own, but i am not that good a c# for that.
-shane
This works in VS2005 expect for Information.IsNothing and Information.IsNumeric
do not work
I am new to C# and I dont know of another way for the Information.IsNumeric.
I am sure for the Information.IsNothing I can do a = null
i'll look on google to try and find the soultion, unless you post first with it.
as soon as i get it working from either googling the answer, or your post, i'll award the 500 pts.
thanks for all your help today.
turly and expert.
i tried conversion tools, i tried re-writing on my own, but i am not that good a c# for that.
-shane
For the IsNumeric and IsNothing functions to work, add Microsoft.VisualBasic.dll to your references
* In Solution Explorer, expand the project node you want to add a reference to.
* Right-click the References node for the project and select Add Reference from the shortcut menu.
* To add a reference to a component or components, do the following:
1. In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
2. In the top pane, select the component you want to reference, and then click the Select button.
The component you referenced appears in the SelectedComponents pane of the dialog box.
Cheers,
-Bill
* In Solution Explorer, expand the project node you want to add a reference to.
* Right-click the References node for the project and select Add Reference from the shortcut menu.
* To add a reference to a component or components, do the following:
1. In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
2. In the top pane, select the component you want to reference, and then click the Select button.
The component you referenced appears in the SelectedComponents pane of the dialog box.
Cheers,
-Bill
That worked for you I gather? Thanks for the points
Cheers,
-Bill
Cheers,
-Bill
// ConstantsManager
// Description: Manages the constants that live in the
// applicationConstants.confi
// in memory and when the file changes, the file is
// removed from memory and re-read back into memory
//
// NOTE: Constants file must live in the webroot
// on the web server for the web application to run.
//
// Author: Rachael Schoenbaum
// Create date: 12/4/2002
// **************************
using System;
,System.Web.Caching;
,System.Xml;
,Microsoft.VisualBasic;
public class ConstantsManager {
// Cache object that will be used to store and retrieve items from
// the cache and constants used within this object
protected internal static System.Web.Caching.Cache myCache = System.Web.HttpRuntime.Cac
private static string applicationConstantsFile = "ApplicationConstantsFile"
public static string applicationConstantsFileNa
private static XmlDocument xmlFile = new XmlDocument();
private static string constantIdentifier = "constant";
private static string constantKey = "cacheDependencyKey";
private static int thisVersion = getConstant("THIS_VERSION"
public static object getConstant(ref string key) {
object tmpObj;
if (!(myCache[(constantIdenti
tmpObj = myCache[(constantIdentifie
}
else {
tmpObj = pullConstantFromFile(key);
// Create the cache dependencies and insert the object into the cache
if (!(tmpObj == null)) {
if ((myCache[constantKey] == null)) {
myCache.Insert(constantKey
}
myCache.Insert((constantId
}
}
return tmpObj;
}
private static object pullConstantFromFile(ref string key) {
object obj = 0;
if ((myCache[applicationConst
PopulateCache();
}
// Attempt to find the element given the "key" for that tag
XmlNodeList elementList = xmlFile.GetElementsByTagNa
// If the key is found, the element list will have a count greater than
// zero and we retrieve the value of the tag...
if ((elementList.Count > 0)) {
XmlNode node;
// If there is only 1 element in the list, then the element doesn't
// have a version number and can be returned as is.
if ((elementList.Count == 1)) {
node = elementList.Item[0];
// If there is more than 1 element in the list, find the element for
// the version of the application
}
else {
node = xmlFile.SelectSingleNode((
+ (key + ("[@version="
+ (thisVersion + "]")))));
}
// Retrieve the value behind the node that matched the key/version
if (!(node == null)) {
obj = node.InnerText();
}
// If the value is a numeric, convert it to a number; otherwise
// convert it to a string (we don't store values other than strings
// and numbers).
if (IsNumeric(obj)) {
obj = ((int)(obj));
}
else {
obj = ((string)(obj));
}
}
return obj;
}
private static void PopulateCache() {
// With a try around the entire event, the object attempts to load the XML
// file and store it in the cache with a dependency on the XML file itself.
// This means that any time the XML file changes, it is removed from the
// cache. When the "getConstant" method is called again, the XML file won't
// exist in memory and the PopulateCache will be re-called.
try {
xmlFile.Load(applicationCo
myCache.Insert(application
}
catch (Exception e) {
System.Diagnostics.Debug.W
}
}
}