|
It may take a while to get used to classes and object orientation. This[^] might help. I would also suggest you buy and study an introductory book on the language of your choice; C# books tend to explain OO concepts to a reasonable degree.
|
|
|
|
|
Classes are basically a collection of functions and properties..
A class can be used to run a number of actions such as DisplayData or UpdateDatabase, but in some cases they can be used to represent an object such as a car in your example.
Although the class does not save the data to disk, it does allow for easy management of a lot of data related to a car. And makes things like passing data to other functions much easier to manage.
Its all just part of good design
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
OK, I think it might be becoming clearer. So I could mock my datatable fields into a class, then hide all of my SQL functions of updating, inserting, etc. in the class. Basically, my code for my form could look like:
<br />
dtclass dt = new dtclass(value1, value2);<br />
dt.update;<br />
|
|
|
|
|
yep, pretty much. thou if your working with SQL then I suggest you take a look at Linq[^].
Visual Studio has a great tool for creating the classes for you Linq to SQL[^]
Just to exemplify Linq.. you can end up with code as simple as this...
MyDatabase DB = new MyDatabase();
Car car = new Car();
car.Make = "Ford";
DB.Cars.InsertOnSubmit(car);
DB.SubmitChanges();
updating a record like so...
MyDatabase DB = new MyDatabase();
Car car = Db.Cars.Single(c => c.ID == 1);
car.Make = "Citreon";
DB.SubmitChanges();
BTW, Linq is not just for SQL it can work on many collections of data. Very powerful tool
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Yes, yes you can. But does it make sense to do so in your particular application?
In my opinion (and it may be rather unpopular with the Linq-fanboys), you should only do that if you have the data hanging around for a while and interacting with other classes. In most of the applications I write, that doesn't happen. Maybe I read a line from a CSV file, parse it directly into SqlParameters and do an Insert, or query some data via a DataReader and write it out (CSV, XML, etc.) -- there is absolutely no need to store the data in a class, it would just needlessly bog things down.
But, there are times where having data in classes is useful. For instance maybe your car has an owner -- you can have separate classes for the car and its owner, with a relationship between them*. Now, I don't think Linq can do that (I could very easily be wrong about that), at most it would have separate classes for car and owner, but no relationship. In a DataSet (which I also don't use) the DataTables might have a relationship, but as far as I know, Linq won't.
I would also use a class if I'm passing data across a Web Service -- and I don't know whether or not Linq can do that.
So, when I use classes to hold data, I don't use Linq or EF or any other ORM tool -- I craft them myself.
*
class Person { ... }
class Car { ... Person Owner ... }
Person me = new Person(...) ;
Car mycar = new Car(...) ;
mycar.Owner = me ;
|
|
|
|
|
There are literally THOUSANDS of descriptions of what classes are, and how they work, and ALL of them are available on the internet. If you don't understand it after reading only a handful of these descriptions, how are we going to be able to describe it plainly enough for you to grasp the concept?
Classes allow you to design objects, which have properties and/or methods with which to manipulate its properties. You can then use the object as a parameter to a method instead of the individual properties. You can extend an object by implementing a new class that inherits from the original, adding more properties and methods. Just because you create an object in memory doesn't necessarily require you to save it anywhere - you can use it and then release it.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Thanks for the support. Its not that I don't understand how I can create a class that inherits properties or methods from another one or I can organize objects and their properties by creating a class for them. I am just having a hard time grasping why I would want to use them. When I think of organizing a list of people that have first names, last names, phone numbers, I think of a database table. I can store all the data in a table and query it to display what I need in a datagridview or whatever. I was hoping someone could explain or point me to a more meaningful example of why to use a class. Just reading generic descriptions of what a class does, does not help me understand how I would incorporate it in real-life coding. Maybe I am thinking to hard about it.
|
|
|
|
|
I don't think you're thinking too hard about it, I think you just haven't written applications complicated enough yet to need OO. You don't need to be asking why you want classes, you need to be asking if you should switch from structured programming to OO programming. The answer is not necessarily. It's perfectly fine to write good old-fashioned structured programs, and you do not need to write OO to use C# - you can write perfectly good C# programs with no OO code in it. However keep in mind that the .NET framework gives you a lot of free stuff, and it's all OO, so if you want to exploit it you'll have to understand how to use it. As you design more complicated applications, you may come to realize benefits of OO and some of the stuff that you get in .NET. (A DataGridView is a class, after all, so you have already "incorporated" that into your code. Do you find the DataGridView easy to use? If so, you might learn something from that model.)
|
|
|
|
|
kruegs35 wrote: When I think of organizing a list of people that have first names, last names, phone numbers, I think of a database table. I can store all the data in a table and query it to display what I need in a datagridview or whatever.
Of course - and that is fine! The DataGridView (a class itself) will hold the data for you in memory. However, to be able to reuse that data it can be far easier to have a proper representation of the data by using classes.
public enum PhoneNumberType
{
Home,
Mobile,
Work
}
public class PhoneNumber
{
private string number;
private PhoneNumberType type;
public PhoneNumber(string number, PhoneNumberType type)
{
this.number = number;
this.type = type;
}
public string Number
{
get{ return number; }
}
public PhoneNumberType Type
{
get{ return type; }
}
}
public class Person
{
private string forename;
private string surname;
private List<PhoneNumber> numbers;
public Person(string forename, string surname, IEnumerable<PhoneNumber> numbers)
{
this.forename = forename;
this.surname = surname;
if(numbers != null)
this.numbers = new List<PhoneNumber>(numbers);
else
this.numbers = new List<PhoneNumber>();
}
public string Forename
{
get{ return forename; }
}
public string Surname
{
get{ return surname; }
}
public List<PhoneNumber> Numbers
{
get{ return numbers; }
}
}
public class People : List<Person>
{
}
Now by using this structure in memory it's easy to access any part of any person in your one collection - People - without having to requery or try and extract from the DataGridView. Any of these classes can have Load, Save, Update, Delete etc methods which can access your database, or better still an interface that represents a database so you can have different implementations of the interface for dfferent database types if ever required... but you should get to grips with the reasoning for classes before exploring interfaces!
BTW, the above code was just typed here and not copied from VS so is untested and may contain errors!
|
|
|
|
|
There is a lot of literature already pointed out, but with a database object you can't "do" things. In your example the car will contain it's properties like model, color etc..., but also methods to do stuff like "accelerate", "brake", "turn" etc... Other reasons to put things in a class is so you can do validation on them 'before' going to the database.
This has not mentioned the OO principles yet, which is in itself a big topic you should read up on.
hope this helps.
V.
|
|
|
|
|
i want to insert some datas to my database from my datatable.i created a stored procedure for that like that
ALTER proc sp_VeriEkle
(
@cek_no nvarchar(10),
@cek_tarih nvarchar(20),
@num1 nvarchar(10),
@num2 nvarchar(10),
@num3 nvarchar(10),
@num4 nvarchar(10),
@num5 nvarchar(10),
@num6 nvarchar(10)
)
as
Begin
Insert into NumaraBilgileri(cekilis_no,cekilis_tarihi,num1,num2,num3,num4,num5,num6) values (@cek_no,@cek_tarih,@num1,@num2,@num3,@num4,@num5,@num6)
End
and my codes like below to insert
for (int i = 0; i < table.Rows.Count; i++)
{
cmd.Parameters.AddWithValue("@cek_no", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][0].ToString();
cmd.Parameters.AddWithValue("@cek_tarih", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][1].ToString();
cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][4].ToString();
cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][5].ToString();
cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][6].ToString();
cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][7].ToString();
cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][8].ToString();
cmd.Parameters.AddWithValue("@num6", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][9].ToString();
cmd.ExecuteNonQuery();
but here after adding first row it gives error like "Procedure or function sp_VeriEkle" has too many specified arguments and it shows as a wrong line cmd.ExecuteNonQuery().what is wrong here for second row ?
vemedya.com
|
|
|
|
|
erdinc27 wrote: for (int i = 0; i < table.Rows.Count; i++)
{
cmd.Parameters.AddWithValue("@cek_no", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][0].ToString();
Creating a parameter inside loop indicates you are creating same parameter with the same name again and again.
So my advice would be to declare parameter outside a loop and then set values only of that inside loop.
E.G.
cmd.parameters.add(\\add parameter name with datatype here);
Loop
{
cmd.parameters["\\name of the parameter"].Value = \\Set value here.
}
Thanks.
Regards,
Hiren.
"The more we give of anything, the more we shall get back." - Grace Speare
(you can consider this quote while giving vote also)
Microsoft Dynamics CRM
|
|
|
|
|
+5 for explaining
I are Troll
|
|
|
|
|
thak u for your help guys..it works good now
vemedya.com
|
|
|
|
|
It is because you are adding the seconds row parameters to the existing list or command parameters. Try creating your cmd instance inside of the for loop
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Why 2 bad answers?!?
The OP question only asks WHY it does not work - which I answer exactly. It does not say tell me the answer and give me a solution. Because that would be a HOW question
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
sometime i am getting a error when trying to generate report through crystal report and c#. but this error not occured everytime rather sometime it occured. please tell me anyone why this error occur and how to fix it.
the error is
--------------
Error Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
Error System.IO.FileNotFoundException: Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
File name: 'CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' ---> System.IO.FileNotFoundException: Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
File name: 'CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
at EOD_Schedular.frmMain.GenerateReport(Boolean ViewOnly)
at EOD_Schedular.frmMain.Generate(Boolean ViewOnly)
Method Name: GenerateReport
Line: 0
Column: 0
Sql: Not Found
thanks
tbhattacharjee
|
|
|
|
|
Putting the required DLL into GAC may solve this problem.
Just try for it.
Regards,
Hiren.
"The more we give of anything, the more we shall get back." - Grace Speare
(you can consider this quote while giving vote also)
Microsoft Dynamics CRM
|
|
|
|
|
Hi,
can someone please explain me what are the advantege and disadvantege of winforms vs. webforms.
When i rather to user one over the other.
Please assist...
10x.
|
|
|
|
|
treuveni wrote: can someone please explain me what are the advantege and disadvantege of winforms vs. webforms.
When i rather to user one over the other.
What a strange question I've ever seen.
If you are going for windows application then use WinForms and WebForms for web application.
That is not like when to use that is by choice of people Either they are devloping web or windows apps.
Usually people are using WebForms because they want to make people to see that from the M/C connected through network rather then installing WinApp on every PC.
Your question should be of WinApp vs WebApp.
Regards,
Hiren.
"The more we give of anything, the more we shall get back." - Grace Speare
(you can consider this quote while giving vote also)
Microsoft Dynamics CRM
|
|
|
|
|
Webforms: originated from the evolution of the web.
winforms: WinForms on the other hand originated from VB, MFC and other desktop environments.
If you use Winforms, you begin having a deployment problem. You either have
to have everyone update their machine everytime you update the application,
or you have to use ClickOnce. Depending on how complicated your application
and deployment scenario would be, clickonce may or may not be an option. You
will need to make sure your users have .NET 2.0 installed (clickonce can do
this for you, I believe). Because this is an internal application only and
will not be redistributed, it may be easier to implement this.
Regards,
Sathesh.
The best way to express one's gratitude to the Divine is to feel simply Happy..
|
|
|
|
|
Sathesh Sakthivel wrote: If you use Winforms, you begin having a deployment problem. You either have
to have everyone update their machine everytime you update the application,
or you have to use ClickOnce. Depending on how complicated your application
and deployment scenario would be, clickonce may or may not be an option. You
will need to make sure your users have .NET 2.0 installed (clickonce can do
this for you, I believe). Because this is an internal application only and
will not be redistributed, it may be easier to implement this.
THIS[^] would be enough rather then writing this much.
Regards,
Hiren.
"The more we give of anything, the more we shall get back." - Grace Speare
(you can consider this quote while giving vote also)
Microsoft Dynamics CRM
|
|
|
|
|
Hiren Solanki wrote: THIS[^] would be enough rather then writing this much.
Ah don't worry, he is a master in that.
..Go Green..
|
|
|
|
|
I've no problem with you quoting from other sources, but you do need to respect copyright law and provide a reference to the original material. Just a "Read more here..." link would be perfect, and extremely helpful.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Hi!!
Hello every one...
I wanted to know what should i do to keep on listening to multiple ports for msgs and displaying on respective text boxes.
i've tried asych sockets. but cant get it right..
it listens for only one time. if i start a while loop the whole app stops responding..
i hope threading is a answer but i know nothing abt it..
plz guide me..
|
|
|
|
|