|
Clever, but you don't avoid the unfortunate cast to and from object without the use of generics...
I've been working on a generic KeyedList, a kind of "brother" to the KeyedCollection, but for use with multible keys.
It's used like this
<code>
KeyedList<string, int, MyObject> keyedlist = new KeyedList<string, int, MyObject>();
keyedlist.Add(new MyObject("kvalue1",123, "actual content"));
keyedlist.Add(new MyObject("kvalue2",123, "actual content"));
// etc...
string key1 = "kvalue1";
int key2 = 123;
if(keyedlist.Contains(key1, key2);
{
MyObject obj = keyedlist[key1, key2];
// etc..
}
</code>
but I had make a new class for each number of keytypes for a coollection...
For 1, 2 and 3 keys the outcome was:
<code>
class KeyedList<TKey1, TObject>
{
}
class KeyedList<TKey1, TKey2, TObject>
{
}
class KeyedList<TKey1, TKey2, TKey3, TObject>
{
}
// etc...
</code>
I was hoping to avoid that ...
My dreams was that you could declare n-number of TKey types in a generic class....
Something like this:
<code>
class KeyedList<TKey[], TObject>
{
void Somemethod()
{
foreach(TKey keytype in TKey[])
{
// etc...
}
}
}
// so you could use it like this
KeyedList<int, int, Guid, MyObject> list = new ...
// or
KeyedList<string, MyObject> list = new ...
// with out having to make new class declare for each ....
</code>
Maybe in the next C# version...
|
|
|
|
|
I don't think the key types matter as much as the value type, generally they go in but don't come out.
Because I was aiming for max flexibility I didn't provide the ability to specify the value type, but it could be done.
|
|
|
|
|
I added the ability to specify the value type and the key type when desired.
namespace Test
{
class Test
{
[System.STAThreadAttribute]
static void
Main
(
string[] args
)
{
try
{
PIEBALD.Types.Hierarchary<string> store =
new PIEBALD.Types.Hierarchary<string>
(
typeof(int)
,
typeof(int)
,
typeof(string)
) ;
store [ 1234 , 1 , "Property1" ] = "1234 1 1" ;
store [ 1234 , 1 , "Property2" ] = "1234 1 2" ;
store [ 1234 , 2 , "Property1" ] = "1234 2 1" ;
store [ 1234 , 2 , "Property2" ] = "1234 2 2" ;
store [ 1237 , 1 , "Property1" ] = "1237 1 1" ;
store [ 1237 , 1 , "Property2" ] = "1237 1 2" ;
System.Console.WriteLine ( "ProductId (key) InventId (key) Property1 (value) Property2 (value)" ) ;
foreach ( int k1 in store.Keys<int>() )
{
foreach ( int k2 in store.Keys<int> ( k1 ) )
{
System.Console.WriteLine
(
"{0,-17} {1,-16} {2,-20} {3,-20}"
,
k1
,
k2
,
store [ k1 , k2 , "Property1" ]
,
store [ k1 , k2 , "Property2" ]
) ;
}
}
}
catch ( System.Exception err )
{
System.Console.WriteLine ( err.Message ) ;
}
return ;
}
}
}
|
|
|
|
|
I have a method which is passed an Exception.
I'd like to find out which Exception it is (ie, was it ArgumentNullException, was it a COM Exception, etc).
How do I address this? I don't have control of the original Try/Catch statement in this method, it just receives an Exception which is passed, not thrown.
|
|
|
|
|
if(ex is ArgumentNullException)
{
MessageBox.Show("Thau shall not nullify");
}
fafafa, ringakta icke sådant som bringa ack så naggande högönsklig välmåga å baronens ära.
|
|
|
|
|
Thanks, that was exactly what I was looking for - what comparator to use. I was trying "==" and getting nowhere. Sounds like "is" is the way to go.
I was also just casting from the general exception to the specific exception and seeing what happened. I was not getting much info there .
|
|
|
|
|
How can I access the specific fields and properties?
public string GetErrorCode(Exception ex)<br />
{<br />
if (ex is COMException)<br />
{<br />
return ex.ErrorCode.ToString();<br />
}<br />
else<br />
{<br />
return "";<br />
}<br />
}
This doesn't work, the compiler complains that System.Exception doesn't contain a definition for ErrorCode.
I tried casting ex using
ex = (COMException)ex
but it fails the same way.
|
|
|
|
|
Hello,
You have to cast it to the Type!
if (ex is COMException)
{
COMException comEx = ex as COMException
return comEx.ErrorCode.ToString();
}
or
COMException comEx = ex as COMException
if(comEx !=null)
{
return comEx.ErrorCode.ToString();
}
All the best,
Martin
|
|
|
|
|
Interesting, I used a direct cast (as in my example posted further down) instead of "as", and it appears to work fine. Any caveats there I should know about?
I'm not that familiar with "is" and "as", clearly. Perhaps I should be...
|
|
|
|
|
Hello,
In you example you instanciated a object of COMException bevore casting,
which is not needed at all!
The advantage of using "is" or/and "as" is the check of the complete inheritance tree!
Additionaly you have the possibility (like I showed in the second example) to use "as" without doing the TypeCheck bevor, and not have to have the "fear" of an Exception thrown because of invalid type.
All the best,
Martin
|
|
|
|
|
Right, I would write that first example as:
if (ex is COMException)
{
return ((COMException)ex).ErrorCode.ToString();
}
"Omit needless local variables." -- (To modernize Strunk)
|
|
|
|
|
Got it.
I had to create a new COMException, and cast into that.
COMException COMex = new COMException();<br />
<br />
COMex = (COMException)ex;
then I was fine.
|
|
|
|
|
JoeRip wrote: I had to create a new COMException
No, you didn't. this would have worked just fine:
COMException COMex = (COMException)ex;
The reason it failed initially is that you tried to reuse the ex variable which was already defined as Exception:
ex = (COMException)ex;
You should definately read up on the C# is and as keywords as they are better suited to what you are trying to do.
|
|
|
|
|
Yeah, Martin set me straight. Is and As are exactly what I needed, very helpful. I've rewritten the section using Is and As, it all works excellently.
|
|
|
|
|
hi every body i need ur help .. i am doing program in windows appplication using C# code .so i created one combo box and i populate data from database using ds . then so i dont want 2 edit in seleteditem in combobox..plzz tell me wht should i do now.plz ..help me...
thanks 2 all..
|
|
|
|
|
First of all, use proper English when posting your question. Secondly, if you set DropDownStyle property to ComboBoxStyle.DropDownList , users won't be able to change the text of current item. However they will able to select another item from the drop down list
|
|
|
|
|
brother thanks its working.... next time ii ll use proper eng ..ok bye thanks..
thanks 2 all..
|
|
|
|
|
Hi all,
I have found the following code on how to fill a tree view control with one's directory structure, but it is in VB. Can anyone please help me to translate into C#?
' In general Declarations
Dim tvn As Node
Private Sub Command1_Click()
Me.MousePointer = vbHourglass
TreeView1.Nodes.Clear
' Pathname to create folder/file list from
p$ = Dir1.Path
If Right$(p$, 1) <> "\" Then p$ = p$ + "\"
Set tvn = TreeView1.Nodes.Add(, tvwParent, p$, p$)
RecurseFiles2 p$
Me.MousePointer = vbDefault
End Sub
Sub RecurseFiles2(ByVal fPath As String)
Dim File_Name As String
Dim File_Read As Integer ' Number of Files Read
Dim strTempPath As String
Dim i As Integer
If Right$(fPath, 1) <> "\" Then fPath = fPath & "\"
' to do a pattern match do, or something.
' folders won`t be included in the list (not my fault)
' File_Name = Dir$(fPath+"\*.exe", vbDirectory)
File_Name = Dir$(fPath, vbDirectory)
File_Read = 1
Do While File_Name <> ""
If File_Name <> "." And File_Name <> ".." Then
strTempPath = fPath & File_Name
If GetAttr(strTempPath) And vbDirectory Then
Set tvn = TreeView1.Nodes.Add(fPath, tvwChild, strTempPath + "\", File_Name)
RecurseFiles2 strTempPath ' if a folder, then call this routine to scan that folder (recursion)
File_Name = Dir$(fPath, vbDirectory)
For i = 2 To File_Read
File_Name = Dir$
Next
Else
Set tvn = TreeView1.Nodes.Add(fPath, tvwChild, strTempPath, File_Name)
End If
End If
File_Name = Dir$
File_Read = File_Read + 1
Loop
End Sub
Many Thanks
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
There are code converters available on the web that will do this. I suggest you use one of those.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Thanks for the reply pmarfleet. Do you know of any good ones?
Thanks again ....
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
I don't use code converters - I don't have any need for them. However a Google search threw up the following results[^].
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
|
|
Hello...
Can some body help me with Timer?
I want to display the data from my database. For an example:
i) 1st data will be displayed ( 10 seconds)
ii)After 10 seconds finish, 2nd data will be displayed
iii) These data will be displayed continuously (looping)
Please help me.
Thanks in advance
Jac
|
|
|
|
|
where do you and which way do you want show the data?
you can read the data at a time and simply use a timer to display on time
|
|
|
|
|