Greetings All,
i need to convert a functin from vb.net to C#.net here is the vb.net function, when i convert the function using converters i am getting few problems. my main problem is ReDim and ReDim Preserve. these function are not available in C#, how can i convert to C# accordingly. can any body pls provide me a solution. thanks in advance
rCnt = 0
nCols = oReader.FieldCount()
If nStart = 1 And nRows = -1 Thennbegin = 0
bAll =
True ElseIf nStart > 0 And nRows = -1 Thennbegin = nStart - 1
bAll =
True ElseIf (nStart > 0 And nRows > 0) Thennbegin = nStart - 1
nEnd = nbegin + nRows - 1
End If Dim vaArray As New ArrayList While (oReader.Read()) If Not bExit Then If nbegin <= rCnt And (nEnd >= rCnt Or bAll = True) Then Try Dim values(nCols - 1) As ObjectoReader.GetValues(values)
vaArray.Add(values)
Catch ex As ExceptionHandleError(ex, C_CLASS_NAME)
End Try ElseIf ((bAll = False) And (nEnd < rCnt)) ThenbExit =
True End If End IfrCnt = rCnt + 1
End While If Not IsArray(vaResultSet) Then ReDim vaResultSet(0) Else ReDim Preserve vaResultSet(UBound(vaResultSet) + 1) End If Dim vaOut(1)vaOut(0) = vaArray
vaOut(1) = rCnt
vaResultSet(UBound(vaResultSet)) = vaOut
Loop While oReader.NextResult() Catch ex As ExceptionHandleError(ex, C_CLASS_NAME)
End Try Return vaResultSet End Function
Regards
sai

