Results 1 to 31 of 31

Thread: Equivalent of C# ?

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Equivalent of C# ?

    In VB.NET I use this

    VB Code:
    1. Public Shared Sub OpenDB()
    2. If Not MyConnection.State = ConnectionState.Open Then
    3.  MyConnection.Open()
    4. Else
    5. msgbox ("Connection already open ")
    6. End If
    7. End Sub
    What's C# equivalent ?
    Thanx

  2. #2

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I stuck only in the (if not ) clause ?? I know everything else !

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    This is off the top of my head, and very well have some problems with it...
    Code:
    public static void OpenDB()
    {
       if(!(MyConnection.State == ConnectionState.Open))
       {
          MyConnection.Open();
       }
       else
       {
          MessageBox.Show("Connection already open");
       }
    }

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanx for this fast response !

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    again what's C# equivalent for (nothing) keyword . I couldn't figure it out !
    VB Code:
    1. Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})

    thanx

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    No problem, it worked for what you wanted?

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    never mind , it's null . Next time I should have a look at C# Keywords .

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith
    No problem, it worked for what you wanted?
    yeah worked fine . btw , how can import class1 data members ?
    in vb.net we do this :

    import Class1.Myclass.Myfunction

    thanx !

  9. #9
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    I'm just a newbie but I thought it was the using keyword

    Code:
    using Namespace.Class blah blah blah
    Mabe that'll help a little bit

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Cagez
    I'm just a newbie but I thought it was the using keyword

    Code:
    using Namespace.Class blah blah blah
    Mabe that'll help a little bit
    Nope !

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Not sure exactly what you need. I would have said the using statement myself, but it seems like you are doing something different. Give some more VB code that shows how your using it so I can understand better what your asking.

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Pirate
    yeah worked fine . btw , how can import class1 data members ?
    in vb.net we do this :

    import Class1.Myclass.Myfunction
    thanx !
    I thought the post explain itself
    How can I shorten a function path . for example

    I can do this , so I don't have to continue typing the whole path to IO members

    Import System.IO.

    Path.method (instead of doing this System.IO.Path.method

    Clear ??

  13. #13
    Member
    Join Date
    Mar 2003
    Posts
    34
    You can use the 'using' directive:

    Code:
    using System.IO;
    AKA 'Lethal'

  14. #14

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by SimonVega
    You can use the 'using' directive:
    Code:
    using System.IO;
    IO class was only example . I need to import (using in C#) classes and voids from Class1 . What should I do ?

    I've been trying this but with no luck

    I want to get this :
    using Class1.Mydatabase.OpenDB

    come on guys this is easy ! This is my first day with C# ! It was the worst .

  15. #15
    Member
    Join Date
    Mar 2003
    Posts
    34
    I think I know where your stuck. When using static members, you have to reference the member using the class name:

    Code:
       public class HelloWorld
       {
            public static void DisplayMsg()
            {
                 Console.Write("Hello World");
            }  
    
            static void Main()
            {
                //Note: You can not use an object reference
                HelloWorld.DisplayMsg();
            }   
        }
    Welcome to the dark side!
    AKA 'Lethal'

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I should explained that well ! I do this everyday in VB.NET . I don't know if I can do this in C# ??
    I have a namespace called "Mydatabase" and one big class called "DBClass" and many functions under "DBClass" class .
    all of them are static (shared) . I call them in form1 like so :

    Code:
    Mydatabase.DBClass.OpenDB();
    how can I shorten the its name to be called like this :

    Code:
    OpenDB();
    I believe it can be done by using Keyword but I couldn't find it there ?

  17. #17
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Sorry if I'm becoming repetitive, but have you tried this?
    Code:
    using Mydatabase.DBClass;
    It should go at the very top of your form code, where the other using statements are.

  18. #18
    Member
    Join Date
    Mar 2003
    Posts
    34
    I already covered this a few threads up. There is no such thing as global functions in C#. To reference a static member of a class, you have to use the class name following by the dot notation...

    It should go at the very top of your form code, where the other using statements are.
    You can only apply the using namespace directive against namespaces, not classes.
    AKA 'Lethal'

  19. #19

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Cagez , yes I've tried that but it seem SimonVega is right . It's only applied to namespaces ! but how come this happens :
    Code:
    using System.Windows.Forms.Design
    System is namespace but others are classes ???
    C# is confusing to me at least . VB.NET can do such stuff easily !

  20. #20

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Class1.cs(73): Type and identifier are both required in a foreach statement

    what's wrong with this code ??
    Code:
    public static void LoadTables (System.Windows.Forms.TreeView TreView)
    			{
    				OpenDB.OpenDB1();
    				DataTable Tables =MyConnection().GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null,null,null,"TABLE"});
    				CloseDB.CloseDB1 ();
    
    				if (Tables.Rows.Count > 0)
    				{
    					TreView.BeginUpdate ();
    				}
    					DataRow dr;
    				foreach (dr in Tables.Rows)
    				{
    					TreView.Nodes.Add (new  TreeNode(dr("TABLE_NAME")));
    				}
    				Tables = null;
    			}
    it shows blue line on "in" word . Why ?

  21. #21
    Member
    Join Date
    Mar 2003
    Posts
    34
    You have to specify the type in the foreach construct:

    Code:
    foreach(DataRow r in t.Rows)
        Console.WriteLine(r[0]);
    AKA 'Lethal'

  22. #22
    Member
    Join Date
    Mar 2003
    Posts
    34
    Code:
    using System.Windows.Forms.Design
    System is namespace but others are classes ???
    'System.Windows.Forms.Design' is a nested namespace.
    AKA 'Lethal'

  23. #23

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is VB version
    VB Code:
    1. 'TreeView
    2.             Public Overloads Shared Sub LoadTables(ByVal TreView As TreeView)
    3.                 'see if connection is already open
    4.                 OpenDB.OpenDB()
    5.                 Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
    6.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    7.  
    8.                 CloseDB.CloseDB()
    9.  
    10.                 '"TABLE_CATALOG" is the database name
    11.                 '"TABLE_NAME" is the table name
    12.                 If Tables.Rows.Count > 0 Then
    13.                     TreView.BeginUpdate()
    14.                     Dim dr As DataRow
    15.                     For Each dr In Tables.Rows
    16.                                                 TreView.Nodes.Add(New TreeNode(dr("TABLE_NAME")))
    17. Next
    18. TreView.EndUpdate()
    19. End If
    20. Tables = Nothing
    21. End Sub

    I've tried your way SimonVega but gave me error :
    Class1.cs(77): 'dr' denotes a 'variable' where a 'method' was expected
    in this line
    Code:
    TreView.Nodes.Add (new TreeNode(dr()("TABLE_NAME")));
    Thank you for trying to solve my problem !

  24. #24
    Member
    Join Date
    Mar 2003
    Posts
    34
    This is what you were supposed to change:

    Code:
    foreach (DataRow dr in Tables.Rows) {
        TreView.Nodes.Add (new  TreeNode(dr("TABLE_NAME")));
    }
    AKA 'Lethal'

  25. #25

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    No , that didn't help ! Still getting that error !

    Class1.cs(77): 'dr' denotes a 'variable' where a 'method' was expected
    in this line

    Code:
    foreach (DataRow dr in Tables.Rows) {
        TreView.Nodes.Add (new  TreeNode(dr("TABLE_NAME")));
    }
    any help guys ??

  26. #26
    Member
    Join Date
    Mar 2003
    Posts
    34
    There is also another syntax error in your code:

    Code:
    foreach (DataRow dr in Tables.Rows) {
        TreView.Nodes.Add (new  TreeNode(dr["TABLE_NAME"]));
    }
    You need to reference the indexer using squar brackets.
    AKA 'Lethal'

  27. #27

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Sorry didn't work either . I got these errors when I tried your code !

    1- Class1.cs(76): The best overloaded method match for 'System.Windows.Forms.TreeNode.TreeNode(System.Windows.Forms.TreeView)' has some invalid arguments

    2-Class1.cs(76): Argument '1': cannot convert from 'object' to 'System.Windows.Forms.TreeView'

    . any suggestions plz ??

  28. #28
    Member
    Join Date
    Mar 2003
    Posts
    34
    Ok...I was merely checking your syntax, not actually running the code. I'm not at my dev box, but here is why i think it is not compiling. When you read the column data out from the data row, use the tostring() method to convert it to a string.

    Code:
    foreach (DataRow dr in Tables.Rows) {
        TreView.Nodes.Add (new TreeNode(dr["TABLE_NAME"].ToString()));
    }
    AKA 'Lethal'

  29. #29

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I got a lot of errors . I believe it's not conversion problem ! it has to do with "in" till the end . Dunno really I though that was simple .
    Code:
    public static void LoadTables (System.Windows.Forms.TreeView TreView)
    			{
    				OpenDB.OpenDB1();
    				DataTable Tables =MyConnection().GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null,null,null,"TABLE"});
    				CloseDB.CloseDB1 ();
    
    				if (Tables.Rows.Count > 0)
    				{
    					TreView.BeginUpdate ();
    				}
    				DataRow dr;
    				
    				foreach (dr in Tables.Rows()) 
    				{
    					TreView.Nodes.Add (new  TreeNode(dr("TABLE_NAME")));
    				}
          Tables = null;
    }
    I appreciate your help SimonVega .
    Thank you

  30. #30
    Member
    Join Date
    Mar 2003
    Posts
    34
    Here is a sample I just wrote up that works and your code should somewhat resemble:

    Code:
    private void button1_Click(object sender, System.EventArgs e) {
        DataTable t = new DataTable();
        DataRow r = t.NewRow();
        t.Columns.Add("ID", typeof(System.Int32));
        r[0] = 10;
        t.Rows.Add(r);
    
        TreeNode root = treeView1.Nodes.Add("Root");
        foreach(DataRow row in t.Rows)
            root.Nodes.Add(new TreeNode(row["ID"].ToString()));	
    }
    AKA 'Lethal'

  31. #31

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Didn't help either ! It gives me a different error , seems simpler but I coudn't solve it though !

    Class1.cs(78): 'dr' denotes a 'variable' where a 'method' was expected

    Code:
    foreach (DataRow dr in Tables.Rows) 
    {
    root = TreView.Nodes.Add("Root");	
    TreView.Nodes.Add(new TreeNode(dr("TABLE_NAME")));
    //nor this 
    //TreView.Nodes.Add(new TreeNode(dr()("TABLE_NAME")));
    }
    Thanx dude !

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