|
Hi,
there recently has been a CP article on sets (with set operations, which you dont want).
What you need is not really a set, it could be any kind of collection.
So try ArrayList (all .NET versions) or a List (if .NET >= 2.0)
|
|
|
|
|
I managed to put something together based on KeyedCollection. It's not perfect, but it get the job done:
<code>
public class Set<T> : KeyedCollection<T, T>
{
public Set()
: base( EqualityComparer<T>.Default, -1 )
{
}
public Set( IEqualityComparer<T> comparer )
: base( comparer, -1 )
{
}
public new void Add( T item )
{
if ( !this.Contains(item ) )
base.Add( item );
}
public void AddRange( IEnumerable<T> enumerable )
{
foreach ( T item in enumerable )
this.Add( item );
}
override protected T GetKeyForItem( T item )
{
return item;
}
}
</code>
|
|
|
|
|
I tried using images in my listview, but it doesn't work o_O
i added an imagelist, made it my listview's imagelist but i still don't see the image when i add an item (yes, i do pick an image index).
any solutions?
|
|
|
|
|
Have you set up Application.EnableVisualStyles by any chance? Try placing Application.DoEvents() immediately after it.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
My question is What is the exact syntax to connect? i mean its our first tym using IBM DB2 server and its kinda hard.
We've connected the DB2 to C# by the components but the exact syntax for opening a connection or closing or inserting an item to the table is a real question...thank you very much...
|
|
|
|
|
Hello, I'm new to attributes. Attributes seem to solve a problem I have with my data representation. However, how are attributes stored in a database?
Specifically, I'm using VS'03 to build a typed data schema, wherein I create and populate some elements. I can use the SqlDataAdapter to load/retrieve from the dB, no problem. Now, I add attributes to the schema, how do I get them into the dB and what is the representation?
Thanks.
NYC Traveler
|
|
|
|
|
I want to make like a schedule program and save the information with XML but im having some problems doing so. this is how i save the information:
<code>
<pre>
string s_FileName = Application.StartupPath + "\\List.xml";
XmlDocument docXML = new XmlDocument();
docXML.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Reminders>" +
"<Reminder>"+
"<Name>"+Add.s_Name+"</Name>"+
"<Date>"+Add.s_Date+"</Date>" +
"<Time>"+Add.s_Time+"</Time>" +
"<Comment>"+Add.s_Comment+"</Comment>"+
"</Reminder>"+
"</Reminders>");
docXML.Save(s_FileName);
</pre>
</code>
The problem is that it overwrites the previous text in the file every time, is there a way to append the text? and is there a better way to save the information? (this is my first time using XML and im realy a newbie o_O).
|
|
|
|
|
sharpiesharpie wrote: The problem is that it overwrites the previous text in the file every time, is there a way to append the text?
If you do not want to overwrite the file, then load the already existing file into the XmlDocument with help of the Load method.
docXML.Load(s_FileName);
Afterwards add all information you want by using the various CreateXX methods. Take a look at their documentation to see what exactly they are doing and how to use them.
sharpiesharpie wrote: and is there a better way to save the information?
If you want to persist a collection of some fix type in a XML file, I suggest the use of the XmlSerializer class. I've written an application for the management of birthdays that makes use of this. You can download the source code here: Birthday Manager[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
wow, the xmlserializer is painfully complicated o_O, is there a simpler alternative? o_O
|
|
|
|
|
What does the XmlSerializer have to do with your original question? I mean, what you're doing is not XML serialization which is maybe why you're finding it hard. To serialize an object graph using actual XML serialization couldn't be any more easy (this is essentially how it works in any framework):
private void Serialize(object obj, Stream s)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer(s, obj);
} You can even customize XML serialization for your classes both declaratively and imperatively. Read Introduction to XML Serialization[^] in the .NET Framework. With attributes, declarative programming makes this way more easy than in other frameworks, like the JDK.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Using the XmlSerializer is not that complicated. I must admit it looks quite complicated in my Birthday Manager cause I'm doing some additional things (validating the XML file before deserialization, insertion of an element with the used ui language). Those things are not ultimately necessary and if you drop them, using the XmlSerializer reduces to creating it and then calling Serialize or Deserialize , respectively.
If you nevertheless do not like it, this should give you a good starting point how to fill your XmlDocument with information: Create New Nodes in the DOM[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Ok, now i just need to know one more thing. i have the Reminder class:
[Serializable]
public sealed class Reminder
{
public string Name;
public string Date;
public string Time;
public string Comment;
}
every time someone makes a new reminder through the adding form, its saved:
string s_FileName = Application.StartupPath + "\\List.xml";
Reminder r = new Reminder();
r.Name = Add.s_Name;
r.Date = Add.s_Date;
r.Time = Add.s_Time;
r.Comment = Add.s_Comment;
XmlSerializer xs = new XmlSerializer(typeof(Reminder));
FileStream fs = new FileStream(s_FileName, FileMode.OpenOrCreate, FileAccess.Write);
xs.Serialize(fs, r);
but now, how do i load it? i tried to do like:
Reminder r = (Reminder)xs.Deserialize(fs);
but then it can only load one reminder. how can i load all of them then?
|
|
|
|
|
First of all, you should declare the fields of your Reminder class as private and make them accessible through properties. It is no good coding practise to have public fields.
public sealed class Reminder
{
public string Name
{
get { return this.name; }
set { this.name= value; }
}
private string name;
...
}
Regarding your actual question, declare a List instance that contains the Reminder objects and then serialize and deserialize that list.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
yes but i add the items one at a time, when a reminder is added it's saved to the xml file and then the list loads it...so if i add it to a list it'll be in a different list therefor won't be loaded along with other reminders wont it?
|
|
|
|
|
sharpiesharpie wrote: when a reminder is added it's saved to the xml file and then the list loads it
Change the order of events. Whenever a reminder is added, it gets stored in the list and afterwards the list is saved (maybe only once when the application shuts down). Deserialization happens only once when you start your application.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
err..yeah..i was afraid i was gonna have to do that.
btw, why would it matter if the variables in the Reminder class are public or not?
|
|
|
|
|
The point is that making fields accessible through properties is much better than simply declaring them public. Some citations from MSDN:
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.
Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields; they can be read-only.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
jeez, i hate being such a pain in the..you know...but now i have another unexplainable problem, this is my saving function:
string s_FileName = Application.StartupPath + "\\List.xml";
FileStream fs = new FileStream(s_FileName, FileMode.Create, FileAccess.Write);
ArrayList al = new ArrayList();
foreach (ListViewItem i in listView1.Items)
{
Reminder r = new Reminder();
r.Name = i.Text;
r.Date = i.SubItems[1].Text;
r.Time = i.SubItems[2].Text;
r.Comment = i.SubItems[3].Text;
al.Add(r);
}
XmlSerializer xs = new XmlSerializer(typeof(ArrayList));
xs.Serialize(fs, al);
when i run it i get a System.InvalidOperationException (the error is on the xs.Seialize line).
why? =\
|
|
|
|
|
Never mind, after going through like 15 pages in google i managed to find a page which said you can't serialize an arraylist, so i converted it to an array of Reminders and it worked.
|
|
|
|
|
Hi all,
I need to make code on button which enables us to change languages of controls in the form
|
|
|
|
|
Set the forms Localizable property to true.
Create a resource file for each language. E.g. Form1.en-GB.resx, Form1.fr-CA.resx. (The existing Form1.resx is the neutral culture resource files)
Use a ComponentResouceManager object to apply the appropriate resources to each control on the form when the button is pressed.
Simon
|
|
|
|
|
Thanx alot for ur reply but now i'm in a new problem
i've created resource files for each language but i'm not able to apply a resourse file from them to the form when pressing on button
|
|
|
|
|
I've written a component that should help you to achieve this. Take a look at its article: UICultureChanger component[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
hi, every one.
I was trying to change an object value of a listbox, I've tried these lines of codes:
listbox1.Text = "hello";
listbox1.SelectedItem = "hello";
but this didn't change the content of the object selected in the listbox, although
that in MSDN they mentioned that these properties have both set and get assessors.
I come up with a solution, remove the object at a specific index using RemovAt(), and
then inserting a new object at the same index using Insert().
but I do want to know if there is any direct way(function, property) to change an object
in listbox.
|
|
|
|
|
Hi, you can do things such as:
lb.Items[2]="new text for line 2";
lb.Items[lb.SelectedIndex]="new text for the selected line";
Luc Pattyn
|
|
|
|