|
Hi,
If you view the compiler warnings you should see what's wrong.
'AbstractChild.Child.GetInt()' hides inherited member
'AbstractChild.AbstractChild.GetInt()'.
To make the current member override that implementation,
add the override keyword. Otherwise add the new keyword.
Alan.
|
|
|
|
|
Hi
Adding new keyword does not solve the problem. It still returns 1
|
|
|
|
|
Hi,
An abstract method must be implemented using the override modifier.
public abstract class Base {
public int GetInt() {
return 1;
}
}
public abstract class AbstractChild : Base {
public new abstract int GetInt();
}
public class Child : AbstractChild {
public override int GetInt() {
return 2;
}
}
When first encountered these concepts are not easy but if you review the information on modifiers in the c# reference and then write some code to test your understanding you'll soon be there.
Alan.
|
|
|
|
|
Do you know why the following code is returning 1 while I have completely replaced the base class method with new operator as you suggested?
Base b = new Child();
Console.Out.WriteLine(b.GetInt());
|
|
|
|
|
Hi,
When you redefine a method with the new modifier it is no longer known to the parent class. The confusing part is that the original method still exists in Base and this is called when you assign the AbstractChild instance into a Base type variable.
If the code is altered (again) we can achieve what I think you originally wanted.
1) declare public virtual int GetInt() in Base
to ensure that the method can be overridden.
2) public override abstract int GetInt() in AbstractChild
This overrides the base class method with an abstract method
3) public override int GetInt() in Child
as required by the abstract modifier in AbstractChild.
Putting all of that together we have
public abstract class Base {
public virtual int GetInt() {
return 1;
}
}
public abstract class AbstractChild : Base {
public override abstract int GetInt();
}
public class Child : AbstractChild {
public override int GetInt() {
return 2;
}
}
class Program {
static void Main(string[] args) {
Base c = new Child();
Console.WriteLine(c.GetInt());
Console.ReadLine();
}
}
The use of the override modifier throughout ensures that the most derived method is always accessible.
Hope you are following the logic, we are both learning here!
Alan.
|
|
|
|
|
Thank you very much for your response Alan! It's working
I didn't know that a method with override modifier can have no body. But with help of override abstract, it can. great to know.
The original code I posted was translated from a java code from Head First Design Patterns book (chapter 3, Decorator Pattern).
Do you know java? It seems that the original code I posted will return 2 written in Java.
|
|
|
|
|
Maysam Mahfouzi wrote: I didn't know that a method with override modifier can have no body.
Actually I didn't either and was a bit surprised too.
I'm not very familiar with java, it's one of my read-only languages, but in contrast to c# it probably has a more relaxed syntax. c# tends to make very few assumptions and as a consequence requires that intentions are stated explicitly.
Alan.
|
|
|
|
|
Hi,
I'm new to the world of .Net and C#. I need some help with serialization:
I'm trying to serialize an object to an XML file, this object has a nested class which I also want to serialize:
[Serializable]
public class NewOrder
{
public NewOrder()
{
}
~NewOrder()
{
}
private string accountGroupfield;
private string accountNumberfield;
public string AccountGroup
{
get
{
return accountGroupfield;
}
set
{
accountGroupfield = value;
}
}
public string AccountNumber
{
get
{
return accountNumberfield;
}
set
{
accountNumberfield = value;
}
}
[Serializable]
public class CardDetail
{
private string cardFirstNamefield;
private string cardLastNamefield;
private string cardNumberfield;
public string CardFirstName
{
get
{
return cardFirstNamefield;
}
set
{
cardFirstNamefield = value;
}
}
public string CardLastName
{
get
{
return cardLastNamefield;
}
set
{
cardLastNamefield = value;
}
}
public string CardNumber
{
get
{
return cardNumberfield;
}
set
{
cardNumberfield = value;
}
}
}
I have a function to serialize the object but all I get in the Xml file is:
<?xml version="1.0"?>
<NewOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountGroup>100</AccountGroup>
<AccountNumber>66302</AccountNumber>
</NewOrder>
I've been searching around the internet for days now, but I still havent managed to get the nested class output as a nested element within the XML.
Apologies for rambling, the output to the XML I'm looking for is as follows:
<?xml version="1.0"?>
<NewOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountGroup>100</AccountGroup>
<AccountNumber>66302</AccountNumber>
<CardDetail>
<CardFirstName>George</CardFirstName>
<CardSurname>Balden</CardSurname>
<CardNumber>6123 2222 4444 6666</CardNumber>
</CardDetail>
</NewOrder>
|
|
|
|
|
Well as far as I can see, you never declare an instance of CardDetail . Serialization only serializes instances of objects, not declarations of objects.
BTW, unless there is something in the part of your code that you haven't posted, I see no reason for CardDetail to be a nested class.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Could you provide some sample code of how i do that, and where i put the code?
would it be similar to:
public CardDetail MyCardDetail;
before i declare the class? Thanks in advance!
George
|
|
|
|
|
Yes but you will need to instantiate it and give its properties some values.
Add the declaration as you have suggested (I would put it with the other two field declarations). Also, strictly speaking you should make it private, then add a public property for access from outside the class.
Then modify your constructor for NewOrder, something like:
public NewOrder()
{
MyCardDetail = new CardDetail();
MyCardDetail.CardFirstName = "Horace";
MyCardDetail.CardLastName = "Horsecollar";
MyCardDetail.CardNumber = "1ABC23";
}
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry, thanks for your help,
I updated the project with your comments but now I'm getting a HTTP 500 error when i Invoke the web service, the full code listing is below:
order.aspx.cs
using System;
using System.IO;
using System.Xml.Serialization;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using ExampleforCodeProject;
namespace ExampleforCodeProject
{
[WebService(Namespace = "http://MyXmlTest/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Order : System.Web.Services.WebService
{
[WebMethod]
public NewOrder SerialiseOrder()
{
NewOrder order1 = new NewOrder();
order1.AccountGroup = "11";
order1.AccountNumber = "878";
order1.Card.CardFirstName = "George";
order1.Card.CardLastName = "Balden";
order1.Card.CardNumber = "872947924793274";
Stream stream = File.Open("C:\\test\\NewSoap.xml", FileMode.Create);
XmlSerializer sf = new XmlSerializer(typeof(NewOrder));
sf.Serialize(stream, order1);
stream.Close();
return order1;
}
}
}
and the Order class (NewOrder.cs):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExampleforCodeProject;
namespace ExampleforCodeProject
{
[Serializable]
public class NewOrder
{
public NewOrder() { }
~NewOrder() { }
private string accountGroupfield;
private string accountNumberfield;
public string AccountGroup
{
get
{
return accountGroupfield;
}
set
{
accountGroupfield = value;
}
}
public string AccountNumber
{
get
{
return accountNumberfield;
}
set
{
accountNumberfield = value;
}
}
public CardDetail Card;
[Serializable]
public class CardDetail
{
private string cardFirstNamefield;
private string cardLastNamefield;
private string cardNumberfield;
public string CardFirstName
{
get
{
return cardFirstNamefield;
}
set
{
cardFirstNamefield = value;
}
}
public string CardLastName
{
get
{
return cardLastNamefield;
}
set
{
cardLastNamefield = value;
}
}
public string CardNumber
{
get
{
return cardNumberfield;
}
set
{
cardNumberfield = value;
}
}
}
}
}
Thanks again for all the help!
|
|
|
|
|
I do not see a Card = new CardDetail() anywhere, you must create an instance before you can use it. I would suggest that you put it in the Constructor for NewOrder since each NewOrder currently requires CardDetail. Later you might want to switch to Lazy Initialization (google it for details).
Although it is difficult to see why that would give an HTTP Error, it is necessary, so try it anyway.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
i need to recognise a road from o picture, and return a 2 color picture, one for the road and one for everithing else.
in theory i know hos to do it: split the image into sqares and analise them separae.
my problem is theat i do not know how to manipulate the sqares, using image filters, to actuali find the road.
any help would be realy apreciated.
thx.
The picture consists of an road surrounded by tree and grass, and you see some sky, am the road has some defects, i thougt recognising the road using the diference in color, but i do not know how .
modified on Monday, May 4, 2009 11:26 AM
|
|
|
|
|
This is not the place for fast answers, particularly on a weekend.
This is not my area of expertise, so please, by all means use the links I give below, but it will not be worth your while asking me any follow-up questions.
Take a look at these articles from CP, to see if they are of any use.
Blobby! - Shape/Blob Recognition Code[^].
Image Recognition with Neural Networks[^]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
The segmantation strategy usually depends and the targeted images. There is no "best" (often even "good") algorithm. Maybe you'll have to throw in some sample images so we can give some opinions.
If by "fast" you mean you need this by Monday morning, you are probably out of luck.
H.
|
|
|
|
|
thx for the sugestion.
helped a lot.
but i have a new problem now, i need the coordenets for a mouse click on the picture, relative to the picture upper left corner, ao any other corner.
i am tying to use this :
this.main_picture_box.Click += new System.Windows.Forms.MouseEventHandler(this.main_picture_box_Click);
private void main_picture_box_Click ( object sender, MouseEventArgs e )
{
label_crop_i.Text = String.Format("x={0} y={1}", e.X, e.Y);
}
protected override void OnMouseWheel ( MouseEventArgs e ){}
but i get this error:
Error 1 Cannot implicitly convert type 'System.Windows.Forms.MouseEventHandler' to 'System.EventHandler'.
any ideea?
|
|
|
|
|
Use MouseDown event instead of Click, this really uses MouseEventHandler and through MouseEventArgs you'll get your coordinates (just like you tried with EventHandler).
H.
|
|
|
|
|
The following:
float.m_result = float.Parse(m_store[0].ToString());
is generating an error:
'float' does not contain a definition for 'm_result'.
The .net help message is:
'type' does not contain a definition for 'identifer'
Not sure which direction I need here. I am new to c#, appreciate help.
Thanks
|
|
|
|
|
onetreeup wrote: float.m_result = float.Parse(m_store[0].ToString());
float m_result = float.Parse(m_store[0].ToString());
My failometer is detecting vast quantities of FAIL!
"Its SQL - hardly programming..." (Caslen)
|
|
|
|
|
i need a code for an Acess Control Code:
a small machine placed on a glass entrance for a company, if an employee wants to get in, he or she has to input an ID thats already givin to him/her.
if its right, the door opens. otherwise, access denied. it also saves the time the employee arrived so that the manager can use this information to make a report of attendancies and abcense.
|
|
|
|
|
I suggest you buy a system off the shelf.
7alool wrote: a report of attendancies and abcense
Will you require employees to enter/leave one at a time even if they arrive at the door together?
Have you considered using system login/logout instead?
At any rate, no such system will be completely accurate, and may leave loopholes for abuse.
|
|
|
|
|
Not to meention the legal issues that can be brought up by an inaccurate attendance report.
|
|
|
|
|
hi
i have a problem, i want do my project that is we have a client/server system and we have 2printer for example hp1 and hp2,and one server and for example 6 cleint computer in network.
a program is install on server and printer me be connected to any client, and if click on hp1 on program in server , hp1 on any client that have hp1 print,and hp2 too.
i want help me ,what i should learn for this project? what idea you have ?
i do client programing and don`t write program on network.
after finish my project take my result on site.
|
|
|
|
|
Soooo, you want to be able to move a printer to any system in the network, and without changing the configuration of any workstation, have them all print to the printer, no matter which system it's connected to?
|
|
|
|