vb.net to c#.net
Marcin Obel
Most converters will have a problem with conversion of ReDim if you do not declare the variable you are redimming as an array. After changing vaResultSet to an array of objects (which I think your code implies), the C# equivalent from Instant C# is:
protected object GetRowsList(ref SqlDataReader oReader, long nStart)
{
return GetRowsList(ref oReader, nStart, -1);
}
protected object GetRowsList(ref SqlDataReader oReader)
{
return GetRowsList(ref oReader, 1, -1);
}
//INSTANT C# NOTE: C# does not support optional parameters. Overloaded method(s) are created above.
//ORIGINAL LINE: Protected Function GetRowsList(ByRef oReader As SqlDataReader, Optional ByVal nStart As Long = 1, Optional ByVal nRows As Long = -1)
protected object GetRowsList(ref SqlDataReader oReader, long nStart, long nRows)
{
int nCols = 0;
long rCnt = 0;
long nbegin = 0;
long nEnd = 0;
long nCol = 0;
object[] vaResultSet = null;
bool bAll = false;
bool bExit = false;
try
{
do
{
rCnt = 0;
nCols = oReader.FieldCount();
if (nStart == 1 & nRows == -1)
{
nbegin = 0;
bAll = true;
}
else if (nStart > 0 & nRows == -1)
{
nbegin = nStart - 1;
bAll = true;
}
else if (nStart > 0 & nRows > 0)
{
nbegin = nStart - 1;
nEnd = nbegin + nRows - 1;
}
ArrayList vaArray = new ArrayList();
while (oReader.Read())
{
if (! bExit)
{
if (nbegin <= rCnt & (nEnd >= rCnt | bAll == true))
{
try
{
object[] values = new object[nCols];
oReader.GetValues(values);
vaArray.Add(values);
}
catch (Exception ex)
{
HandleError(ex, C_CLASS_NAME);
}
}
else if ((bAll == false) & (nEnd < rCnt))
bExit = true;
}
rCnt = rCnt + 1;
}
if (! (Microsoft.VisualBasic.Information.IsArray(vaResultSet)))
vaResultSet = new object[1];
else
{
//INSTANT C# NOTE: The following 4 lines reproduce what 'ReDim Preserve' does behind the scenes in VB.NET:
//ORIGINAL LINE: ReDim Preserve vaResultSet(UBound(vaResultSet) + 1)
object[] tempReDim1 = new object[vaResultSet.GetUpperBound(0) + 2];
if (vaResultSet != null)
System.Array.Copy(vaResultSet, tempReDim1, System.Math.Min(vaResultSet.Length, tempReDim1.Length));
vaResultSet = tempReDim1;
}
object[] vaOut = new object[2];
vaOut[0] = vaArray;
vaOut[1] = rCnt;
vaResultSet[vaResultSet.GetUpperBound(0)] = vaOut;
} while (oReader.NextResult());
}
catch (Exception ex)
{
HandleError(ex, C_CLASS_NAME);
}
return vaResultSet;
}
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# converter
Instant VB: C# to VB.NET converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB.NET to J# converter
Clear VB: Cleans up VB.NET code
Kade
Here is the syntatical equivalent. I verified it compiles but not that it behaves like you want. I made a change to your ReDim code. Redimensioning an array is inefficient so I switched to an ArrayList instead. Your function didn't specify a return type so I used object. In general you should return the appropriate type. Since you are returning an ArrayList in this case you should change the return type. Of course you can also have the ArrayList generate a normal array and return that instead. Also note that C# doesn't support default arguments and therefore you have to specify several overrides in order for it to work.
protected
object GetRowsList ( SqlDataReader oReader ){
return GetRowsList(oReader, 1, -1); } protected object GetRowsList ( SqlDataReader oReader, long nStart ){
return GetRowsList(oReader, nStart, -1); } protected object GetRowsList(SqlDataReader oReader, long nStart, long nRows){
int nCols; long rCnt = 0, nbegin = 0, nEnd = 0;ArrayList vaResultSet =
new ArrayList(); bool bAll = false, bExit = false; try{
do{
rCnt =
0;nCols = oReader.FieldCount;
if ((nStart == 1) && (nRows == -1)){
nbegin =
0;bAll =
true;}
else if ((nStart > 0) && (nRows == -1)){
nbegin = nStart -
1;bAll =
true;}
else if ((nStart > 0) && (nRows > 0)){
nbegin = nStart -
1;nEnd = nbegin + nRows -
1;};
ArrayList vaArray =
new ArrayList(); while (oReader.Read()){
if (!bExit){
if ((nbegin <= rCnt) && ((nEnd >= rCnt) || bAll)){
try{
object[] values = new object[nCols - 1];oReader.GetValues(values);
vaArray.Add(values);
}
catch (Exception ex){
HandleError(ex, C_CLASS_NAME);
};
}
else if (!bAll && (nEnd < rCnt)){
bExit =
true;};
};
rCnt = ++rCnt;
};
object[] vaOut = new object[] { vaArray, rCnt };vaResultSet.Add(vaOut);
}
while (oReader.NextResult());}
catch (Exception ex){
HandleError(ex, C_CLASS_NAME);
};
return vaResultSet;}
Michael Taylor - 1/12/06
JensLundberg
Greetings David,
Thanks for your posting, its really helped me alot.
Thanks once again
Regards
sailu
Zakary
You can convert between .NET languages in a variety of ways. To do so programmatically you can load the source file into a CodeCompileUnit of the CodeDOM using the appropriate parser (VBCodeProvider) and then use a different generator to generate the new source code (CSharpCodeProvider). You can skip the language determination entirely in 2.0 using the CodeDomProvider methods to determine the contents from the file extension.
If this is a one time thing or you don't want to deal with the CodeDOM then you can compile the code into an assembly and then use Reflector to disassemble the code in C# which you can then copy and paste.
Michael Taylor - 1/12/06
ZhangQing
Search google for: VB.NET to C#
There are web site that can do this online: http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
Miketrix
Greetings Micheal,
Thanks for your help. its really helped me alot.
Thanks once again
regards
sailu