|
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
|
|
|
|
|
Our campany is going toward using ES to develop applications in .Net. Our plan is to use ES to distribute applications across our intranet and have them auto-magically interact. The question I have is how (in ES) do you discover what resources are available throught your organization. In other words, if application A want to provide an service on the network how does application B discover its availability. Also what universal naming menthod is used. All I need is some search phrases that apply so that I can get started.
Chuck Knight
|
|
|
|
|
wow! this is one to add to your favourites if you want to call a win32 function but can't work out the c# signature...
http://pinvoke.net/[^]
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
Yep! It's nice isn't it.
By the way there's a VS.net addin that makes life even easier Search for it.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Hi all,
I am doing my first setup project for the addin I wrote and can't solve following problem.
I want to deploy an addin for Visual studio. So I need to look in registry for the HKLM\SOFTWARE\Microsoft\VisualStudio\7.X\InstallDir (where 7.X is 7.0 or 7.1) and take it into MyPATH0 and MyPATH1 variables. For the installed version(s) I need to make TARGETDIR to MyPATH0\..\..\\Vc7\VCWizards.
I have no idea how to solve this. Setup wizard seems to have very simple logic, so how can I program in the wizard
"
1.TAKE REGISTRY KEY XX\7.0 into VAR
2. If success Make VAR+AAA to the TARGET DIR
3.Install
4.TAKE REGISTRY KEY XX\7.1 into VAR+AAA
5. If success Make VAR to the TARGET DIR
"
Thank you very much for your help,
Boni
|
|
|
|
|
Hi guys, I need help, DatagridTableStyle property AlternatingBackColor alternates ones per row (odd numbers), i need it the alternate every two rows, (ex: Blue, Blue, Red, Red, .....) any idea how to do this? ![Confused | :confused:](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_confused.gif)
|
|
|
|
|
Make your own DataGridColumnStyle or just change an existing one:
<br />
public class AlternatingColumnStyle : DataGridTextBoxColumn <br />
{<br />
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)<br />
{ <br />
int i = rowNum % 4;<br />
Color c = Color.Blue;<br />
if (i > 1)<br />
c = Color.Red; <br />
base.Paint(g, bounds, source, rowNum, new SolidBrush(c), foreBrush, alignToRight);<br />
}<br />
}<br />
|
|
|
|
|
is there is any method to return web services values not from the net and web sites only but return it also from win apps on the web or the network or windows application that is connected to the net
ByMindOnlyYouCanDoIt
|
|
|
|
|
Web services run in the IIS process and can only be executed within this process. What you're looking to do is called Remoting but it does not use Web Services.
|
|
|
|
|
Hey
I am Working on a Server/Client application in Asynchronous mode. I am using simple Socket when Asynchronous dont simes to be suported by TcpClient och TcpListener.
I am using Serializable on some objects(in a DLL file) to be abled to send this objects easy between the Server and Client, by this i dont want to work with a byte buffer, insted i want somthing like this.
<br />
stream = socket.GetStream(); <br />
formatter.Serialize(stream,user);<br />
There i easely can get the objects(if there is any in the stream. Tha abow code is not with a regular socket but a TcpClient.
Right now my Program looks like this
<br />
private void SetupRecieveCallback(Socket server)<br />
{<br />
try<br />
{<br />
if(server != null)<br />
{<br />
AsyncCallback recieveData = new AsyncCallback(OnRecievedData);<br />
<br />
<br />
}<br />
}<br />
catch(Exception ex)<br />
{ MessageBox.Show("Error in ForumClient - Connection - SetupRecieveCallback : " + ex.Message);<br />
}<br />
}<br />
<br />
<br />
public void OnRecievedData(IAsyncResult ar)<br />
{<br />
Socket server = (Socket)ar.AsyncState;<br />
try<br />
{<br />
int nbyte<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show("Error in ForumClient - Connection - OnRecievedData : " + ex.Message);<br />
}<br />
}<br />
How do i get the stream from the socket? and how do i implement it in my program ?
//SnowJim
|
|
|
|
|
Have manage to get a stream no by using NetStream! ofcource =)
Now i have another question!
In the SetupRecieveCallback i am using server.BeginReceive(m_byBuffer,0,m_byBuffer.Length,SocketFlags.None,recieveData, server);
This will trigger so that OnRecievedData is thown if someone is sending data, but if i dont whant to use this buffert and insted use this
ForumPack.command user = (ForumPack.command)formatter.Deserialize(stream);
is there any way to do the same thing but with this stream? or do i have to use the buffert(and set the size) and then translate the byte in to the object?
BestRegards
SnowJim
|
|
|
|