In VB.NET I use this
What's C# equivalent ?VB Code:
Public Shared Sub OpenDB() If Not MyConnection.State = ConnectionState.Open Then MyConnection.Open() Else msgbox ("Connection already open ") End If End Sub
Thanx
Printable View
In VB.NET I use this
What's C# equivalent ?VB Code:
Public Shared Sub OpenDB() If Not MyConnection.State = ConnectionState.Open Then MyConnection.Open() Else msgbox ("Connection already open ") End If End Sub
Thanx
I stuck only in the (if not ) clause ?? I know everything else !
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");
}
}
Thanx for this fast response !:D
again what's C# equivalent for (nothing) keyword . I couldn't figure it out !
VB Code:
Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
thanx
No problem, it worked for what you wanted?
never mind , it's null . Next time I should have a look at C# Keywords . :D
yeah worked fine . :D btw , how can import class1 data members ?Quote:
Originally posted by hellswraith
No problem, it worked for what you wanted?
in vb.net we do this :
import Class1.Myclass.Myfunction
thanx !
I'm just a newbie but I thought it was the using keyword
Mabe that'll help a little bit :cool:Code:using Namespace.Class blah blah blah
Nope !Quote:
Originally posted by Cagez
I'm just a newbie but I thought it was the using keyword
Mabe that'll help a little bit :cool:Code:using Namespace.Class blah blah blah
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.
I thought the post explain itself :rolleyes:Quote:
Originally posted by Pirate
yeah worked fine . :D btw , how can import class1 data members ?
in vb.net we do this :
import Class1.Myclass.Myfunction
thanx !
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 ?? :rolleyes:
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 ?Quote:
Originally posted by SimonVega
You can use the 'using' directive:
Code:using System.IO;
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 .
I think I know where your stuck. When using static members, you have to reference the member using the class name:
Welcome to the dark side! :DCode:public class HelloWorld
{
public static void DisplayMsg()
{
Console.Write("Hello World");
}
static void Main()
{
//Note: You can not use an object reference
HelloWorld.DisplayMsg();
}
}
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 :
how can I shorten the its name to be called like this :Code:Mydatabase.DBClass.OpenDB();
I believe it can be done by using Keyword but I couldn't find it there ?Code:OpenDB();
Sorry if I'm becoming repetitive, but have you tried this?
It should go at the very top of your form code, where the other using statements are.Code:using Mydatabase.DBClass;
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...
You can only apply the using namespace directive against namespaces, not classes.Quote:
It should go at the very top of your form code, where the other using statements are.
Cagez , yes I've tried that but it seem SimonVega is right . It's only applied to namespaces ! but how come this happens :
System is namespace but others are classes ???Code:using System.Windows.Forms.Design
C# is confusing to me at least . VB.NET can do such stuff easily !:D
Class1.cs(73): Type and identifier are both required in a foreach statement
what's wrong with this code ??
it shows blue line on "in" word . Why ?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;
}
You have to specify the type in the foreach construct:
Code:foreach(DataRow r in t.Rows)
Console.WriteLine(r[0]);
Code:using System.Windows.Forms.Design
'System.Windows.Forms.Design' is a nested namespace.Quote:
System is namespace but others are classes ???
This is VB version
VB Code:
'TreeView Public Overloads Shared Sub LoadTables(ByVal TreView As TreeView) 'see if connection is already open OpenDB.OpenDB() Dim Tables As DataTable = MyConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _ New Object() {Nothing, Nothing, Nothing, "TABLE"}) CloseDB.CloseDB() '"TABLE_CATALOG" is the database name '"TABLE_NAME" is the table name If Tables.Rows.Count > 0 Then TreView.BeginUpdate() Dim dr As DataRow For Each dr In Tables.Rows TreView.Nodes.Add(New TreeNode(dr("TABLE_NAME"))) Next TreView.EndUpdate() End If Tables = Nothing 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
Thank you for trying to solve my problem !Code:TreView.Nodes.Add (new TreeNode(dr()("TABLE_NAME")));
This is what you were supposed to change:
Code:foreach (DataRow dr in Tables.Rows) {
TreView.Nodes.Add (new TreeNode(dr("TABLE_NAME")));
}
No , that didn't help ! Still getting that error !
Class1.cs(77): 'dr' denotes a 'variable' where a 'method' was expected
in this line
any help guys ??Code:foreach (DataRow dr in Tables.Rows) {
TreView.Nodes.Add (new TreeNode(dr("TABLE_NAME")));
}
There is also another syntax error in your code:
You need to reference the indexer using squar brackets.Code:foreach (DataRow dr in Tables.Rows) {
TreView.Nodes.Add (new TreeNode(dr["TABLE_NAME"]));
}
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'
:rolleyes: . any suggestions plz ??
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()));
}
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 .
I appreciate your help SimonVega .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;
}
Thank you
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()));
}
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
Thanx dude !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")));
}