|
In the documentation on this method it says i can extract the read data within the callback method? i dont understand how to do this...
eg
NetworkStream str = (NetworkStream) result.AsyncState;
byte[] myReadBuffer = new byte[2048];
int numberOfBytesRead = 0;
numberOfBytesRead = str.EndRead(result);
it says "To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object"
so in my case the IAsyncResult = result and I case the stream state from it but how do i get the data? as theres no method to call??
confused
mike
|
|
|
|
|
Create a object which looks something like this
class ObjectState
{
public byte[] myReadBuffer = new byte[2048];
public NetworkStream str = set the network stream object u had created
}
now when you call beginread call it this way
ObjectState state = new ObjectState();
networkstream.BeginRead(state.myReadBuffer, 0, 2048, AsyncCallback method pointer, state)
//Pass state object to last parameter of BeginRead.
when u endread
ObjectState state;
state = (objectState) result.AsyncState;
int numberOfBytesRead = 0;
numberOfBytesRead = str.EndRead(result);
now use state.myReadBuffer; //This should contain the data u r looking for
|
|
|
|
|
Say I have this:
class Boo
{
private int x;
private Foo f;
public Boo()
{
x = 0;
Foo = null;
}
}
Can I use the Wizard to inplement the constructor? Keeps telling me NO. -thanks...
|
|
|
|
|
Well, in those particular cases, they will be initialized to 0 and null by default.
Matt Gerrans
|
|
|
|
|
Say I have this:
class Boo
{
private int x;
private Foo f;
public Boo()
{
x = 0;
Foo = null;
}
}
Can I use the Wizard to inplement the constructor? Keeps telling me NO. -thanks...
|
|
|
|
|
|
So it's answered your question, but you feel the need to ask here ?
What wizard ? You've implimented a constructor as far as I can see, what do you mean ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Hey
I'm doing a little C# exercise and it involves overriding an inherited method
Here is my code
using System;<br />
<br />
namespace BookOverride<br />
{<br />
class Book<br />
{<br />
public string Title()<br />
{<br />
return "Damn Good Book";<br />
}<br />
public string Rating()<br />
{<br />
return "5 Stars";<br />
}<br />
}<br />
<br />
class OtherBook : Book<br />
{<br />
public string Title()<br />
{<br />
return "Some Other Damn Good Book";<br />
}<br />
}<br />
<br />
class Class1<br />
{<br />
static void Main(string[] args)<br />
{<br />
Book bc = new Book();<br />
Console.WriteLine(bc.Title());<br />
Console.WriteLine(bc.Rating());<br />
<br />
OtherBook ob = new OtherBook();<br />
Console.WriteLine(ob.Title());<br />
Console.WriteLine(ob.Rating());<br />
}<br />
}<br />
<br />
}
Now the funny thing is that my VS .NET tells me that new is required when creating the method in class OtherBook, but the whole thing works without it? I do get a warning though
warning CS0108: The keyword new is required on 'Polymorphism.B.Foo()' because it hides inherited member 'Polymorphism.A.Foo()'
Anybody knows why it issues a warning instead of an error?
I am gonna use the new keyword from now on. It's nice to see which methods that get overridden, but does it have a valid purpose other than that?
/thallish
---Sometimes it's the smallest of things that make the biggest of systems crash.... Hey get away from that switch!!!
|
|
|
|
|
hi again
I found my answer.
It turns out that only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.
/thallish
---Sometimes it's the smallest of things that make the biggest of systems crash.... Hey get away from that switch!!!
|
|
|
|
|
was your question than??
However I think my note about override vs new applies to it :P
David
Never forget: "Stay kul and happy" (I.A.)
|
|
|
|
|
hi,
"If you remove the new modifier, the program will still compile and run, but you will get the warning"(MSDN)[^]
"A class-member-declaration is permitted to declare a member with the same name or signature as an inherited
member. When this occurs, the derived class member is said to hide the base class member. Hiding an inherited
member is not considered an error, but it does cause the compiler to issue a warning. To suppress the warning,
the declaration of the derived class member can include a new modifier to indicate that the derived member is
intended to hide the base member" (ECMA C# Spec[^])
What above quotations say, IMHO means new identifier is meant to mark members/classes you intentionaly hide, while when you don't mark 'em it can be because you forget about base members with same names... So it's more like auxilliary keyword - bt useful.
thallish wrote:
PS: I am gonna use the new keyword from now on. It's nice to see which methods that get overridden, and it must have a valid purpose other than that.
exactly!
What is also meaningful difference between new and override is that overriden provides specialization of base method - provides new implementation, while new method's purpose can be different - methods just happend to have same name.
Well, I may not be 100% acurate, bt hopefully my answer is not useless
best regards,
David 'DNH' Nohejl
Never forget: "Stay kul and happy" (I.A.)
|
|
|
|
|
The other important thing to note is that new allows you to access the base class member through your object via a cast to the base class.
override , on the other hand, will force .NET to execute your specific implementation (ie, overriding all base implementations) regardless of whether you cast the object to it's base class or not. Since this keyword is relatively powerful you are required to mark the base methods as virtual or abstract to ensure that behaviour is desired.
At least, I'm fairly sure that's correct. new and override have confused me for a while, and I think I nutted out the difference, finally :p
This space for rent!
My Blog
|
|
|
|
|
The use of the keyword new indicates that you are "hiding" or "shadowing" the base implementation of the method, and that is generally not a good idea.
If you are responsible for the base implementation as well as the inheriting implementation, the best thing to do (unless your situation absolutely precludes it) is to make the base implementation virtual and use override in your subclass(es).
If you do not control the base implementation, using new , or failing to use override on the inheriting method implementation indicates to the compiler that you are completely replacing the base implementation which can be a dangerous choice if you do not fully understand what the base implementation is doing. In fact, hiding base class members is a risky undertaking in any case given that this practice obscures the behavior of your class instances (objects).
A major source of confusion when hiding/shadowing methods can appear when you're declaring variables at design time. If, for instance, your implementation of Class1 required you to treat all types of books as Book instances (e.g. if you had many possible types of book and did not know which specific type you might be operating on - a very common scenario, and one of the biggest values of polymorphism), then you might want to declare all book-type variables as Book. This solution works beautifully if you are using inheritance with virtual and overridden versions of the Title property; the instance version of each overridden member is called without the client ever having to know which type it's using.
However, using new will get you into trouble. If you have hidden a base implementation, then declare a variable as the base type, at run time the framework has no choice but to use the base implementation instead of the subclass implementation. From your example:
class Class1
{
static void Main(string[] args)
{
Book bc = new Book();
// The following will return "Damn Good Book"
Console.WriteLine(bc.Title());
Console.WriteLine(bc.Rating());
// Changed to make the book instance more general
Book ob = new OtherBook();
// The following will return "Damn Good Book" instead of the expected
// "Some Other Damn Good Book", even though, when inspected, your
// instance "ob" will indicate that it is of type "OtherBook".
Console.WriteLine(ob.Title());
Console.WriteLine(ob.Rating());
}
}
The long and short? Use virtual and override modifiers wherever it is possible, and use new only as a last resort.
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start.
Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself.
How do I do that?
|
|
|
|
|
|
What the hell... now it takes 1-2 seconds longer to start up.
And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works.
Thx for the answer
|
|
|
|
|
Weird.
My Program doesn't get a message when the window is hidden.
|
|
|
|
|
Seems to be a bug in System.Diagnostics.Process that the MainWindowHandle is 0 when the window is hidden. I wonder if the assumption they are making is that if the main window is hidden, it should be treated like a "daemon" or "service" kind of app (and thus it pretends that there is no main window). Anyway, I guess if you are hiding your window, then you could use FindWindow() or EnumWindows() to get the handle instead of System.Diagnostics.Process.GetProcesses() . There are other ways I can think of too...
Matt Gerrans
|
|
|
|
|
> What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works.
Well, don't worry, there's a full money-back guarantee. Just send one proof-of-purchase and a self addressed, stamped envelope and you'll get refunded the full amount.
If your startup time increased, that's probably because of something else you did. It doesn't take that long to create a mutex. My app starts up in less than a second, so that couldn't be it. Post your profiler logs and I'll believe you.
Anyway, next time, I'll be sure and submit everything to a full QA cycle, running it through an exhaustive matrix of OS (including all service packs and languages, of course), hardware and environment configurations before posting any suggestions.
Did you miss the "you've got the gist of it and can proceed with the fine-tuning as you like" part? If you expect people to fully vet any suggestions, then you might perhaps state how much you are willing to pay for that work.
Matt Gerrans
|
|
|
|
|
I'm sorry if my comments sound to offensive/overacting. I alway speak a little bit to rough, I'll try better next time
|
|
|
|
|
Glad to hear it. And glad to hear (in your other post) that you got it all working smoothly.
Matt Gerrans
|
|
|
|
|
You mentioned that your previous attempt didn't work. Were you using a mutex?
string uniqueid = Application.ExecutablePath.Replace(@"\", @"_");
Mutex m = new Mutex(false, uniqueid);
if (m.WaitOne(1, true))
{
}
else
{
}
|
|
|
|
|
>You mentioned that your previous attempt didn't work. Were you using a mutex?
No, I didn't say that. But indeed I aint using one cause I don't now I which Namespace the Mutex Class is (and yet I was to lazy to serch for that).
|
|
|
|
|
After using Mutexes and also implementing FindFindow if hwnd is zero everything works fine, it's even way faster.
Thanks a lot!
|
|
|
|
|
I want to make a program that wil work with the arrow keys.
Now i got two questions. First of al what is the code of an arrow key in c# and second how can i use it properly.
The Underdog
|
|
|
|