|
Hi Mav,
Thanks, that did the trick !
I have the setup correct in general:
collection class with elements called obj which is an interface and all objects i store in the collection 'derive' from obj.
So, using class objects like:
public class objTeacher : obj
public class objStudent : obj
My problem (major error ) was that i did not 'copy' the properties of a 'general' object to the object i need like your example:
if (o is Teacher)
{
Teacher t = o as Teacher;
string test = t.TeacherName;
t.DoSomethingOnlyTeachersDo();
}
in my case i could not access the propertie TeacherName because it is an object/class specific property.
Casting it as a Teacher object did the trick !
How live can be sweet and simple sometimes
So to understand the logic here, the Teacher is a derivate of the obj interface.
Is this the propper structure to enumerate and use different objects in 1 collection, or am i on a dangerous path here?
Me.
|
|
|
|
|
I think it's the easiest way to go.
In fact you wouldn't even have to derive from obj , because all classes in .NET implicitely inherit object , which is the item type of most base collections.
Using an obj interface might be the cleaner way if you want to ensure type safety at compile time. But then you have to make the collections typesafe (for example by deriving from CollectionBase and overriding the required methods). And find a meaningful interface.
I usually doubt if an empty marker-interface/base class (I think somebody suggested this) is a good idea from an object oriented view. By defining an interface you extract all common properties and behaviour into the interface, but if you can't find anything the different classes implementing your interface have in common then why should you create an interface?
One caveat remains, though. Using object derived items will allow you to add anything to your collection at compile time, even if this might not be desireable. You will not be able to check this until run time.
Regards,
Mav
|
|
|
|
|
I now removed the interface and use object to enumerate,... works perfectly.
Thanks !
|
|
|
|
|
Despite what Microsoft marketing tries to tell you, VB6 is nothing like VB.NET (which is just a language which targets the CLR and uses a common class library). Collections are objects all by their lonesome and you can extend CollectionBase if you want to avoid a lot of the mess but still provide typed parameters (you could override Add , for example, and provide a parameter type as the base class).
You'll still want to go with an abstract object model as everyone here so far has told you. This is something VB couldn't do and is very powerful and leads to clearner designs than prototyping functions like you did in your snippet (having one method to add the base class defined on the collection itself).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks Heath and Mav,
yes VB is a whole different cookie to eat then C#,...noticed )
Will do some reading/investigating regarding the abstract model or go for the mav-version [ArrayList-logic].
Base, Abstract, Virtual, Interface and Derivative classes.
A class collection with a collection of 1 class i got covered.
Mostly i work of examples / snippets but did not find anything regarding various calsses in one collection.
So, welcoming myself to the new world )
|
|
|
|
|
Mav's way, IIRC, uses interfaces. Interfaces and abstract classes differ mostly in the fact that abstract classes can at least contain some functionality. Take the top-most class from which all classes derive: System.Object . It's ToString method is available on any class with default behavior to return the namespace-qualified type name, but classes can override it to return something else. With an interface, each implementing class would have to override it; they don't have a choice. Also, a class can implement multiple interfaces, but can only extend one class.
pxp wrote:
yes VB is a whole different cookie to eat then C#,...noticed
Actually, even VB.NET is different. The syntax is mostly the same, yes, but the output is very different. VB.NET, C#, MC++, and any other managed languages can use assemblies written in any other language because the language compiler for each language compiles to Intermediate Language (IL). Some languages may not support all the features of the Common Language Infrastructure (CLI) and, in many cases, would only be able to use CLS-compliant (Common Language System) types. JScript.NET is a good example.
pxp wrote:
Mostly i work of examples / snippets but did not find anything regarding various calsses in one collection.
Don't think of it as multiple classes but as one class - the abstract class. Sure, in actually the collection contains multiple instances but since each one of those classes derives from the base class (and your collection's methods and properties use that base class).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Various approaches to the same goal, but just what's best shall vary in cases.
The VB logic i brought up was about the same logic. It's collection sonsists of objects. Why did i not know that one can add an object to a collection :P
Thanks for the info.
|
|
|
|
|
I need to create an unknown number of objects, for example DataReaders at runtime. In c# how would I create these names so that the first is dtrFred1 and the next is dtrFred2 etc. Obvisiuosly this will be in the form of a loop. In JScrpit/JavaScript one could use the eval() command, how is this achieved in c#
Robert T Turner
South Gloucestershire Council
|
|
|
|
|
You could hold them in a suitable collection. For instance you can use a Hashtable , where the key is "dtrFred1" etc. and the value is the DataReader.
Does this help?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
|
Hello!
We are developing a program in c#, we have a problem with probably the threading. After a few minutes running our program, it starts to consume all cpu time, we have not started any own threads.
The running thread does not seem to have any priority, it does not slow down any other programs, it just overrides the idle thread, it seems.
Does anyone know of any known problem using c# concering out problem, or has any other ideas of what the problem might be.
M
|
|
|
|
|
Mikke_x wrote:
After a few minutes running our program, it starts to consume all cpu time, we have not started any own threads.
The running thread does not seem to have any priority, it does not slow down any other programs, it just overrides the idle thread, it seems.
The idle thread is the lowest of the low in terms of priority. Of course your application will override it - it is supposed to do that. The idle thread is just what the OS does when it has nothing else to do. As your application is doing something the OS is therefore not, by definition, idle.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Yeah of course. The problem here was that the process used 99% of the processor all the time. Altough it looked like it had really low priority since it did only use the processor if no other thread wanted to execute.
We did resolve the problem however. Can´t really explain the reason behind the problem, but it was because of a sleeping thread. We started a thread when we started the application and suspended it immedediately. Now we have changed it so that we don´t start the thread until we need it instead of starting it and suspend it from the start.
M
|
|
|
|
|
I have made a Treeview, I add only parents.. no child.. I can do that, but the problem is in counting the no. of parents.. I have declared an array, called child[5,100] , I intend to count the no. of parents and associated children. my code for adding parent
string node1;<br />
node1=Convert.ToString("RuleSet No." + (p+1).ToString()); <br />
if(p>=5)<br />
{<br />
MessageBox.Show("YOU CAN ADD ONLY 5 ROOT NODES!!!"); <br />
}<br />
else<br />
{<br />
if (ruletree.Nodes.Count<=0)<br />
{<br />
p++;<br />
this.ruletree.Nodes.Add(node1);<br />
child[p,0]=p;<br />
}<br />
else<br />
{<br />
<br />
int nop=ruletree.Nodes.Count;<br />
p=nop;<br />
child[p,0]=p;<br />
p++;<br />
this.ruletree.Nodes.Add(node1); <br />
}
the peob. is that it counts the second parent as the third.. and hence..
HELP!
_____________________________________________________
Believe! Every thing has a purpose
|
|
|
|
|
Sorry to bother you people.. I got it ..
just a '--' did the job..
sorry
_____________________________________________________
Believe! Every thing has a purpose
|
|
|
|
|
How do I disable the selection of a particular node in a tree..
Eg. I don't want to add children to nodes already children.. I can show a messege box.. or do a no-op if a child is selected, but can I disable its selection?
_____________________________________________________
Believe! Every thing has a purpose
|
|
|
|
|
You can cancel the selection of a node by handling the TreeView.BeforeSelect event:
treeView1.BeforeSelect +=
new TreeViewCancelEventHandler(treeView1_BeforeSelect);
private void treeView1_BeforeSelect(object sender,
TreeViewCancelEventArgs e)
{
if (e.Node.Nodes.Count > 0) e.Cancel = true;
} If you want to have more control over what people can add, this is where encapsulation comes in. Rather than trying to hack the TreeView to allow/disallow certain operations, host the TreeView in a UserControl or something where you define methods and properties that reflect operations in the TreeView . This way you can easily perform a check before doing something, like how many nodes exist and whether you should add a new one. To implement this kind of behavior on the tree can be quite difficult. This is the reason for encapsulation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok..
I'll try this.. thank you.
_____________________________________________________
Believe! Every thing has a purpose
|
|
|
|
|
Hello,
I'm having a problem to read data from my bluetooth gps mouse!
In detail: I'm trying to program a little tool for an IPaq 5450 PPC 2003 which reads the gps data that comes in from the virtual com port (in my case com port 8). That is where my problem begins. I'm pretty new with c# and serial port communication at all! So i hope anyone of you can help me with that problem! How do i open the serial com port in c# and start reading out of a buffer!
I just need the simple geographical coordinates. Not more!
And please no recommendation to use another language... i have to use c#, because it's part of a bigger project!
hope someone can help me...
nils
|
|
|
|
|
.NET 1.x does not include a serial stream reader/writer class; .NET 2.0 will (finally). There are several classes available that people have written to get around this problem. If you try the following search, you'll find several hits:
http://www.codeproject.com/info/search.asp?cats=3&cats=5&searchkw=serial+com+port[^]
As far as using another language (because you're easiest way to talk to a serial port is using Managed C++), you do realize that you can write assemblies using any managed language, don't you? You could, for example, write an assembly in Managed C++ (typical when many native functions and supporting data types need to be used) and reference that in your C# project just like you reference System.dll, System.Data.dll, etc. The .NET BCL assemblies are written in C# yet they're used by all other managed languages (MC++, VB.NET, Perl.NET, COBOL.NET, etc.).
Use what makes it the easiest.
If you want additional help for reading NMEA data from your GPS unit, try the following google search:
http://www.google.com/search?q=GPS+C%23[^]
There are many great examples and even some third-party libraries which could do all this for you.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Have a look at GPS Reader NMEA 0-183[^]
He uses a DBComm dll.
Also nice way of showing sat's on screen.
Good starting point,...
|
|
|
|
|
well, looks like what i'm looking for! but do i need to write the dbcomm.dll on my own or can i download it somewhere. so far i wasn't able to find it!
nils
|
|
|
|
|
|
I'm using this code (from a MS example) to encrypt/decrypt simple strings. These methods work fine while I maintain an instance of the class but if I encrypt a string, then later want to decrypt it (using the same key of course)
the first 5 characters or so are messed up.
<br />
public string EncryptString(string strValue)<br />
{<br />
SymmetricAlgorithm m_csp = new TripleDESCryptoServiceProvider();<br />
ICryptoTransform ct = m_csp.CreateEncryptor(m_csp.Key, m_csp.IV);<br />
byte[] byteBuf = Encoding.UTF8.GetBytes(strValue.PadLeft(5, '*'));<br />
MemoryStream ms = new MemoryStream();<br />
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);<br />
cs.Write(byteBuf, 0, byteBuf.Length);<br />
cs.FlushFinalBlock();<br />
cs.Close();<br />
<br />
return Convert.ToBase64String(ms.ToArray());<br />
}<br />
<br />
public string DecryptString(string strValue)<br />
{<br />
SymmetricAlgorithm m_csp = new TripleDESCryptoServiceProvider();<br />
ICryptoTransform ct = m_csp.CreateDecryptor(m_csp.Key, m_csp.IV);<br />
byte[] byteBuf = Convert.FromBase64String(strValue.Trim());<br />
MemoryStream ms = new MemoryStream();<br />
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);<br />
cs.Write(byteBuf, 0, byteBuf.Length);<br />
cs.FlushFinalBlock();<br />
cs.Close();<br />
<br />
return Encoding.UTF8.GetString(ms.ToArray());<br />
}<br />
<br />
|
|
|
|
|
Something obviously is stored in that class that you're not saving (perhaps the key, which in these snippets is getting created each time, even between encryption and decryption calls).
How are you saving the ciphertext across calls and instances of the containing class? Are you writing them to a file? If you use a TextWriter , you may be inadvertently writing a BOM (byte order mark) to the beginning of the file. Depending on how you read the file back in, this BOM may become part of the ciphertext (a TextReader should strip this, but if you use a FileStream or something, it won't be stripped from the rest of the ciphertext, for example).
The only thing with block ciphers is, though, that if something is screwed up then the ciphertext would be invalid. The only possibility for this not to happen as I see it is if a particular block of ciphertext were changed and the block data size wasn't changed, and this may only work (depending on the algorithm) if the data wasn't signed, either (because the digest would be invalid). If this is possible (depending on the algorithm), then the rest of the blocks should decrypt.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|