|
What is "bgworker" and why can't you pass it a string?
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
Well you can pass parameters. The RunWorkerAsync can take an object parameter which will be transferred to the event arguments of the DoWork event callback. You could argue that it is only one parameter and that you need several ones but this is not really a restriction because you can stuff anything you like into that parameter like an ArrayList filled with values, a Hashtable or plain an object array.
using (BackgroundWorker bw = new BackgroundWorker()) {
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync(new object[] { tempDestination, tempRemoveable, ExtensionFlag});
bw.DoWork -= new DoWorkEventHandler(bw_DoWork);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
object[] o = (object[])e.Argument
CopyFiles cf = new CopyFiles((string)o[0], (string)o[1], (string)o[2]);
}
Not tested but should work
|
|
|
|
|
hi
i'm making a form in VS.Net .When i debug the code,i want my textbox come with grids.
But i dont know how i drawing grids to the textbox and show it when the form run.That is in one way,i want to use InitializeComponent() method.
how can i do it
thanks
|
|
|
|
|
You cannot use grid in textbox. Use DataGrid instead.
It is simple to use datagrid. Just put datagrid in your
form. Then fill datagrid using datatable.
e.g. :
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("X", typeof(int));
dataTable.Columns.Add(new DataColumn("Y", typeof(int));
for(int i = 0; i < 10; i++)
{
DataRow dataRow = dataTable.NewRow();
dataRow["X"] = i;
dataRow["Y"] = i;
dataTable.Rows.Add(dataRow);
}
this.dataGrid1.DataSource = dataTable; Now you can see a table with X and Y column, fill with number 0 until 10.
|
|
|
|
|
i will study about this , thank you very much stancrm;)
|
|
|
|
|
If I've got a WeakReference:
private WeakReferences weakRef = new WeakReference();
...
weakRef.Target = new string[...];
Am I guaranteed the following will always work?
if(weakRef.IsAlive)
{
string[] args = (string[])weakRef.Target;
}
Isn't it possible that the garbage collector will kick in right after the IsAlive check, thus collecting the object before I can get the Target?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Islamic Domination: Coming to a Jewish state near you!
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
|
|
Use the IsAlive method if you only want to check the status of the reference. If you actually want to use the reference there is no reason to use IsAlive first, just get the value from the Target property and check if it's null.
---
b { font-weight: normal; }
|
|
|
|
|
Hello, i would like to know how to sort an xml file. here is the xml file i have:
<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>
I don't have any template or style file.
I want to display a sorted information inside a listbox in C#:
Advanced XML, Lucerne Publishing
XML for Gurus, Lucerne Publishing
Developing XML solutions, MSPress
Learn XML Today, MSPress
I assume that we want to sort by the publisher first and then by the title.
Any suggestion will be great.
Donkaiser
|
|
|
|
|
private static void SortXmlTest()
{
string data = @"<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
ArrayList books = new ArrayList();
foreach (XmlElement book in doc.DocumentElement.ChildNodes)
books.Add(book);
books.Sort(new BookComparer());
doc.DocumentElement.RemoveAll();
foreach (XmlElement book in books)
doc.DocumentElement.AppendChild(book);
doc.Save(Console.Out);
}
class BookComparer : IComparer
{
public int Compare(object x, object y)
{
//return (x as XmlElement)["Title"].InnerText.CompareTo((y as XmlElement)["Title"].InnerText);
int publisherCompare = (x as XmlElement)["Publisher"].InnerText.CompareTo((y as XmlElement)["Publisher"].InnerText);
if (publisherCompare != 0)
return publisherCompare;
return (x as XmlElement)["Title"].InnerText.CompareTo((y as XmlElement)["Title"].InnerText);
}
}
I hope I didn't just do all your homework.
|
|
|
|
|
Hi Reinux,
Thank you for your help. im still new to XML programming.
I was compiling ur code and it gave me this error in vs 2005:
"Using the generic type 'System.Collections.Generic.IComparer<t>' requires '1' type arguments"
I've been looking around and i didn't see any implementation of the parameter.
Thx again
Donkaiser
|
|
|
|
|
Ah, that was actually .NET 1.1 code. You can still make it work by adding
"using System.Collections" to the top of your code, but here's the .NET 2.0 version (you don't need BookComparer):
private static void SortXmlTest()
{
string data = @"<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>";
//Create a new document and load the sample data.
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
//Copy all of the Book elements to a list.
List<XmlElement> books = new List<XmlElement>();
foreach (XmlElement book in doc.DocumentElement.ChildNodes)
books.Add(book);
//Sort the data -- if the publishers are the same, sort by title.
books.Sort(delegate(XmlElement x, XmlElement y)
{
int publisherCompare = x["Publisher"].InnerText.CompareTo(y["Publisher"].InnerText);
if (publisherCompare != 0)
return publisherCompare;
return x["Title"].InnerText.CompareTo(y["Title"].InnerText);
});
//Remove all the Book elements from the document, and put the sorted copy back in.
doc.DocumentElement.RemoveAll();
foreach (XmlElement book in books)
doc.DocumentElement.AppendChild(book);
//Print the data to the console.
doc.Save(Console.Out);
}
|
|
|
|
|
uraaa! I got it working and listed on a textbox. If i listed only the titles of the books in the listbox. when i select an item an click an "ok button". How can a retrieve the matching publisher?
Thanks a lot again
Donkaiser
|
|
|
|
|
Practically every control in Windows Forms has a handy Tag property that you can assign what ever you want to.
So you can assign those book XmlElement items that you extracted earlier to each one of their corresponding list items.
And then when you click an ok button, check the listbox.SelectedItem.Tag property -- that'll be whatever you assigned to the tag.
Edit: sorry, that's ListView I'm thinking of.
ListBox lets you assign any type of object, so you can make a custom class and override the ToString method to have it show the right text in the list box.
So make a class like this:
class Book
{
public Book(string name, string publisher)
{
this.name = name;
this.publisher = publisher;
}
string name, publisher;
public string Publisher
{
get { return publisher; }
}
public string Name
{
get { return name; }
}
public override string ToString()
{
//Just show the name
return name;
}
}
When you add an item to the list box, do something like this:
listBox.Add(new Book(element["Name"], element["Publisher"]);
When you want to get the publisher for the selected book, do this:
publisher = (listBox.SelectedItem as Book).Publisher;
-- modified at 19:59 Friday 19th May, 2006
|
|
|
|
|
I am writing some code in C# and have the need to call a third party java api. I am attempting to write a C++ dll that can be called from
C# -> C++ -> Java -> Java API.
Everytime I make a call to JNI_CreateJavaVM from Java I get a JNI_ERR or -1
I do have a java class file in the same directory.
I also have the location of jvm.dll in the system path variable.
Also, if I run the C++ code as a stand-alone executable instead of a DLL it works.
Any Help would be appreciated.
Thanks
JNIEnv *env;
JavaVM *jvm;
jint res;
JavaVMInitArgs vm_args;
avaVMOption options[1];
options[0].optionString = "-Djava.class.path=C: \\CsstProductInterface.jar";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if(res < 0)
{
AddToLog("UNABLE to start JVM\n\n");
}
Pat
|
|
|
|
|
I have a piece of code in which collection is assigned to dropdown list's data source property for binding data. When multiple clients access the site simultaniously the following exception was being thrown:"Collection was modified; Enumeration Operation may not execute". Any idea about y this is happening and how to get this code work???
|
|
|
|
|
Is not allowed to modify any item of a collection in any way while you're inside the foreach loop on that collection.
You may try to lock the piece of code, something like the following
lock(this) {
...
}
paco
|
|
|
|
|
Thank you carlopagliei for the reply; but i am not using the foreach statement. I have a code like
ddlTest.DataSource= TestCollection;
ddlTest.DataBind();
Otherthan populating the collection from database, i am not manipulating the collection.
|
|
|
|
|
It was only an example. You don't use foreach but probably the problem is that while DataBind() method enumerates over the TestCollection another piece of code (maybe the same but from another thread) is doing the same. So try to lock the code that acess TestCollection before use it:
lock (TestCollection) {
ddlTest.DataSource= TestCollection;
ddlTest.DataBind();
}
paco
|
|
|
|
|
Hi.
when we create a class, and in code type 'ClassName.', some methods and properties appear, and on an object of that class there is another properties and methods.
first : we don't write them for that class, these properties and methods come from where?
second : for class and object these are difference, why?(Is it for static methods and properties?)
Best wishes
|
|
|
|
|
mehrdadc48 wrote: first : we don't write them for that class, these properties and methods come from where?
Are you referring to methods such as ToString()? They would be inherited from the base class (in this case, System.Object).
mehrdadc48 wrote: second : for class and object these are difference, why?(Is it for static methods and properties?)
You are correct. You cannot call static methods using an object instance.
Cheers,
Vikram.
I don't know and you don't either.
Militant Agnostic
|
|
|
|
|
Typing "Classname." invokes intellisense which lists the properties and methods of that class or object so as to ease the work of a prorgammer so he/she does not have to remember all the properties and methods of the Dot Net Framework. There are many classes built into the Dot Net Framework, many of which wrap commonly used functionality that already exists in Windows. In summary, these classes and intellisense are there to make a programmer more productive and are part of the Dot Net Framework installed on your computer.
As for the difference between the methods displayed by Classes and Objects; Yes you are right that for Classes, only static methods are displayed unlike Objects.
|
|
|
|
|
Hi. I have a rss, and I want to remove the items that its pubDate are lower than a DateTime. The problem is that I can't use the function to compare date in XPath because the pubDate use the RFC-822 format and the function in XML/XSD/XSLT, use ISO-8601 format. So I have to use an external function to remove the nodes like this:
public static XmlDocument XMLFilter(XmlDocument doc, DateTime aDate)
XPathNavigator nav = doc.CreateNavigator();
DateTime nodeDate;
foreach (XPathNavigator node in nav.Select("rss/channel/item/pubDate")) {
nodeDate = DateTime.Parse(node.Value).ToLocalTime();
if (nodeDate.CompareTo(aDate.ToLocalTime()) < 0) {
node.DeleteSelf();
}
}
return doc;
}
But If I return the doc, the element are not removed. I want to remove that node (where I compare the condition node.DeleteSelf())
Thanks a lot !!!!!
|
|
|
|
|
Heyyy, I found the way !!! . Thanks anyway people !! May be this solution could help somebody, like me !!
public static XmlDocument XMLFilter(XmlDocument doc, DateTime aDate)
{
DateTime nodeDate;
XmlNode nodeChannel = doc.SelectSingleNode("rss/channel");
XmlNodeList pudDateNodeList = doc.SelectNodes("rss/channel/item/pubDate");
foreach (XmlNode node in pudDateNodeList){
nodeDate = DateTime.Parse(node.InnerText).ToLocalTime();
if (nodeDate.CompareTo(aDate.ToLocalTime()) < 0) {
nodoChannel.RemoveChild(node.ParentNode);
}
}
return documento;
}
|
|
|
|