|
-
Mar 14th, 2003, 12:31 PM
#1
Thread Starter
Sleep mode
Equivalent of C# ?
In VB.NET I use this
VB Code:
Public Shared Sub OpenDB()
If Not MyConnection.State = ConnectionState.Open Then
MyConnection.Open()
Else
msgbox ("Connection already open ")
End If
End Sub
What's C# equivalent ?
Thanx
-
Mar 14th, 2003, 12:33 PM
#2
Thread Starter
Sleep mode
I stuck only in the (if not ) clause ?? I know everything else !
-
Mar 14th, 2003, 12:34 PM
#3
PowerPoster
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");
}
}
-
Mar 14th, 2003, 12:36 PM
#4
Thread Starter
Sleep mode
Thanx for this fast response !
-
Mar 14th, 2003, 03:01 PM
#5
Thread Starter
Sleep mode
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
-
Mar 14th, 2003, 03:01 PM
#6
PowerPoster
No problem, it worked for what you wanted?
-
Mar 14th, 2003, 03:11 PM
#7
Thread Starter
Sleep mode
never mind , it's null . Next time I should have a look at C# Keywords .
-
Mar 14th, 2003, 03:19 PM
#8
Thread Starter
Sleep mode
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 !
-
Mar 14th, 2003, 03:34 PM
#9
Lively Member
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
-
Mar 14th, 2003, 03:38 PM
#10
Thread Starter
Sleep mode
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 !
-
Mar 14th, 2003, 04:01 PM
#11
PowerPoster
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.
-
Mar 14th, 2003, 04:11 PM
#12
Thread Starter
Sleep mode
-
Mar 14th, 2003, 04:34 PM
#13
Member
You can use the 'using' directive:
-
Mar 14th, 2003, 04:42 PM
#14
Thread Starter
Sleep mode
Originally posted by SimonVega
You can use the 'using' directive:
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 .
-
Mar 14th, 2003, 05:11 PM
#15
Member
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!
-
Mar 15th, 2003, 01:46 AM
#16
Thread Starter
Sleep mode
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 :
I believe it can be done by using Keyword but I couldn't find it there ?
-
Mar 15th, 2003, 02:35 AM
#17
Lively Member
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.
-
Mar 15th, 2003, 02:43 AM
#18
Member
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.
-
Mar 15th, 2003, 02:51 AM
#19
Thread Starter
Sleep mode
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 !
-
Mar 15th, 2003, 02:56 AM
#20
Thread Starter
Sleep mode
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 ?
-
Mar 15th, 2003, 03:14 AM
#21
Member
You have to specify the type in the foreach construct:
Code:
foreach(DataRow r in t.Rows)
Console.WriteLine(r[0]);
-
Mar 15th, 2003, 03:16 AM
#22
Member
Code:
using System.Windows.Forms.Design
System is namespace but others are classes ???
'System.Windows.Forms.Design' is a nested namespace.
-
Mar 15th, 2003, 03:38 AM
#23
Thread Starter
Sleep mode
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
Code:
TreView.Nodes.Add (new TreeNode(dr()("TABLE_NAME")));
Thank you for trying to solve my problem !
-
Mar 15th, 2003, 11:20 AM
#24
Member
This is what you were supposed to change:
Code:
foreach (DataRow dr in Tables.Rows) {
TreView.Nodes.Add (new TreeNode(dr("TABLE_NAME")));
}
-
Mar 15th, 2003, 12:14 PM
#25
Thread Starter
Sleep mode
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 ??
-
Mar 15th, 2003, 01:06 PM
#26
Member
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.
-
Mar 15th, 2003, 01:13 PM
#27
Thread Starter
Sleep mode
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 ??
-
Mar 15th, 2003, 01:45 PM
#28
Member
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()));
}
-
Mar 15th, 2003, 01:56 PM
#29
Thread Starter
Sleep mode
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
-
Mar 15th, 2003, 02:08 PM
#30
Member
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()));
}
-
Mar 16th, 2003, 02:24 PM
#31
Thread Starter
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|