|
hi.
I have a treeview that has a context menu associated with it. When I run the application and select a node then right click on another node, the context menu appears, but since the node isn't "selected" (the focus reticle is on the left-clicked node), the contextmenu doesn't work right since it performs it's actions on treeview1.SelectedNode.
I hope I have explained myself cleary. I am looking for a way to update selectednode and move the focus reticle to the node where the context menu was popped up.
|
|
|
|
|
|
worked like a charm. thanks
|
|
|
|
|
what scewey logic is c# using for scope?
why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System;
using System.Collections;
public class Test {
public static int Main(String[] args) {
ArrayList b = new ArrayList();
ArrayList c = new ArrayList();
for( int i = 0; i < b.Count; i++ ) {
if( true ) {
IEnumerator lenum = b.GetEnumerator();
while( lenum.MoveNext() ) {
int j = 0;
j++;
}
}
IEnumerator lenum = c.GetEnumerator();
while( lenum.MoveNext() ) {
int j = 0;
j++;
}
}
return 0;
}
}
|
|
|
|
|
My guess is that the C# compile knows that the if( true ) block will ALWAYS be executed and optimizes the if statement out. The result is that lenum = b.GetEnumerator() and lenum = c.GetEnumerator() are in the same scope.
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
i just did that as a short example. my actual code where i found that oddity does the same thing.
|
|
|
|
|
This is no different to C. Just brace it.
public class Test
{
public static int Main(String[] args)
{
ArrayList b = new ArrayList();
ArrayList c = new ArrayList();
if( true )
{
IEnumerator lenum = b.GetEnumerator();
while( lenum.MoveNext() )
{
int j = 0;
j++;
}
}
{
IEnumerator lenum = c.GetEnumerator();
while( lenum.MoveNext() )
{
int j = 0;
j++;
}
}
return 0;
}
}
leppie::AllocCPArticle("Zee blog");
|
|
|
|
|
The scopes of the two lenum variables overlap whereas those of the j variables do not. Read section 5.1.7 of the C# language specification.
Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes its variable-declarator.
However...
The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way.
These two statements together seem to indicate that the compiler is doing what it is suppose to. I wonder why the scope of a variable starts at the entry point of the block and not at the declaration point? Afterall, if you can't reference the variable in a textual position preceding the declaration then why not define its scope in the same manner? I'm sure there's a good answer. I just don't know what it is
Brian
Brian
|
|
|
|
|
so its not a bug because they say its feature. nice.
any other decent language would let this code compile.
oh I'll just add it to my list of screwed up things c# does
|
|
|
|
|
So C is not decent. Get a life or go back to whatever decent language you were using previously.
PS: did you even read my reply?
PSS: using a foreach loop allows you to do that.
PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:
IEnumerable nodes;
....
for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
{
object node = enumer.Current;
...
}
leppie::AllocCPArticle("Zee blog");
|
|
|
|
|
leppie wrote:
So C is not decent. Get a life or go back to whatever decent language you were using previously.
you would think a made i made a comment about your mother.
PS: did you even read my reply?
i did read your reply but you didn't explain the problem you just posted a work around.
PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:
incorrectly ? there is very little difference between the two and in my opinion the while loop is more readably, you don't have an empty statement as in your for loop.
what's the deal anyways. i make a couple of comments about the quirks in the language( every language has them ) and you go off on me like i declare C# as the worst language in the world and every programmer who uses is stupid.
now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.
|
|
|
|
|
jpribele wrote:
now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.
I think I have misunderstood too. (rough week/boring weekend) I apologize. From the post below by Rich333 (pnet developer) he clarified it.
It appears to be a EMCA spec interpretation problem.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I think you may have misunderstood what the point of the original post was. It was a question about how C# handles scoping. It is a very legitimate and intelligent question. The author was not looking for a work-around.
Brian
|
|
|
|
|
You could try using a compiler that doesn't punish good programmers for using perfectly legitimate code. Technically, there is no conflict so long as the outer scope lenum comes after the inner scope one. MS's csc and Mono's mcs both use this buggy scoping, however DotGNU Portable.NET's cscc allows this code (as well it should) since there is no conflict. It does reject code where the outer scope variable is declared before the inner scope variable, however (since in that case there is an actual conflict).
Rich
P.S. For more info on DotGNU check out: http://www.dotgnu.org/
|
|
|
|
|
Here's the deal: I have created an XSD file that relates back to an XML data file. The XML data is EMPTY (no data), only the XML Header, DataSet name and reference to the XSD file comprise the contents of the XML (see below):
<?xml version="1.0" encoding="utf-8" ?>
<MyDataSet> xmlns="http://tempuri.org/MyDataFile.xsd">
</MyDataSet>
I load this XML file into a DataSet object using ReadXML() with the ReadSchema parm. While the XML data file is empty, this read statement succeeds just fine; expected this, it's all good.
Now, I want to add a 'record' into the XML file, so I need to create a new DataTable, but I want to use the schema that's already defined in the XSD for my table (makes sense). In my schema (XSD) file, this record is defined as MyDataRecord (if I view the Schema in designer, I can see the datastructure just fine).
Becase the XML file is empty, doing something like:
DataTable dt = MyDataSet.Tables["MyDataRecord"];
only returns NULL
I thought, perhaps, I could do the following:
DataTable dt = MyDataSet.Tables.Add();
and it would be "smart enough" to create a DataTable object using the schema datastructure, but it does not ...
I need to know how to create a DataTable object of type "MyDataRecord " so I have the complete data structure ...
There has GOT to be a simple way to do this, someone please enlighten me.
TIA.
D.
|
|
|
|
|
How do you load your Dataset?It should be like this:
XmlDataDocument doc = new XmlDataDocument();
doc.DataSet.ReadXmlSchema("schema.xsd");
doc.Load("mydataset.xml");
After this you can get your tables in dataset.
Mazy
No sig. available now.
|
|
|
|
|
I create a DataSet and use ReadXML("mydatafile.xml", XmlReadMode.ReadSchema);
It's loading the schema just fine, but as I stated, since the XML file is empty, essentially the DataSet contains no valid tables in it's Table Collection.
So how do I go about creating a DataTable that has the datastucture defined in the schema?
D.
|
|
|
|
|
|
Ok,
what you suggested did not work, since a DataAdapter in itself is an Abstract class, but it reminded me of an example I downloaded a while back that used a 'pre-canned' xml/xsd file ...
The only difference between that example, and my code, is that they have you drop a DataSet object on your Window in the Form Designer window and bind it to a "typed dataset" ... which I did ...
Now this seems to be working(?)
I'm not sure what the difference is between creating a DataSet object programmically and attaching it to my schema and what's taking place when I do this visually ...
Obviously something is being 'set' somewhere that either I am missing, or doing wrong.
Perhaps someone knows the answer to this question? I'd still like to know how to make this all happen in code.
D.
|
|
|
|
|
D,
If you want to peek under the hood, maybe taking a look to .cs file generated by your .xsd may shed some light on the issue.
On the other hand, if you want to test the FillSchema solution, use a SqlDataAdapter, I gave you the wrong link
eperales
|
|
|
|
|
Hey,
I checked the CS files in the generated code section, but I'll be #%&* if I can figure out what the differences were/are, between the generated code and what I had done programmically ... obviously there's something because it works now.
Thank you for the information and taking the time to respond.
D.
|
|
|
|
|
Is there an easy way to clone a DataTable?
*->>Always working on my game, teach me
*->>something new.
cout << "dav1d\n";
|
|
|
|
|
DataRow[] sourceRows=sourceTable.Select();
DataTable targetTable=sourceTable.Clone();
foreach(DataRow sourceRow in sourceRows)
targetTable.ImportRow(sourceRow);
eperales
|
|
|
|
|
what is s1ck about that is that I just clone the table without its rows; all I need is the schema.
Thanks E.
*->>Always working on my game, teach me
*->>something new.
cout << "dav1d\n";
|
|
|
|
|
DataSet ds;<br />
ds.Clone();
From MSDN:
DataSet.Clone Method
Copies the structure of the DataSet, including all DataTable schemas, relations, and constraints. Does not copy any data.
[C#]
[Serializable]
public virtual DataSet Clone();
Return Value
A new DataSet with the same schema as the current DataSet, but none of the data.
|
|
|
|