|
Given the following class hierarchy:
public class A
{
public virtual void func();
}
public class B : A
{
public override void func();
}
public class C : B
{
public override void func()
{
base.func();
}
}
How can my class C , call the func() implementation in the base class A .
In C++ this could be done by simply specifying the name of the base class, A::func() , but how can I do this in C#?
Thanks for your help.
|
|
|
|
|
this.func(); will always call the "highest" function it can
|
|
|
|
|
Which is very naughty and therefore not allowed in C#.
Hmmm... will ((A)this).func() do it? I'd better go check.
Nope.
modified on Friday, January 18, 2008 9:09:34 PM
|
|
|
|
|
PIEBALDconsult wrote: Hmmm... will ((A)this).func() do it?
No, it will just recurse.
|
|
|
|
|
So I see. Good. I even tried
public class C : B
{
public override string
func
(
)
{
return ( (string) typeof(A).GetMethod
(
"func"
,
System.Type.EmptyTypes
).Invoke ( this , null ) ) ;
}
}
and it still recursed (as it should).
|
|
|
|
|
PIEBALDconsult wrote: Which is very naughty and therefore not allowed in C#.
Yeah, that's kind of what I thought...
I was beginning to think things were getting a little out of hand, and you've just confirmed it. Time to refactor this particular section, before one of my co-workers posts it to Coding Horrors!
|
|
|
|
|
Hello everyone.
I'm back, and that means... yes, in my ongoing attempts to self-learn C# and .NET, I have once again painted (or coded) myself into a corner.
I managed to write a bunch of code and now find that it needs to be able to do an invalid casting in order for me to proceed. Basically, I have created a class (let's say "Automobile") and other classes ("Truck" and "Van") that are derived from it.
Unfortunately I find my self in the situation where I created
Automobile rentedCar = new Automobile();
but now I need to use rentedCar as a Van. However
Van myWheels = (Van)rentedCar;
is illegal.
I have to create rentedCar first [so that I can rentedCar.DriveHome() ], but I do not know ahead of time whether I will be trying to use rentedCar as a Van or a Truck - depends on whether the kids have a soccer game [myWheels.PlayDisneyDVD() ] or if a friend is moving [myWheels.DropTailgate() ] .
Could someone point me in the right direction for solving this please? Is this what generics are for?
I know, in your world you determine the type of vehicle you want and then rent it. But in my world, we rent it first, drive it home and then decide what kind of vehicle we want it to be - and we like it that way!
Clive Pottinger
Victoria, BC
|
|
|
|
|
You cannot cast it because you've create the "basic" automobile definition which knows nothing about how a van operates.
The easiest way would be to create a new van accepting an Automobile in it's contructor through which you can "copy" stuff across.
|
|
|
|
|
Have your kids and friends decide before you go and rent a car, then rent the
appropriate vehicle.
You can't transform a general-purpose car into a specialized one, but you can ignore
the specialization of a vehicle and concentrate on its general-purpose characteristics.
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.
|
|
|
|
|
It seems to me that your Automobile class should have a DriveHome() method. Since the classes Truck and Van are derived from Automobile, they would be able to use the DriveHome method declared in Automobile.
If the outcome of DriveHome() is going to be different depending on whether your object is a Truck or if it is a Van, then in your Automobile class, when you declare your DriveHome() method, declare it as virtual
public virtual void DriveHome()<br />
{<br />
}
Then, in your Truck class for example you could override the DriveHome() method to customize it specifically for the Truck's situation
public override void DriveHome()<br />
{<br />
if (IAmHelpingFriendMove)<br />
{<br />
}<br />
<br />
base.DriveHome()<br />
}
Hope that helps you out a bit...
|
|
|
|
|
cpotting wrote: I know, in your world you determine the type of vehicle you want and then rent it. But in my world, we rent it first, drive it home and then decide what kind of vehicle we want it to be - and we like it that way!
Well maybe you need to wrap your basic auto in a van or pickup envelope,
Auto auto = new Auto() ;
auto.DriveHome() ;
switch ( typeofvehicleneeded )
{
case VehicleType.Van : return new Van ( auto ) ; break ;
...
}
van.CommitDriveByShooting() ;
auto = van.Auto ;
van.Dispose() ;
auto.Getaway();
auto.Return();
Either way you should read up on interfaces.
|
|
|
|
|
Thanks everyone. It would appear that the general consensus is "You're screwed, pal!".
That's okay. I think I have a way out of this akin to Ed.Poore's suggestion that the Van and Truck construction methods should accept an Automobile object and derive the required information from that.
Thanks again.
If I can't get it to work, I'll be back.
Clive Pottinger
Victoria, BC
|
|
|
|
|
This is the problem, i have a ocx, that have one control and one uint Property, like this
Property color, acept values uint, Manually i set the value as this:
object.color = 0xFFFFFF;
for wythe color, i want to set the color using:
ocxObject.color = colorDialog1.Color; /// this is the error
and i get the error, because the ocxObject.color only acept uint
i make the convertion:
ocxObject.color = (uint)(colorDialog1.Color);
and not have error in code but, the colors not Match.
any solutions thanks
|
|
|
|
|
Hi,
not sure but I think you should try
ocxObject.color = colorDialog1.Color.ToArgb();
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.
|
|
|
|
|
Hello there,
I´m writing a database application with ADO.net to manage my contacts.
I have a DataGridView DGVFriends on my Form that I want to connect to the Table "tblFriends" in my DataSet ds.
So far my code simply is:
DGVFriends.DataSource = ds.Tables["tblFriends"]
the problem is that all the columns in "tblFriends" are shown. For example ID, even though this is only the primary key and the user won´t be interested in it. so i want to omit it´s display in the dataGridView.
The second problem is:
in the same dataGridView I want to add a column that displays the full name of my contact and therefore is compound of the three columns name, middleName and lastName that are also in tblFriends.
Any ideas??
Thanks for help, useful links etc.
der gunnar
|
|
|
|
|
dergunnar,
Adapt the SQL your executing, eg: select name, lastName from tblFreinds;
You can also concat values using SQL.
Regards,
Gareth.
|
|
|
|
|
This should get you going (code not tested for accuracy).
<asp:GridView ID="UserGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="MiddleName" HeaderText="Middle Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Namwe" />
<asp:templatefield headertext="Full Name">
<itemtemplate>
<asp:label id="FullNameLabel" Text='<%# Eval("FirstName") +
Eval("MiddleName") + Eval("LastName")%>' runat="server"/>
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
|
|
|
|
|
Okay,
I should have asked a little more specific. The problem is I´m not working with a databasee. I just read an xml file into the dataset. since it is a local application that´s the easiest way to go.
the next thing is that I want to work with all the columns in the DataTable. that means that in the Datatable the columns ID, Name, middleName... ahould be present. not loading them doesn´t help me.
my problem really lies between the dataTable (already in memory) and the DataGridView.
A workaround for the compound name column that came to me is to create a column FullName in tblFriends. so the only problem would be to prevent the rest of the columns from being displayed n the DataGridView.
Thanx for your help so far!!!
der gunnar
|
|
|
|
|
Hi
i wanna draw circle in a form and setting a picture in that , then moving that in form on a + (Plus) path . in fact the circle (with 100 pixel radius will move from center of form to each side of form
|
|
|
|
|
Create a class that derives from Control. Override the OnPaint and OnPaintBackground methods. Use the graphics context to do the drawing.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
A table contains a,b,c columns
a contains data.
b and c Contains NULL Values.
I fill dataset(ds) with the values of a,b,c
if(ds.Tables[0].Rows[0]["b"].ToString()==Null )
textbox1.text="asd";
this condition is not satisfied,when comparing with value NULL ,they failed to compare and skip that condition.
What other syntax I can use to solve that problem.
|
|
|
|
|
if(ds.Tables[0].Rows[0]["b"] == System.DBNull.Value)
|
|
|
|
|
|
My print preview code looks like this:
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Owner = Application.OpenForms[0];
printDocument.DocumentName = file;
printDocument.PrinterSettings.DefaultPageSettings.Landscape = true;
dlg.Document = printDocument;
dlg.ShowDialog();
The setting is never turned off from landscape, and yet, the printing is landscape, and the print preview isn't. I've done some googling, but I can't see any further info on this, or any reason why it's not working ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
Couple of years ago I struggled a lot with printer settings (landscape, color, duplex),
I never understood the model, it seems complex and not logical; but I got it to work
eventually.
I use printDocument.DefaultPageSettings.Landscape=landScapeFlag;
and that seems to do it for me.
Hope this works for you too.
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.
|
|
|
|