|
-
Apr 17th, 2007, 06:59 AM
#1
Thread Starter
Just Married
[RESOLVED] [2.0] Code
Hi all
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
-
Apr 17th, 2007, 07:14 AM
#2
Re: [2.0] Code
Code:
string[] Restrictions = new string[3];
Restrictions[2] = "Table";
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Apr 17th, 2007, 07:25 AM
#3
Thread Starter
Just Married
Re: [2.0] Code
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);
}
}
-
Apr 17th, 2007, 07:41 AM
#4
Re: [2.0] Code
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 this
Code:
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;
}
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Apr 17th, 2007, 07:50 AM
#5
Re: [2.0] Code
 Originally Posted by Shuja Ali
Code:
string[] Restrictions = new string[3];
Restrictions[2] = "Table";
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.
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.
The code should be:
Code:
string[] Restrictions = new string[4];
Restrictions[3] = "Table";
-
Apr 17th, 2007, 08:05 AM
#6
Re: [2.0] Code
 Originally Posted by penagate
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.
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.
ps: As you can see English is not my first language
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Apr 17th, 2007, 08:24 AM
#7
Thread Starter
Just Married
Re: [2.0] Code
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];
-
Apr 17th, 2007, 09:04 AM
#8
Re: [2.0] Code
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
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Apr 17th, 2007, 06:06 PM
#9
Re: [2.0] Code
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#:This declares a variable, creates an array object with 4 elements and assigns it to the variable:and so does this:
vb Code:
Dim myArray() As String = New String(3) {}
This does the same in C#:
Code:
string[] myArray = new string[4];
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#.
-
Apr 17th, 2007, 11:41 PM
#10
Thread Starter
Just Married
Re: [2.0] Code
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);
}
}
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
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|