Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dears
I wrote a Class in one of my C# application that I use it a lot in my other programs
it's a class that contains some methods for Reading or Writing Data in an Access database.
I was seeking for a solution to not add and write this class to my every program
I searched the net, and found that it's better to write a dll file that I can use every time in my programs just by adding a reference,
but now my question is:

for e.g. suppose that I have a method like this
C#
public void Write_Account(Account account)   //Write_Data
        {
            if (Validate_Account(account.UserName) == false)
            {
                try
                {
                    if (con.State != ConnectionState.Open)
                    {
                        con.ConnectionString = con_string;
                        con.Open();
                    }

                    StringBuilder st = new StringBuilder();
                    
                    st.Append("Insert into Accounts Values ('");
                    st.Append(account.UserName);
                    st.Append("','");
                    st.Append(account.PassWord);
                    st.Append("','");
                    st.Append(account.Email);
                    st.Append("','");
                    st.Append(account.VerificationCode);
                    st.Append("','");
                    st.Append(account.ActivationStatus);
                    st.Append("')");
                    
                    data_cmd.Connection = con;
                    data_cmd.CommandText = st.ToString();

                    data_cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("an error occured while connecting database\n" + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
            else
                MessageBox.Show("This user already exists!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }  //end of Write_Data*************************************


here I know that how many entry I want to write (for e.g my Account class has 5 arguments)

but what can I do if I don't know how many entries I have?
how can I write this part of code more general?
Posted

The problem is not clear. You are looking for some abstraction technique while you don't have abstract target. I mean that in your exact case you don't really need further abstraction. Here is why: you pass a parameter of the class or structure Account. It has five properties you want to append to some string, no more. If this is a class, you can extend it and pass an instance of a derived class, but as your method is written to the base abstract or pseudo abstract class, it can only work with the property of a base class.

The classic OOP approach is that you need to add some virtual method like AppendPropertyValuesToStringBuilder and override it in each of the derived classes. In this case, you will move the fragment appending Name, Password, Email, VerificationCode and ActivationStatus from your Write_Account method to each of derived classes where it really belongs, because only the classes know what are their properties. Remember, in each of derived classes you can always call base.AppendPropertyValuesToStringBuilder to append the part of data already defined in the base class (where this method is non-abstract). As it leverages the mechanism of late binding, the method AppendPropertyValuesToStringBuilder called by Write_Account on a base class as a compile-time type, will actually be dispatched to call the overridden method of one of the run-time types to be passed to this method as its argument; and each of those run-time types will be one of the derived classes. This is a heart of classical OOP. Please see:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Late_binding[^],
http://en.wikipedia.org/wiki/Object-oriented_programming[^].

You really need to fully understand OOP to do any .NET development.

Now, you have some solution, but you need to look at this problem from a different point of view. You don't use appropriate abstraction mechanism not just because you don't know them well, but because your approach to data is questionable. Your property appending code tells me that you are trying to work with string presentation of data instead of data itself. Yes, there are places where you need to show some data in the string form; on the screen, for example. But this should be totally isolated from data itself. Just think about it.

And finally, your code has one critical problem. In your exception catch handler, you totally block exception propagation. As a rule of thumb, you should not do it. Let exception go. You can finally catch all exceptions only on the top of the stack of each thread. By blocking exception propagation, you effectively defeat the purpose of structural exception handling. You really need to learn the basics of exception handling. The pain purpose of it is to avoid dealing with exception in almost all of your code. The mechanism isolates exceptional situation from the regular operation, so you can forget about them in most of your work. Please see:
http://en.wikipedia.org/wiki/Exception_handling[^].

Please also see my recommendations in my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Error Logging and Screen Shot.[^],
Catching an Exception[^],
Handling exceptions in class library (dll)[^].

—SA
 
Share this answer
 
Comments
BillW33 15-Aug-12 16:40pm    
Very nice answer, +5
Sergey Alexandrovich Kryukov 15-Aug-12 17:10pm    
Thank you,
--SA
Mostafa M.A 15-Aug-12 21:19pm    
thank a lot Sergey Alexandrovich Kryukov
yes you are write
because I'm new to this language and also this site
I must learn many things first
by now that I'm a member of this site I have good friend like you to guide me what should I learn for my purposes
Sergey Alexandrovich Kryukov 15-Aug-12 21:43pm    
Great. And you are very welcome.
If so, please accept this answer formally (green button) -- thanks.
Your possible follow-up questions are of course welcome.
--SA
Abdul Quader Mamun 15-Aug-12 21:37pm    
greatly Answered!
I agree with Sergey Alexandrovich, the problem isnt in that piece of code. The problem in simple words is that your function depends on a class "Account", so if you want to make the function more abstract you need to do the class more abstract by overloading functions.

Now Im curious, why do you pass an Accaunt parameter to this function? it isnt on the account class?

I would prefer to include this function in the Accaunt class so I could take the default class parameters when you create it.
 
Share this answer
 
Comments
Mostafa M.A 15-Aug-12 21:24pm    
I said the reason of sending account to another class
it's a class that have many functions for working with data that I prepared myself, so I wanted to make a dll of that
one of the methods is this.

you mean that I have to overload the method 2-3 times for getting for e.g 5 or 6 or more parameters, right?
Member 8437747 16-Aug-12 14:32pm    
Well I consider writing this method into Account and overload it would be the best option, if you want to do a general class that abstract you should study more about OOP and abstraction.

Enjoy your time here in this page and dont doubt in sharing any knoledge, we are here to help each other.
Mostafa M.A 16-Aug-12 15:58pm    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900