|
Make a wrapper class that you can use to preserve the order:
class OrderHelper<T> : IComparable<OrderHelper<T>> {
public T Item {get; private set;}
public int Order {get; private set;}
public OrderHelper(T item, int order) { this.Item = item; this.Order = order; }
public int CompareTo(OrderHelper<T> other) {
return Comparer<T>.Default.Compare(Item, other.Item);
}
}
Create a list of these from your primary sort list:
int index = 0;
List<OrderHelper<int>> orderedList = listOne.Select(i => new OrderHelper(i, index++)).ToList();
Sort that list, and then you can retrieve the index list:
orderedList.Sort();
List<int> indices = orderedList.Select(oi => oi.Order).ToList();
Finally you can order the other lists by that index set:
public static List<T> OrderBy(List<T> list, List<int> indices){
List<T> r = new List<T>(indices.Count);
for(int i = 0; i < r.Count; i++){
r[i] = list[indices[i]];
}
return r;
}
listTwo = OrderBy(listTwo, indicies);
listThree = OrderBy(listThree, indicies);
|
|
|
|
|
Hi,
I am learning OOP and trying to apply it to my C# WinForms applications.
So I have a Form with 2 textboxes. One to enter professor. Another to enter lecturer.
I have created an abstract 'Staff' class. Then I have a Professor class and a Lecturer class. Both inherit from Staff.
In Staff, I have declared an abstract property StaffName. So this must be implemented in other classes. Am I correct so far?
These classes are called on Button_Click event...
Professor professor = new Professor();
professor.StaffName = staffTextBox1.Text;
Lecturer lecturer = new Lecturer();
lecturer.StaffName = staffTextBox2.Text;
MessageBox.Show(string.Format("Analyst {0}\nSupervisor {1}", professor.StaffName, lecturer.StaffName), "Output", MessageBoxButtons.OK);
And my classes...
public abstract class Staff
{
protected string staffName;
public abstract string StaffName
{
get;
set;
}
}
public class Professor : Staff
{
public override string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
public class Lecturer : Staff
{
public override string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
Is this a correct OOP technique in WinForms or is there issues with my code design?
Thanks for your help!!
|
|
|
|
|
You should implement your StaffName property in your base (not abstract) class, as it will be used by all types. Any common properties or methods should be in the base class, and the child classes only need things that are unique to them. So you would have:
public class Staff
{
private string staffName;
public string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
public class Professor : Staff
{
}
public class Lecturer : Staff
{
}
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Not sure if the OP noticed but I wanted to point this out. In Richard's response, he changed the access modifier for "string staffName" to "private" rather than "protected". Since the "StaffName" property is handled entirely by the base class, the derived types do not need to see the "staffName" member. "private" means that the object is only accessible to the class or struct defining it. "protected" would mean that the derived types could see the member as well.
EDIT:
Richard, I have a question about this now. Haha. What if the programmer decided to create a new class deriving from 'Staff' and the new class needed its own implementation of 'StaffName'? Would it be better to create a new 'private' field in the derived class? Or should 'private staffName' be changed to 'protected' in the base class? (I'm guessing create its own in the new class because none of the other derived types need it. But I wanted to clarify.)
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Matt U. wrote: In Richard's response, he changed the access modifier for "string staffName" to "private" rather than "protected". Exactly so, because the variable should only be accessible to the getter and setter in the base class; no other class needs to access it. In later versions of .NET you do not even need to define the variable as the compiler generates a hidden one automatically.
Matt U. wrote: What if the new class needed its own implementation of 'StaffName'? Why would it, it's only a name?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Perhaps that was a bad example. Or perhaps you see what I was getting at but it isn't really ever going to happen the way I was talking?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Matt U. wrote: Perhaps that was a bad example. No, it was quite reasonable, but you need to understand the use and abuse of properties. They have a specific purpose and the important point is that the actual variable is always hidden from the outside world. If you need to access the variable from another class then you are breaking the encapsulation, and potentially, your program.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That makes sense. Thanks for the clarification. I guess it's time I study OOP a bit more. Haha.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Right. Perhaps a virtual property, but it doesn't sound like a good idea in this case. (I didn't even know properties could be virtual. Why?)
|
|
|
|
|
I don't see what that has to do with WinForms.
I agree with not making StaffName abstract, and using an auto-implemented property. And why name it StaffName rather than Name? And so far I see no reason for inheritance.
So, on the whole, I'd say, "no, it's not a good design (so far)", but that's just me.
Additionally, I'd suggest providing the name and such to the constructor -- an instance should be meaningful once it's instantiated, and a nameless Staff doesn't seem meaningful.
|
|
|
|
|
1 )Which part of your code do you add to your article
2) how much code should you add
3) how important are illustration
|
|
|
|
|
1) Anything required to build the demo -- you included a demo, right?
2) In the article with explanations? More is better.
3) Generally not very, but it depends on the subject. Please don't include screenshots of Visual Studio.
There is also an Article Writing forum here http://www.codeproject.com/Forums/1641/Article-Writing.aspx[^]
|
|
|
|
|
|
How Print Crystal Report in c# 2008 using Access Database ....
How set User Id, Password when Print Crystal Report????
|
|
|
|
|
Hi everybody,
I try to create a morse code sound with high speed. I referenced some project, and i used this project to reference. But when i used this code, it seems cannot hear the sound. Can anybody help me please?
Thank so much.
|
|
|
|
|
I really doubt that anyone here is going to download this project just to figure out what's wrong. If I were you, I would ask here[^] instead; they are more likely to know the answer.
|
|
|
|
|
|
No problem. I hope they can sort this issue out for you.
|
|
|
|
|
Hello,
I've been working on a program to renew Windows profiles on a remote location via Active Directory.
The program retrieves a user profile location(ProfilePath)
once the program verifies the email adres to be valid. The problem with the code provided below is that aside from the test e-mail account i used any other e-mail adres inputted returns the NullReferenceException.
my test account works perfectly and renames the profile to <profilename>.old. I can't think or see what i did wrong,
Any critisism or suggestions what i could try is welcome.
Thanks in advance.
Greetings,
Edit:
Found out the problem lies with loggin in with different credentials. I need to extend the right of the logged in over the entire application the loggin works but renaming a folder is not allowed on for the user running the application
private string GetProfilePath(string user)
{
DirectoryEntry dirEntry = CreateDirEntry();
DirectorySearcher dirSearcher = new DirectorySearcher();
dirSearcher.Filter = "mail=" + user;
dirSearcher.PropertiesToLoad.Add("profilePath");
SearchResult sResult = dirSearcher.FindOne();
if (sResult == null)
{
MessageBox.Show("Niks gevonden", "Niks gevonden", MessageBoxButtons.OK, MessageBoxIcon.Error);
SystemSounds.Beep.Play();
return null;
}
else
{
DirectoryEntry dirEntr = new DirectoryEntry(sResult.Path);
string profile = dirEntr.Properties["profilePath"].Value.ToString();
dirEntr.Close();
dirEntry.Close();
return profile;
}
}
modified 21-Nov-12 6:51am.
|
|
|
|
|
May I suggest you always write English, including comments.
I'm native Flemish speaking, but most on CP are not and if you're Belgian, you'd know that our French-speaking neighbors across the language border do not understand it either...
PS: Doesn't the Beep come AFTER you pressed OK on the Messagebox, in this case? Shouldn't it be before?
it's difficult to say what happens, but I suggest you add some logging and print out the different values, that might give you a clue.
|
|
|
|
|
doing so now.
and point taken about comments being in english.
My code is kinda messy at this point but the beep comes when the error box opens
Tested it beep comes when the error box opens but thanks for pointing it out
|
|
|
|
|
MacUseless wrote: and point taken about comments being in english.
You're welcome.
MacUseless wrote: Tested it beep comes when the error box opens but thanks for pointing it out
Against my expectations (messagebox blocks the thread, but if it works, it works )
|
|
|
|
|
Hi,
I want to validate the xml file against certain set of rules in database.I have done most of the this but got stuck in below part. Any pointers will be appreciated.
Following is the sample xml file.
<pre lang="xml">
<BACKGROUND>
<QUES_NUMBER>1</QUES_NUMBER>
<QUES_RESPONSE>Y</QUES_RESPONSE>
</BACKGROUND>
<BACKGROUND>
<QUES_NUMBER>2</QUES_NUMBER>
<QUES_RESPONSE>Y</QUES_RESPONSE>
</BACKGROUND>
<BACKGROUND>
<QUES_NUMBER>3</QUES_NUMBER>
<QUES_RESPONSE>Y</QUES_RESPONSE>
</BACKGROUND>
<BACKGROUND>
<QUES_NUMBER>4</QUES_NUMBER>
<QUES_RESPONSE>N</QUES_RESPONSE>
</BACKGROUND>
<BACKGROUND>
<QUES_NUMBER>5</QUES_NUMBER>
<QUES_RESPONSE>Y</QUES_RESPONSE>
</BACKGROUND>
<BACKGROUND>
<QUES_NUMBER>1A</QUES_NUMBER>
<QUES_RESPONSE>Y</QUES_RESPONSE>
</BACKGROUND>
As displayed there are multiple instances of <BACKGROUND><QUES_NUMBER><QUES_RESPONSE></BACKGROUND> tag. Now I have to validate below rule.
Rule 1- If QUES_NUMBER in (1,01,001) Then QUES_RESPONSE should be in (Y,N)
Rule 2- If QUES_RESPONSE Equals (Y) Then one instance of QUES_NUMBER 1A,01A,1B,01B should exist.
Rule 3- If QUES_NUMBER in (1A,01A) Then QUES_RESPONSE (X,Y,Z,Y/Z)
Rule 3.1- If QUES_NUMBER in (1B,01B) Then QUES_RESPONSE (X,Y,Z,Y/Z)
This is the complete Rule. If we are processing rule number 1 and result is true then we have to validate THEN condition (QUES_RESPONSE).
If THEN condition is true then I have to process Rule 2 (as there are multiple instance of same tag) I have to check the QUES_RESPONSE (Y)
of the same tag for which I have validated Rule 1 condition. For that purpose I am passing then index of index of the QUES_NUMBER and it works f9.
PROBLEM occurred in the THEN condition of Rule 2..Where are I am passing the value in xml file and match value (value in database Y,N) and index of the
question number to look at. But the as per then condition 1A..tag should exist and the code will try to locate the tag on that index which he will never
find.
Any pointer where I need to change in logic/any other way will be appreciated.
Below is the function which is validating the value in xml file against db value.
public static bool PROCESSRULE_RANGE(string expValue, ArrayList xmlValColl, out int valIndex)
{
string currentValue = string.Empty;
valIndex = -1;
string[] expValArr = expValue.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
var intersect = expValArr.Intersect(xmlValColl.ToArray());
if (intersect.Count() > 0)
{
valIndex = xmlValColl.IndexOf(intersect.First());
return true;
}
}
I am using XPATH to read the values in xml file.
People Laugh on me Because i am Different but i Laugh on them
Because they all are same.
|
|
|
|
|
What occurs to me is the possibility of having the rules be XPath expressions. For example, rule 1 could be expressed as something like:
//BACKGROUND/QUES_NUMBER[.='1' or .='01' or .='001']/parent::BACKGROUND/QUES_RESPONSE[.='Y' or .='N']
These expressions could then be stored in the database, probably with something to indicate which order to apply them and a value (e.g. 0 or 1) that would indicate a successful test.
Maybe have a branching scheme -- so you can choose the next rules based on the result of the current one.
For instance, the above expression should return one node -- if 0 fail, if 1 goto rule 2, else fail.
However, it will be very tricky to define the rules exactly right.
|
|
|
|
|
Hi,
Thanks for your reply..
Now the problem has been resolved...
Rahul
People Laugh on me Because i am Different but i Laugh on them
Because they all are same.
|
|
|
|