|
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?
|
|
|
|
|
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
|
|
|
|
|
I've a windows form, on which a picturebox is placed. I've loaded images into the picturebox. I've implemented the Zoom In and Zoom out functinality. Now my problem is when I zoom in, the picturebox is not spreading equally in the form. I mean the distance between picturebox and the container form should be equal, on four sides, as I zoom in and out ( the picturebox should be exactly centered). How can I achive this? Any help?
Here is the piece of code for Zoom In and Out
For Zooming In :
pictureBox1.Top= (int)(pictureBox1.Top - (pictureBox1.Height * 0.025));
pictureBox1.Left = (int)(pictureBox1.Left - (pictureBox1.Width * 0.025));
pictureBox1.Height = (int)(pictureBox1.Height + (pictureBox1.Height* 0.05));
pictureBox1.Width = (int)(pictureBox1.Width + (pictureBox1.Width * 0.05));
For Zooming Out :
pictureBox1.Top = (int)(pictureBox1.Top + (pictureBox1.Height * 0.025));
pictureBox1.Left = (int)(pictureBox1.Left + (pictureBox1.Width * 0.025));
pictureBox1.Height = (int)(pictureBox1.Height - (pictureBox1.Height* 0.05));
pictureBox1.Width = (int)(pictureBox1.Width - (pictureBox1.Width * 0.05));
Stay cool
|
|
|
|
|
I bet you can do a lot better than this with some custom drawing code.
To answer your question about centering... the standard way of center goes like this:
int newX = (form.Width - picBox.Width) / 2;
int newY = (form.Height - picBox.Height) / 2;
...
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|