|
It would be much easier to make it nullable.
In C# 2.0 onwards, to create a nullable struct declare it like this:
int? myNullableInt = null;
Note the question mark after the type name. This indicates that it is actually a nullable int, and as you can see you can assign null to it.
So, to declare a list of nullable struct types, try something like this:
List<int?> myNullableIntList = new List<int?>;
|
|
|
|
|
That causes problems when trying to access properties of the struct but I get the idea!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
If you're trying to keep the check simple and elegant, you could try an approach of returning either the nullable item, or when null a default item, something similar to:
struct myStruct {
public bool IsValid;
public readonly myStruct Empty;
}
foreach(myStruct? item in collection) {
if((item ?? myStruct.Empty).IsValid) {
...
}
}
The ?? operator returns whats on the left if not null or else whats on the right.
I'm largely language agnostic
After a while they all bug me
|
|
|
|
|
It works when definining an object that way but not when derriving from List<struct>
See below[^]
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
I'll second Colin's suggestion (use the Nullable type). I just thought I'd explain how it works, essentially the nullable operator (? ) in C# is syntatic sugar like LINQ which tells the compiler to change:
int? myInt;
Nullable<int> myInt; Nullable is a class defined in the System namespace and has two properties, Value and HasValue (almost exactly what you're suggesting implementing), however the operators are already implemented so for most cases you can forget you're dealing with a nullable type (just remember to perform null checks).
|
|
|
|
|
I think I get it - but how to make it work?
Consider this simple sample code:
public struct X
{
private string m_Property;
public string Property
{
get { return m_Property; }
set { m_Property = value; }
}
}
public class XCollection : List<X>
{
private bool ItemExists(string stringProperty)
{
X found = Find(item => item.Property == stringProperty);
return found.Property != null ? true : false;
}
}
Changing the collection class declaration to public class XCollection : List<X?> screws it up!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Then put an additional check for null in the lambda expression
item => (item == null) ? false : item.Property == stringProperty
|
|
|
|
|
Also, your stuct is mutable, this will cause you additional problems because the instances of the struct are copied rather than referenced you may be making changes to a copy rather than the original and the functionality may not be desired.
You should make your struct immutable by removing any setters and methods that modify the internal state once it is created.
|
|
|
|
|
Thanks for the advice but I need the struct to be mutable in this situation.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Then you would be better changing it to a class.
|
|
|
|
|
The struct could have a static member to use in that case, kinda like String.Empty and Guid.Empty
|
|
|
|
|
Doh! That hadn't crossed my mind - thanks
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
I want to make a program to monitor the useage of my system,
In short a keylogger like program.
Now i don't know which method i should use,
I dont care for precision just performance.
So i use a timer then get the current active window and GetKeyboardState using getkeyboardstate does mean i wont have the correct order of letters but thats not really the point.
Or i could use a keyboard and mouse hook but im wondering if this is unstable for my system or has heavy workload which i dont really want.
So what should i do?
Thanks
|
|
|
|
|
Scalee wrote: I want to make a program to monitor the useage of my system,
In short a keylogger like program.
Why? Do you type in your sleep?
led mike
|
|
|
|
|
1) Convince us you're not trying to write malware
2) see )1
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
If i wanted to write malware then i would care about the order the letters where typed,
And i would give a crap about system stability because it wouldn't be my system.
And second why try to make a keylogger while you can download a ton online, So i hope this convinces you.
The reason why i want to create this is because i often leave my laptop unattended for a while at work and probably because im paranoid.
|
|
|
|
|
"Why try to make a keylogger while you can download a ton online" would then be my answer
|
|
|
|
|
|
I want to use the "SELECT " statement on DataTable, which fetches records between these 2 date ranges:
StartDate : 01-01-2007 and,
EndDate : 01-01-2008
The code is as follows:
if (newPDS.Tables[0].Columns["ADM_DATE"]== null)
{
DataColumn dCol = new DataColumn(newPDS.Tables[0].Columns.Add("ADM_DATE", typeof(DateTime), "").ToString());
}
for(Int32 i = 0; i < newPDS.Tables[0].Rows.Count; i++)
{
row = newPDS.Tables[0].Rows[i];
row["ADM_DATE"] = row["CADMMM"] + "/" + row["CADMDD"] + "/" + row["CADMHH"] + row["CADMYY"];
DateTime newDT = new DateTime();
newDT = Convert.ToDateTime(row["ADM_DATE"]).Date;
row["ADM_DATE"] = newDT.ToShortDateString();
string newQuery = "ADM_DATE between StartDate and EndDate";
newDR = myTable.Select(newQuery);
}
myReport.SetDataSource(newDR);
crystalReportViewer1.ReportSource = myReport;
Thanking you in anticipation.
modified on Tuesday, August 5, 2008 3:33 PM
|
|
|
|
|
Hi,
Try using syntax #m/d/yy# for literal date strings. For example:
string newQuery = "ADM_DATE between #1/1/2007# and #1/1/2008#";
Mika
|
|
|
|
|
Thanks Mika.
Just now I learned that, ADO.Net expressions do not support the "BETWEEN" keyword (it's showing an error as : Expression contains unsupported operator"BETWEEN").......so I just break it up into clauses with an "AND" as
"ADM_DATE > StartDate AND ADM_DATE < EndDate"
And when I'm using the above lines, its showing up the database connection popup, where it asks for Server Name, Database Type, LoginId, Password etc. That means that its still looking for the Crystal Reports datasource as DataSet's Table.
My previously working Crystal Reports datasource expression was like this :
myReport.SetDataSource(newPDS.Tables["SAMFILE_CLIENTP"]);
where : <br />
newPDS - DataSet <br />
SAMFILE_CLIENTP - DataSet Table
Any idea ???
|
|
|
|
|
Glad it solved.
It's been a while since I last used Crystal but if I remember correctly, try also to use SetDatabaseLogon method and after setting databases and datasources, use VerifyDatabase -method. This way you have full control over datasources and their logon information and you can catch any possible problems while executing VerifyDatabase.
Hope this helps,
Mika
|
|
|
|
|
when changing the Image property of a picturebox, the select resource dialogue is showing up with two options...
local resource and project resource file. What is the difference?
thank you
When you get mad...THINK twice that the only advice
Tamimi - Code
|
|
|
|
|
Hi,
Project resources are *shared* throughout all the classes in the project but local resources are stored in the current form's resx file so they are usable only through current form.
Hope this helps,
Mika
|
|
|
|
|
thank you..
but is there any relation between these options and the project
distribution ??
When you get mad...THINK twice that the only advice
Tamimi - Code
|
|
|
|