|
Scott Dorman wrote: Defining the member in an interface doesn't imply that it is virtual at all.
Aha! This is the root of my confusion. I was assuming that a C# interface was the same as a C++ interface (i.e. a collection of pure virtual/abstract methods). I remembered reading in the O'Reilly "C# Essentials" book that a C# interface was simply a syntactic shortcut for a bunch of abstract members in an abstract class but their exact wording was "similar to"
The output from the following code gave me a surprise:
interface I
{
void foo( string msg ) ;
}
class A : I
{
public void foo( string msg ) { System.Console.WriteLine( msg+" -> A::foo" ) ; }
}
class B : A
{
public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}
static void Main()
{
B b = new B() ;
((I)b).foo( "Static type I" ) ;
((A)b).foo( "Static type A" ) ;
((B)b).foo( "Static type B" ) ;
}
--- OUTPUT ---
Static type I -> A::foo
Static type A -> A::foo
Static type B -> B::foo
In C++, the B::foo() method would've been called every time.
No wonder I thought foreach appeared to be ignoring the IEnumerable's in the inheritance hierarchy. In every other OO language I've worked with, to get access to GetEnumerator() you would have to go through the IEnumerable but in C#, deriving from an interface seems to be little more than a directive to the compiler telling it that certain methods need to defined i.e. it doesn't really affect the class as such (e.g. by causing the layout of the v-table to change, or whatever C# uses).
Playing around a little with the definition of B:
class B : A,I
{
void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}
--- OUTPUT ---
Static type I -> A::foo
Static type A -> A::foo
Static type B -> A::foo
That's weird, but I think because B::foo() is not public:
class B : A,I
{
public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}
--- OUTPUT ---
Static type I -> B::foo
Static type A -> A::foo
Static type B -> B::foo
And how about these two:
class B : A,I
{
void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
void I.foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo 2" ) ; }
}
--- OUTPUT ---
Static type I -> B::foo 2
Static type A -> A::foo
Static type B -> A::foo
class B : A,I
{
public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
void I.foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo 2" ) ; }
}
--- OUTPUT ---
Static type I -> B::foo 2
Static type A -> A::foo
Static type B -> B::foo
I think I get it
Scott Dorman wrote: I am explicitly implementing the IEnumerable.GetEnumerator member. In the calling code, it will default to using the implicit implementation (the public IEnumerator<int> GetEnumerator()) because the call isn't being cast to IEnumerable.
Ah, so by writing IEnumerator IEnumerable.GetEnumerator() { ... } you are saying that this method is only available if your static type (i.e. at compile time) is IEnumerable. I was going to say this seems to break polymorphism but that doesn't apply if the method is not virtual.
Scott Dorman wrote: hopefuly my answers above make it clearer as to why declaring one of the methods public makes a difference.
This I still don't quite get. You were refering to the two GetEnumerator methods but something still seems intuitively wrong: they are implicitly public, because that's how they are defined in their respective interfaces, so explicitly declaring one as public should be redundant but somehow, it actually changes how things work It certainly did when I was playing around with the definition of B above.
But I'm heartily sick of this, as I'm sure you are. Thanks again for the help
I enjoy occasionally wandering around randomly, and often find that when I do so, I get to where I wanted to be [^].
Awasu 2.3.2 [^]: A free RSS/Atom feed reader with support for Code Project.
|
|
|
|
|
So I have been working on teaching myself XNA game programming using C#. Next quarter I have a free space in my schedule finally and was considering taking a programming class. My school doesn't offer C# so my question is would java or C++ be more help for me when it comes to c# programming? Hope that makes sense.
|
|
|
|
|
Hi,
Java and C# are very similar languages. I had several years of Java experience when I switched
to C#, where the new things were merely .NET related (that is the base classes, as used
in C#, VB.NET and C++/CLI but not in unmanaged C++).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
keisal wrote: would java or C++ be more help for me when it comes to c# programming?
Either is fine. I knew both before I tried C#, and when I self taught C#, because of my C++ and Java background, it was easy.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
I'll 3rd that. Coming from Java, there really wasn't much to learn. It was a really smooth transition. Of course things have changed some since then, but I think you'll have a similar experience.
I don't know much C++, but I'm sure Java is most like C#. Interfaces, single inheritance, etc...
|
|
|
|
|
If you're already reasonably confident in the basics of C# I'd suggest C++.
As others have said, if you're a total novice in C#, knowing some Java really helps get you started. Personally, I'd been using Java, PHP and VB/VB.net and I found the transition fairly smooth. I'd tried C++ with very limited success and always filed it as one of those things that 'I really should learn properly one day'. Now, having used C# for a while, I'm sure I'd be much more confident with and knowlegeable about C++ if i ever find the need, as C# can do pretty much everything I want.
So (I think) it comes dowwn to how advanced you feel you are in C#. An early learner - probably do the Java. Confident/fairly experienced in C# - probably do C++.
Just my 2c. Good luck with which ever you choose (or do them both if you have the time and can handle it!?!).
Dave
|
|
|
|
|
I'd take the C++ class over the Java based one. Nothing is better than learning the master of all language. If you fully understand C# syntax (then to some extent you know simple C syntax) then I'd go and learn the C++. Nothing is better than fully understanding C++. If you do then you can write C++ in any programming language, now and into the future.
The hard part of switch to C++ syntax from a C# style is the operators used. this->setColor(red,green,blue); instead of this.setColor(red,green,blue);
This is the same when switching from C# (or C++) to php or something with similar syntax.
|
|
|
|
|
C# is based mostly on C++, Pascal and Java.
It's a lot of C++ syntax, but without the need to mess with pointers.
It's object oriented like Java.
It's strictly typed like Pascal (and Java).
I think that you should choose your class depending on where you feel that your weaknesses are. If you want to learn about object orientation you should choose Java, if you like to learn about what really happens with bytes and references you should choose C++.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
I'm using the remove method of a generic list and then saving the class to disk by serialization.
I've noticed that sometimes the data is serialized before the remove has completed, therefore the class gets saved inlcluding the data that should have been removed.
I'm using Application.DoEvents() and it appears to be working but I wondered if this would be reliable, or if there's a better way?
|
|
|
|
|
you can also use backgroundWorker for the same
|
|
|
|
|
Thanks, I don't know why I didn't think of that - do the serialize in the completed event of BackgroundWorker
|
|
|
|
|
Hello, I'm currently in the midst of taking an assessment test, and I need some help with something. essentially, what I need to do, is dynamically reverse a string as it is entered into a text box, and while it is being reversed, replace every number entered (1,2,3) with it's word equivalent (one,two,three). Now I have the string reversal part nailed with the following function:
<br />
public string Reverse(string x)<br />
{<br />
char[] charArray = new char[x.Length];<br />
int len = x.Length - 1;<br />
for (int i = 0; i <= len; i++)<br />
charArray[i] = x[len-i];<br />
return new string(charArray);<br />
}<br />
My main problem is I am not very sure how to dynamically change the numbers to words as the entered string is being reversed. I am considering using some code along the lines of this:
<br />
public string numCheck(string x)<br />
{ <br />
string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };<br />
string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };<br />
string strReplaced = ""; <br />
int i;<br />
for(i=0; i9) <br />
{<br />
strReplaced = x.Replace(numbers[i], words[i]);<br />
x = x.Replace(ch);<br />
}<br />
return x; <br />
}<br />
However, I do not know how to properly insert this into the rest of my program to get the desired result, or even if this is the best way to do it. Thank you in advance.
|
|
|
|
|
You need to include some code in the loop of your main function that checks whether the current character is numeric. If it is, you need to do a lookup to find the word that represents this number and replace the number with its word equivalent.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Lookup? As in a select case statement?
so far after taking your advice I have the following:
<br />
public string Reverse(string x)<br />
{<br />
char[] charArray = new char[x.Length];<br />
int len = x.Length - 1;<br />
for (int i = 0; i <= len; i++)<br />
if(char.IsNumber())<br />
{<br />
<br />
}<br />
charArray[i] = x[len-i];<br />
return new string(charArray);<br />
}<br />
I suppose my next dilemma is trying to figure out what goes into that if statement, I believe my best bet is some sort of select case statement, though the last time I tried that resulted in error. And I'm still trying to figure out what to about the IsNumber(), or rather what I need to put between those parenthesis.
|
|
|
|
|
BREdwards wrote: Lookup? As in a select case statement?
Select...Case . Array lookup. Hashtable lookup. These are all viable options.
BREdwards wrote: I suppose my next dilemma is trying to figure out what goes into that if statement, I believe my best bet is some sort of select case statement, though the last time I tried that resulted in error. And I'm still trying to figure out what to about the IsNumber(), or rather what I need to put between those parenthesis.
Keep trying and you'll get there. Good luck .
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
First off, I'd say this should be in the KeyPress event handler.
The replacements can be done with a switch or Dictionary.
I expect the quickest way to do the reversal is to set the insertion point back to the start of the text with SelectionStart=0 after each character is added, but the user could move the insertion point so handle Click as well.
No, wait, set RightToLeft=true
|
|
|
|
|
Thanks so far. The string reversal part is all taken care of, my problem is having to dynamically replace all number with their word equivalents, here's what I have so far:
<br />
public string Reverse(string x)<br />
{<br />
int i;<br />
char[] charArray = new char[x.Length];<br />
int len = x.Length - 1;<br />
for ( i = 0; i <= len; i++)<br />
if(char.IsNumber(x,i))<br />
{<br />
switch(i)<br />
{<br />
case'0':<br />
x.Replace("0","zero");<br />
break;<br />
}<br />
}<br />
charArray[i] = x[len-i];<br />
return new string(charArray);<br />
}<br />
I have something of an idea of what needs to be done, problem is, I can't seem to use that if statement without breaking the program. I keep getting an "index was outside the bounds of the array error" for both letters and numbers. I simply don't understand what I'm doing wrong, never had to do something like this before.
|
|
|
|
|
Try this for the reversal (not taken care of if you have an error message ):
public string Reverse(string x)
{
char[] charArray = x.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
|
|
|
|
|
This does what you ask using a Hashtable...
public string Reverse(string x)
{
char[] charArray = x.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
private static class NumericStringRepresentations
{
private static Hashtable _PairsTable = new Hashtable(9);
public static Hashtable PairsTable
{
get { return _PairsTable; }
}
static NumericStringRepresentations()
{
_PairsTable.Add("0", "zero");
_PairsTable.Add("1", "one");
_PairsTable.Add("2", "two");
_PairsTable.Add("3", "three");
_PairsTable.Add("4", "four");
_PairsTable.Add("5", "five");
_PairsTable.Add("6", "six");
_PairsTable.Add("7", "seven");
_PairsTable.Add("8", "eight");
_PairsTable.Add("9", "nine");
}
}
public string NumbersToStrings(string x)
{
string returnString = "";
if (!(x.Length == 0))
{
for (int i = 0; i < x.Length; i++)
{
string thisCharacter = x.Substring(i, 1);
int returnedInt;
if (int.TryParse(thisCharacter, out returnedInt))
{
returnString += NumericStringRepresentations.PairsTable[thisCharacter];
}
else
{
returnString += thisCharacter;
}
}
}
return returnString;
}
|
|
|
|
|
Marvelous! I was planning to use an array, but this works as well. Thank you, I'll be sure to save this so I can study later. However, when compiled, it delivers an error saying: "The Modifier 'static' is not valid for this term."
After a bit of research, I found that my version of VS is too old to have static as a modifier for the class, as I am using 2003 still.
Anyhoo, thanks for eveything.
|
|
|
|
|
I'd like to create a button that isn't the standard rectangular shape.
Haven't found anything in the articles, the forums or on the net on how to do it.
So far the only solution I can come up with is to use a PictureBox, handle the mouse events, etc...
Any suggestions or code snippets would be great.
thanks,
Mike
|
|
|
|
|
This code came from here[^]. It needs a little work in the OnPaint as it doesn't display the text but it might help get you started...?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CustomButtons
{
public class MyButton : Button
{
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = new Rectangle(new Point(0, 0),
new Size(this.Width, this.Height));
System.Drawing.Drawing2D.GraphicsPath myPath = new
System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
myPath.AddEllipse(rect);
Region myRegion = new Region(myPath);
this.Region = myRegion;
using (System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.CornflowerBlue, Color.White, 45))
{
e.Graphics.FillEllipse(brush, 0, 0, this.Width, this.Height);
}
}
}
}
|
|
|
|
|
One little additional hint: I don't think that creating a new Region every time the button is repainted without disposing of the previous one is a good idea...
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads.
I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls B2.OnBEvent() .
However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't call A.OnAEvent() .
My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I call ClassA.OnAEvent() and have that event handled by object A?
BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.
Clive Pottinger
Victoria, BC
|
|
|
|
|
Perhaps make it a static event and make the "child" classes register for the event when they're created?
|
|
|
|
|