Hi all :wave:
this code in the vb.net I want in the C#
vb Code:
Dim Restrictions() As String = New String(3) {} Restrictions(3) = "Table"
Guide me
Thanks
Printable View
Hi all :wave:
this code in the vb.net I want in the C#
vb Code:
Dim Restrictions() As String = New String(3) {} Restrictions(3) = "Table"
Guide me
Thanks
Array Indexes in C# are different that VB.NET. The number of elements in the above code is 3 but the array index starts from 0. So if you wish to assign the 3rd item then you will use 2 and not 3.Code:string[] Restrictions = new string[3];
Restrictions[2] = "Table";
Thanks but this not giving me the result.
see the whole VB.NET code return the table name form the ACCESS Database.
vb Code:
Public Function GetTableName() As DataTable Dim UserTables As DataTable = Nothing Try Dim Restrictions() As String = New String(3) {} Restrictions(3) = "Table" Dim NewConnection As OleDbConnection = OpenNewConnection() 'Open the Oledb Connection UserTables = NewConnection.GetSchema("Tables", Restrictions) NewConnection.Close() Catch ex As Exception Throw New Exception("Error Occuring For Getting The Table Name", ex) End Try Return UserTables End Function
But in the c# code this is not giving me the result.
c# Code:
public DataTable GettableName() { try { DataTable DataTable = new DataTable(); string[] Restrictions = new string[3]; Restrictions[2] = "Table"; OleDbConnection OleDbConnection = new OleDbConnection(); OleDbConnection = OpenNewConnection(); DataTable = OleDbConnection.GetSchema("Tables", Restrictions); OleDbConnection.Close(); return DataTable; } catch (Exception ex) { throw (ex); } }
Shakti:
You should compare the code that you have written in VB.NET with the code written in C# and check what is wrong.
Try thisCode:public DataTable GetTableName() {
DataTable UserTables = null;
try
{
string[] Restrictions = new string[3];
Restrictions[2] = "Table";
OleDbConnection NewConnection = OpenNewConnection();
UserTables = NewConnection.GetSchema("Tables", Restrictions);
NewConnection.Close();
}
catch (Exception ex){
throw new Exception("Error Occuring For Getting The Table Name", ex);
}
return UserTables;
}
Array indexing behaves the same way in both VB.NET and C#. 3 is the fourth item, not the third. However, the declaration is different.Quote:
Originally Posted by Shuja Ali
The code should be:
Code:string[] Restrictions = new string[4];
Restrictions[3] = "Table";
My bad, I did not put it in proper words. What I should have written is this. When you declare an array like this in VB.NET [tt] Dim MyArray(3) As String[tt], it means that the array will contain 4 elements and same kind of code in C# means that you are creating an array of 3 elements. So if you want to create an array of 4 elements in C#, you need to declare it like how it is shown in post #5.Quote:
Originally Posted by penagate
ps: As you can see English is not my first language ;)
Thanks
But main problem in VB.NET array size is not fix as you can see the code, but in C# all you fixing the size of the array (3) so the code not working fine and not returning me the table from the database.
But if I do like this (assume there is 4 table in the database)
vb Code:
string[] Restrictions = new string[9]; Restrictions[2] = "Table";
then it show me the result.
But problem is we do not know that how many table located in the database so we can not fix the size of the array.
What I do in that condition
This is in the VB.NET array size is not fix
vb Code:
Dim Restrictions() As String = New String(3) {}
But in c# Array size is fix i.e.3
C# Code:
string[] Restrictions = new string[3];
GetShcema method has got nothing to do with the fixed array size. You just specify an Array of Size 4 and use it to get the Restrictions
I'm afraid you're confused shakti. This declares an array variable but does not create an array object, so the variable is Nothing:This does the same in C#:vb Code:
Dim myArray() As StringThis declares a variable, creates an array object with 4 elements and assigns it to the variable:Code:string[] myArray;
and so does this:vb Code:
Dim myArray(3) As StringThis does the same in C#:vb Code:
Dim myArray() As String = New String(3) {}As soon as you specify a size then you have created an array object with a fixed size, whether it's in VB or C#. If you don't specify a size then you haven't created an array object, because you cannot create an array object without specifying the size. That goes for VB and C#.Code:string[] myArray = new string[4];
Thanks all this code work for me.
C# Code:
public DataTable GettableName() { try { DataTable DataTable = new DataTable(); string[] Restrictions = new string[4]; Restrictions[3] = "Table"; OleDbConnection OleDbConnection = new OleDbConnection(); OleDbConnection = OpenNewConnection(); DataTable = OleDbConnection.GetSchema("Tables", Restrictions); OleDbConnection.Close(); return DataTable; } catch (Exception ex) { throw (ex); } }
ThanksQuote:
GetShcema method has got nothing to do with the fixed array size. You just specify an Array of Size 4 and use it to get the Restrictions