you can overwrite the ToString function
joost versteegen
Posts
-
filling a column with property value of object -
Minimize the risk that (bad) programmers call specific functions?make your functions private and call them from the validation layer
-
problem with xml codeI have a problem with the following xml documents. they look like this:
my c# code:
public class StructArray { \[XmlAttribute(AttributeName = "name")\] public string Name { get; set; } \[XmlArray(ElementName = "structDef")\] \[XmlArrayItem(ElementName = "item", Type = typeof(StructDefItem))\] public List StructDefinition { get; set; } \[XmlArray(ElementName = "values")\] \[XmlArrayItem(ElementName = "item", Type = typeof(StructValueItem))\] public List Values { get; set; } } public class StructDefItem { \[XmlAttribute(AttributeName = "name")\] public string Name { get; set; } \[XmlAttribute(AttributeName = "dataType")\] public Int32 DataType { get; set; } } \[XmlInclude(typeof(Tool))\] \[XmlInclude(typeof(Component))\] public class StructValueItem { } public class Tool : StructValueItem { //\[XmlAttribute(AttributeName = "id")\] public string id { get; set; } //\[XmlAttribute(AttributeName = "state")\] public Int32 state { get; set; } //\[XmlAttribute(AttributeName = "counter")\] public Int32 counter { get; set; } }
The problem is that the type of object can be all sort of things (in this example a tool or a component), but when I use XmlInclude, the result gives : wich is not allowed, the data comes without xmnls information and
-
finding periodsi finally figured it out, here's the result:
DECLARE @PERIOD TABLE (start_time DATETIME, end_time DATETIME)
INSERT INTO @PERIOD(start_time)
SELECT t.start_time FROM (
SELECT act_start_time_utc AS start_time, state_cd AS new_state, LAG(state_cd, 1) OVER (ORDER BY act_start_time_utc ASC) AS last_state
FROM SRPMES901007.WWMESDB.dbo.job
WHERE act_start_time_utc IS NOT NULL) AS t
WHERE t.new_state = 4 AND t.last_state <> 4UPDATE period
SET end_time = (SELECT TOP(1) act_start_time_utc FROM SRPMES901007.WWMESDB.dbo.job WHERE state_cd <> 4 AND act_start_time_utc > period.start_time ORDER BY act_start_time_utc ASC)
FROM @PERIOD periodSELECT * FROM @PERIOD ORDER BY start_time ASC
But the query takes 19 seconds, can it be speeded up?
-
finding periodsgiven a table job with columns time_utc (datetime) and state (integer), i want to find the periods (start-time and end_time) where the consequetive value of the state column equals 4. i tried to find the start of the periods with the folllowing query, but no success. too many results. can someone please help me? i cannot figure it out.
SELECT act_start_time_utc, state_cd FROM SRPMES901007.WWMESDB.dbo.job WHERE act_start_time_utc IS NOT NULL ORDER BY act_start_time_utc ASC
DECLARE @PERIOD TABLE (start_time DATETIME, end_time DATETIME)
INSERT INTO @PERIOD(start_time)
SELECT t.start_time/*, t.new_state, t.last_state*/ FROM (
SELECT act_start_time_utc AS start_time, state_cd AS new_state, LAG(state_cd, 1) OVER (ORDER BY act_start_time_utc ASC) AS last_state
FROM SRPMES901007.WWMESDB.dbo.job
WHERE act_start_time_utc IS NOT NULL) AS t
WHERE t.new_state = 4 AND t.last_state <> 4UPDATE period
SET end_time = (SELECT MIN(act_start_time_utc) endtime FROM SRPMES901007.WWMESDB.dbo.job INNER JOIN @PERIOD period ON job.act_start_time_utc > period.start_time AND state_cd <> 4)
FROM @PERIOD periodSELECT * FROM @PERIOD ORDER BY start_time ASC
-
update temp table with results from sub query (actually it is a function)this works! Thanks.
-
update temp table with results from sub query (actually it is a function)wow, ok, i will try it first thing in the morning. thanks.
-
update temp table with results from sub query (actually it is a function)thanks. i tried it, but i get the error 'Cannot call methods on table'. too bad. my original looks so inefficient, but it will have to do i guess.
-
update temp table with results from sub query (actually it is a function)hi, i am having trouble with a query. how can i update a temp table with results from sub query (actually it is a function)? this works:
UPDATE @TEMP SET formula\_name = (SELECT name FROM dbo.fn\_GetFormulaForJob(wo\_id, oper\_id, seq\_no)), formula\_version = (SELECT version FROM dbo.fn\_GetFormulaForJob(wo\_id, oper\_id, seq\_no)) FROM @TEMP
but i want something like this:
UPDATE @TEMP SET formula\_name = r.name, formula\_version = r.version FROM (SELECT name, version FROM dbo.fn\_GetFormulaForJob(wo\_id, oper\_id, seq\_no) r
thanks.
-
USB OVER NETWORKCreate a small service and install it on PC "A", that accepts or sends xml or json messages over http or tcp. Let the C# program on server "B" ask PC "A" to read te USB device. The messages could optionally be encrypted later.
-
how to access temp table field in subquery?thank you, i will try it
-
how to access temp table field in subquery?hi, I get an error : The multi-part identifier "e.ent_name" could not be bound. when trying to access a field from a temp table in a sub-query.
declare @entities table(ent\_id int, ent\_name varchar(50), startDT datetime) insert into @entities select ent.ent\_id, ent\_name, prod\_job.act\_start\_time\_local from job as prod\_job inner join ent as ent on prod\_job.run\_ent\_id = ent.parent\_ent\_id where prod\_job.wo\_id = 'B.100077779' select \* from @entities -- so far so good select \* from @entities as e inner join ( select top(1) act\_finish\_time\_local, oper\_id from job as cleaningJob where cleaningJob.oper\_id like '%'+ e.ent\_name +'%' ----> error order by act\_finish\_time\_local desc ) as j on j.act\_finish\_time\_local < e.startDT
how can i fix this? Thanks
-
is there a way to define the signature of a delegate in an interface without defining the signature twice?Oke, that makes sense. Thank you.
-
is there a way to define the signature of a delegate in an interface without defining the signature twice?Thank you for the reply. It is not what I mean; In your solution the ICounterprocessor can raise the event, but it's meant to react on the event, like an observer. Maibe I better forget the whole event stuff, and change to Observer like structure?
-
is there a way to define the signature of a delegate in an interface without defining the signature twice?Hi, I have the following code:
class Program
{
static void Main(string[] args)
{
ProductCounterWatcher w = new ProductCounterWatcher();
ICounterProcessor p = new PrdStandStillDetector();
w.OnCounterChanged += p.CounterChanged;
w.Detect();
w.OnCounterChanged -= p.CounterChanged;
}
}
//-----------------------------------------------------------------------------------
public class ProductCounter
{
public int Value { get; set; }
public DateTime Timestamp { get; set; }public override string ToString() { return string.Format("\[ProductCounter:{0},{1}\]", Timestamp, Value); }
}
//-----------------------------------------------------------------------------------
public interface ICounterProcessor
{
void CounterChanged(object source, CounterChangedEventArgs e); // <<<----
}
//-----------------------------------------------------------------------------------public delegate void CounterChangedHandler(object source, CounterChangedEventArgs e); // <<<----
//-----------------------------------------------------------------------------------public class CounterChangedEventArgs : EventArgs
{
public ProductCounter Counter { get; set; }public CounterChangedEventArgs(ProductCounter counter) { this.Counter = counter; }
}
//-----------------------------------------------------------------------------------
public class PrdStandStillDetector : ICounterProcessor
{
public void CounterChanged(object source, CounterChangedEventArgs e)
{
}
}
//-----------------------------------------------------------------------------------
public class ProductCounterWatcher
{
public CounterChangedHandler OnCounterChanged;public void Detect() { if (OnCounterChanged != null) OnCounterChanged(this, new CounterChangedEventArgs(new ProductCounter() { Timestamp = DateTime.Now, Value = 1 })); }
}
I have to define the procedure signature twice (see <<<--- mark). is there a way to avoid this and thus only define the signature once? Thanks.
-
System.IndexOutOfRangeExceptionvery interesting, thanks a lot!
-
System.IndexOutOfRangeExceptionok, thanks. I will try.
-
System.IndexOutOfRangeExceptionthey are all framework, except maybe OnRowEnter, but the breakpoint i set did not get hit.
private void dataGridViewDefects_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (_SelectedDrum != null && e.RowIndex > -1) _SelectedDefect = _SelectedDrum.Defects[e.RowIndex];
}
catch (Exception exc)
{
Logger.LogException(exc);
}
}the application crashes in the line:
Application.Run(new TwisterForm());
-
System.IndexOutOfRangeExceptionyes i have, but it does not reach the breakpoint.
private void dataGridViewDefects\_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { }
in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)
this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseClick); this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseDoubleClick); this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects\_RowEnter); this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects\_Enter); this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects\_KeyDown); this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects\_Leave);
-
System.IndexOutOfRangeExceptionHi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}Then I import them in a datagridview like so:
\_SelectedDrum.Defects = database.GetList(); dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect() { AVI\_Defect = new AVIDefect() { SequenceNumber = -1, Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, MVI\_Defect = new Defect() { Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, FVI\_Defect = new Defect() }; \_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a def