-
A little Class Help
Hi I am trying to learn/understand classes, so I built a small app with just a button and a text field to test.
Here is my main code file
Code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Class_Test
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
}
void Button1Click(object sender, EventArgs e)
{
Connection test = new Connection();
label1.Text = test.dbPath;
}
}
}
Here is my simple class file
Code:
using System;
using System.Text;
namespace Class_Test
{
class Connection
{
public string dbPath()
{
string Path = "DATASOURCE";
return Path;
}
}
}
I am having issues just trying to pass a simple string to a text label.
getting an error stating
"can not convert method group (dbPath) to non-delegate type string.
Thanks for the help
-
Re: A little Class Help
Since you are just learning C#, might I suggest that you take a few moments to learning the C# naming conventions? Understand that you can write your code in whatever style you wish, but if you follow standard C# naming convensions, other folks will be able to immediately read your code. It also makes your code look more professional, imo.
That being said, here are a couple of C# naming conventions:
There are kind of two naming styles that are used. One is called camelCased and the other is called PascalCased. The main difference is whether the leading character is capitalized or not. Another character that is used is a '_' which is used for class fields to disambiguate them (as we will see).
Here's a little table for C# types and naming style
Code:
Type C# Style Example
Class PascalCased public class MyClass { }
Method PascalCased public void MyMethod( )
Method Parameters camelCased public void MyMethod( string param1 )
Ctor Parameters camelCased public MyClass( string param1 )
Local variable camelCased string name = String.Empty;
private class field camelCased private int age;
_camelCased private int _age; (preferred)
protected class field PascalCased protected Name;
public class field PascalCased public NameLast;
Properties PascalCased public string NameFirst { get; set; }
Class constants PascalCased public const int MaxLength = 300;
Disambiguating ctor/method parameters from class fields. Since both ctor/method parameters and private class fields are named with camel casing, a method must be used to disabiguate them.
Problem:
Code:
class MyClass
{
private string name;
public MyClass( string name )
{
name = name;
}
}
The above code with get you a compiler error (or warning). The problem is that the ctor parameter 'name' takes precidence over the class field and the compiler simply assigns the value of the parameter to itself (as if it was written as (param)name = (param)name; ).
To disabiguate, 'this.' is often used:
Code:
class MyClass
{
private string name;
public MyClass( string name )
{
this.name = name;
}
}
Another way to disambiguate is to prepend the class field with an underscore:
Code:
class MyClass
{
private string _name;
public MyClass( string name )
{
_name = name;
}
}
This has the added advantage of less typing and the developer can easily spot a class field in the code (because only class fields would have prepended underscores).
If you follow these conventions, your code will be easy to read by other (experienced) folks.
Sometimes beginners mix up the conventions (or use conventions from other styles). This makes it a bit of a pain to read and is often the cause bugs. Example - using Pascal casing or '_' at the wrong time.
Remember method parameters are camelCased, what if we did the following?
Code:
public void MyMethod( string _name, int Age )
{
...
}
_name now implies it's a class field and 'Age' looks like a class property rather than method parameters.
The bottom line is if you follow the standard, be sure to get it right and get it right everywhere (i.e. be consistent).
With that background, the problem with your code is you've named the dbPath method with camelCasing (which isn't necessarily a bug), but called it as a property (you missed including the '()). Instead of a method, you probably should use a property here.
Let's rewrite the code to follow the naming conventions I've outlined above.
Code:
class Connection
{
// DbPath property
public string DbPath { get { return "DATASOURCE"; } }
}
Code:
void Button1Click(object sender, EventArgs e)
{
Connection test = new Connection();
label1.Text = test.DbPath;
}