|
Is anybody aware of any guidelines, issued by Microsoft or any other parties, about exposing a Queue or Queue<T> as a public property on an object? I recall some old guidelines about exposing a generic collection, but I can't find any recent information about that, either.
|
|
|
|
|
Yeah, it's best not to unless you really want other classes to have access. For one thing, it exposes implementation which makes altering the implementation difficult later.
Another problem would be if one class is iterating (foreach ing) the collection when another is trying to alter it.
What are you trying to accomplish? Think in terms of interface. Will AddItem and/or GetItem methods suffice?
|
|
|
|
|
I have not come across any such guidelines.
Its best advised not to use public for any property, unless you really have to.
|
|
|
|
|
Exposing any collection as a public property means that you lose control over the collection, because the calling class can push, pop and inspect items in the collection. It also potentially causes synchronisation issues if you use that collection within your own code and expect it to be unchanging (in particular, if you enumerate over it).
I won't say 'don't do it', because it's often extremely convenient to do so, at least with a list where you want to provide access and you don't want to write a bunch of pointless IList wrapper methods to protect it. (With a queue, chances are all you want to allow the caller to do is add to it, so in that case I would write a wrapper method so I could use something other than a queue later.) But if you do expose a collection, make sure you put a note of caution about how a caller should use it in the documentation, and remember that you're relying on the end user being responsible.
If you do choose to expose a collection, it's generally better to expose it as the relevant interface instead of the concrete class, since the Framework provides both. That allows you to change your underlying data structure in future, and expose a shim that supports the interface and maps to whatever you now use.
None of this has anything to do with generics, by the way. It is exposing collections to the outside that you should be wary of.
|
|
|
|
|
I'm exposing a recorded history of events (changes to a database), in the order that they occured. I expect to update the queue periodically. Original order of events is important.
I expect the user to periodically consume this information, in order, and then come back for more. So I was using a queue as it enables them to dequeue at will, while maintaining the original order, and keeping track of which events they have already noted/processed.
The user really only needs to dequeue (ie, remove one item at a time, and be able to tell that they have consumed all the items currently available).
|
|
|
|
|
If the user tries to dequeue as you are trying to enqueue, I think odd things can happen. It may be better to instead expose an event for when a new event should be posted, and have the user catch that event and add the item to a local queue, which it can dequeue when convenient.
|
|
|
|
|
Consider an event for that. Among other things it allows multiple listeners.
|
|
|
|
|
Heh. Actually, this code was written to prevent the user from having to deal with events. I'm wrapping a COM object which does raise events, but has complexities and issues that are problematic. This code maps an event raiser to something a user can poll.
If those are the primary concerns, I'll stick with this model for now. My stress tests should tell me how much collision I'm seeing during my internal enqueue.
Thanks everybody!
|
|
|
|
|
Then maybe have Enqueue and Dequeue methods.
|
|
|
|
|
How can we use XML file to configure Visual studio post build event?
|
|
|
|
|
This makes no sense. What exactly are you trying to accomplish??
Post Build events are stored in the project file. So where does this XML file come into play??
|
|
|
|
|
You can create a custom MSBuild file to build the project, and then your post build actions.
Then use MSBuild to build your project, instead of building from Visual Studio.
|
|
|
|
|
Server Error in '/bliss' Application.
Incorrect syntax near 'w'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'w'.
Source Error:
Line 83: SqlDataAdapter da3 = new SqlDataAdapter(dept_Abr, con);
Line 84: DataSet ds3 = new DataSet();
Line 85: da3.Fill(ds3);
Line 86: string fun_abr = ds3.Tables[0].Rows[0][0].ToString();
Line 87:
Source File: c:\bliss\www\www_ca_view.aspx.cs Line: 85
Stack Trace:
[SqlException (0x80131904): Incorrect syntax near 'w'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +925466
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800118
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +31
System.Data.SqlClient.SqlDataReader.get_MetaData() +62
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +122
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +7
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +141
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +86
www_www_ca_view.refno() in c:\bliss\www\www_ca_view.aspx.cs:85
www_www_ca_view.Page_Load(Object sender, EventArgs e) in c:\bliss\www\www_ca_view.aspx.cs:64
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
|
|
|
|
|
Your SQL query is broken. That is pretty obvious and why I've given you a 1, because you've clearly not even tried to understand before wasting our time here. Furthermore, you haven't shown us the SQL query you're using so there is no way we can possibly tell you what's wrong with it, except you have a stray 'w' in it.
|
|
|
|
|
thank u.. am very much new to .NET so ..
|
|
|
|
|
So far I am not very impressed with the story line. The characters are two dimensional and the plot is obvious,.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
You might want to look at the stack trace next time to get a better clue of where the error actually is.
|
|
|
|
|
.
As Dave said, look at the details provided by the stack trace.
And don't use release builds when there are still big issues in your code; with a debug build, the stack trace would provide actual line numbers rather than offsets.
|
|
|
|
|
Hi,
I wrote a code to export my data in gridview to excel. But at runtime it throws an exception saying "syntax error in INSERT TO" on execution of the command. I stepped through the code to see the output of the sql statement and I see nothing wrong with it. Can anyone assist me quickly on this please? Very many thanks in advance.
NB: The table creates perfectly, but the INSERT statement gives the exception.
My Code:
public static void ExportStudentsToXls(string filepath, List<Student> students)
{
using (ExcelHelper helper = new ExcelHelper(filepath))
{
helper.Hdr = "YES";
helper.Imex = "0";
Dictionary<string, string> tableDefinition = new Dictionary<string, string>();
tableDefinition.Add("StudentID", "int");
tableDefinition.Add("RegNo", "varchar(50)");
tableDefinition.Add("Surname", "varchar(100)");
tableDefinition.Add("Firstname", "varchar(100)");
tableDefinition.Add("Middlename", "varchar(100)");
tableDefinition.Add("LevelID", "int");
tableDefinition.Add("Sex", "varchar(10)");
tableDefinition.Add("DOB", "decimal");
tableDefinition.Add("Religion", "varchar(50)");
tableDefinition.Add("Nationality", "varchar(100)");
tableDefinition.Add("StateOfOrigin", "varchar(100)");
tableDefinition.Add("ClassID", "int");
tableDefinition.Add("ParentID", "int");
tableDefinition.Add("Address", "varchar(150)");
tableDefinition.Add("HomePhone", "varchar(20)");
tableDefinition.Add("MobilePhone", "varchar(20)");
tableDefinition.Add("Email", "varchar(255)");
tableDefinition.Add("NextofKin", "varchar(100)");
tableDefinition.Add("KinAddress", "varchar(200)");
tableDefinition.Add("BloodGroup", "varchar(50)");
tableDefinition.Add("Genotype", "varchar(50)");
tableDefinition.Add("Disability", "varchar(50)");
tableDefinition.Add("SessionID", "int");
tableDefinition.Add("TermAdmitted", "varchar(50)");
tableDefinition.Add("LevelAdmitted", "varchar(50)");
tableDefinition.Add("ClassAdmitted", "varchar(50)");
tableDefinition.Add("DateAdmitted", "decimal");
tableDefinition.Add("Hobbies", "varchar(50)");
tableDefinition.Add("Picture", "varchar(50)");
tableDefinition.Add("DateRegistered", "decimal");
tableDefinition.Add("Status", "varchar(50)");
tableDefinition.Add("Deleted", "varchar(5)");
helper.WriteTable("Students", tableDefinition);
string decimalQuoter = (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.Equals(",") ? "\"" : String.Empty);
foreach (var student in students)
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO [Students] (StudentID, RegNo, Surname, Firstname, Middlename, LevelID, Sex, DOB, Religion, Nationality, StateOfOrigin, ClassID, ParentID, Address, HomePhone, MobilePhone, Email, NextofKin, KinAddress, BloodGroup, Genotype, Disability, SessionID, TermAdmitted, LevelAdmitted, ClassAdmitted, DateAdmitted, Hobbies, Picture, DateRegistered, Status, Deleted) VALUES(");
sb.Append(student.StudentID); sb.Append(",");
sb.Append('"'); sb.Append(student.RegNo); sb.Append("\",");
sb.Append('"'); sb.Append(student.Surname); sb.Append("\",");
sb.Append('"'); sb.Append(student.Firstname); sb.Append("\",");
sb.Append('"'); sb.Append(student.Middlename); sb.Append("\",");
sb.Append(student.LevelID); sb.Append(",");
sb.Append('"'); sb.Append(student.Sex); sb.Append("\",");
sb.Append(decimalQuoter); sb.Append(student.DOB.Value.ToOADate()); sb.Append(decimalQuoter); sb.Append(",");
sb.Append('"'); sb.Append(student.Religion); sb.Append("\",");
sb.Append('"'); sb.Append(student.Nationality); sb.Append("\",");
sb.Append('"'); sb.Append(student.StateOfOrigin); sb.Append("\",");
sb.Append(student.ClassID); sb.Append(",");
sb.Append(student.ParentID); sb.Append(",");
sb.Append('"'); sb.Append(student.Address); sb.Append("\",");
sb.Append('"'); sb.Append(student.HomePhone); sb.Append("\",");
sb.Append('"'); sb.Append(student.MobilePhone); sb.Append("\",");
sb.Append('"'); sb.Append(student.Email); sb.Append("\",");
sb.Append('"'); sb.Append(student.NextofKin); sb.Append("\",");
sb.Append('"'); sb.Append(student.KinAddress); sb.Append("\",");
sb.Append('"'); sb.Append(student.BloodGroup); sb.Append("\",");
sb.Append('"'); sb.Append(student.Genotype); sb.Append("\",");
sb.Append('"'); sb.Append(student.Disability); sb.Append("\",");
sb.Append(student.SessionID); sb.Append(",");
sb.Append('"'); sb.Append(student.TermAdmitted); sb.Append("\",");
sb.Append('"'); sb.Append(student.LevelAdmitted); sb.Append("\",");
sb.Append('"'); sb.Append(student.ClassAdmitted); sb.Append("\",");
sb.Append(decimalQuoter); sb.Append(student.DateAdmitted.Value.ToOADate()); sb.Append(decimalQuoter); sb.Append(",");
sb.Append('"'); sb.Append(student.Hobbies); sb.Append("\",");
sb.Append('"'); sb.Append(student.Picture); sb.Append("\",");
sb.Append(decimalQuoter); sb.Append(student.DateAdmitted.Value.ToOADate()); sb.Append(decimalQuoter); sb.Append(",");
sb.Append('"'); sb.Append(student.Status); sb.Append("\",");
sb.Append('"'); sb.Append(student.Deleted); sb.Append("\",");
sb.Append(")");
helper.ExecuteCommand(sb.ToString());
}
}
|
|
|
|
|
kaycey90 wrote: sb.Append('"'); sb.Append(student.Deleted); sb.Append("\",");
sb.Append(")");
You have a , just before your closing ) . You don't need to put that in. BTW, why did you not consider using sb.AppendFormat instead? You could have saved yourself a lot of sb.Append work there.
|
|
|
|
|
Use a parameterised query! And if that's too hard (it's not), at least escape the input strings. You're going to break things when you meet Mr. O'Brien. (Not to mention Bobby Tables.)
SQL doesn't use double quotes, it uses single quotes. But I will re-emphasise, building a query string like this is the wrong way.
|
|
|
|
|
That deserves more than a 5.
|
|
|
|
|
I've heard of Bobby Tables, but could you provide a link to Mr. O'Brien?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
That's not a comic reference, just the first Irish name that came to mind. Irish surnames are the source of most real world accidental query failures of this kind.
|
|
|
|
|
Oh I see. The apostrophe.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|