Results 1 to 10 of 10

Thread: [RESOLVED] [2.0] Code

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] Code

    Hi all

    this code in the vb.net I want in the C#


    vb Code:
    1. Dim Restrictions() As String = New String(3) {}
    2.             Restrictions(3) = "Table"

    Guide me
    Thanks

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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:
    1. Public Function GetTableName() As DataTable
    2.         Dim UserTables As DataTable = Nothing
    3.         Try
    4.  
    5.             Dim Restrictions() As String = New String(3) {}
    6.             Restrictions(3) = "Table"
    7.             Dim NewConnection As OleDbConnection = OpenNewConnection() 'Open the Oledb Connection
    8.             UserTables = NewConnection.GetSchema("Tables", Restrictions)
    9.             NewConnection.Close()
    10.         Catch ex As Exception
    11.             Throw New Exception("Error Occuring For Getting The Table Name", ex)
    12.         End Try
    13.         Return UserTables
    14.     End Function

    But in the c# code this is not giving me the result.

    c# Code:
    1. public DataTable GettableName()
    2.         {
    3.             try
    4.             {
    5.                 DataTable DataTable = new DataTable();
    6.                 string[] Restrictions = new string[3];
    7.                 Restrictions[2] = "Table";
    8.                 OleDbConnection OleDbConnection = new OleDbConnection();
    9.                 OleDbConnection = OpenNewConnection();
    10.                 DataTable = OleDbConnection.GetSchema("Tables", Restrictions);
    11.                 OleDbConnection.Close();
    12.                 return DataTable;
    13.             }
    14.             catch (Exception ex)
    15.             {
    16.                 throw (ex);
    17.             }
    18.         }

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Code

    Quote 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";

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2.0] Code

    Quote 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

  7. #7

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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:
    1. string[] Restrictions = new string[9];
    2.                          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:
    1. Dim Restrictions() As String = New String(3) {}

    But in c# Array size is fix i.e.3

    C# Code:
    1. string[] Restrictions = new string[3];

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    vb Code:
    1. Dim myArray() As String
    This does the same in C#:
    Code:
    string[] myArray;
    This declares a variable, creates an array object with 4 elements and assigns it to the variable:
    vb Code:
    1. Dim myArray(3) As String
    and so does this:
    vb Code:
    1. 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#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Code

    Thanks all this code work for me.

    C# Code:
    1. public DataTable GettableName()
    2.         {
    3.             try
    4.             {
    5.                 DataTable DataTable = new DataTable();
    6.                 string[] Restrictions = new string[4];
    7.                 Restrictions[3] = "Table";
    8.                 OleDbConnection OleDbConnection = new OleDbConnection();
    9.                 OleDbConnection = OpenNewConnection();
    10.                 DataTable = OleDbConnection.GetSchema("Tables", Restrictions);
    11.                 OleDbConnection.Close();
    12.                 return DataTable;
    13.             }
    14.             catch (Exception ex)
    15.             {
    16.                 throw (ex);
    17.             }
    18.         }

    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
  •  



Click Here to Expand Forum to Full Width