PC Review


Reply
Thread Tools Rate Thread

abstract and a static method (Help)

 
 
Polo
Guest
Posts: n/a
 
      22nd Mar 2005
Hi

I have a abstract class and some other than inherite from it
I woul like to return a bitmap (defined in resource) from the each subclass
(a bitmap that is global (static))

Thank's in advance

public abstract class Segment
{
static public Bitmap GetBitmap()
{
return new Bitmap(....);
}
}

public class SegmantA : Segment
{
....
}


public class SegmantB : Segment
{
.....
}

public class Test
{
static void Main()
{
Bitmap A = SegmentA.GetBitmap();
Bitmap B = SegmentB.GetBitmap();
}
}


 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      22nd Mar 2005
Hi,

GetBitmap cannot be static, it has to be virtual

I would suggest you to take a look into OOP concepts.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Polo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I have a abstract class and some other than inherite from it
> I woul like to return a bitmap (defined in resource) from the each
> subclass
> (a bitmap that is global (static))
>
> Thank's in advance
>
> public abstract class Segment
> {
> static public Bitmap GetBitmap()
> {
> return new Bitmap(....);
> }
> }
>
> public class SegmantA : Segment
> {
> ....
> }
>
>
> public class SegmantB : Segment
> {
> .....
> }
>
> public class Test
> {
> static void Main()
> {
> Bitmap A = SegmentA.GetBitmap();
> Bitmap B = SegmentB.GetBitmap();
> }
> }
>
>



 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      22nd Mar 2005
Polo wrote:
> Hi
>
> I have a abstract class and some other than inherite from it
> I woul like to return a bitmap (defined in resource) from the each subclass
> (a bitmap that is global (static))
>
> Thank's in advance
>
> public abstract class Segment
> {
> static public Bitmap GetBitmap()
> {
> return new Bitmap(....);
> }
> }
>
> public class SegmantA : Segment
> {
> ....
> }
>
>
> public class SegmantB : Segment
> {
> .....
> }
>
> public class Test
> {
> static void Main()
> {
> Bitmap A = SegmentA.GetBitmap();
> Bitmap B = SegmentB.GetBitmap();
> }
> }
>
>


Polo

What you want to do will work. See the following code. I've changed
things so strings are returned instead of Bitmaps but you should get the
idea.

using System;

namespace StaticAbstract
{
class StaticAbstractTest
{
[STAThread]
static void Main(string[] args)
{
string A = SegmentA.GetString();
string B = SegmentB.GetString();

Console.WriteLine(A);
Console.WriteLine(B);

Segment SegA = new SegmentA();
Segment SegB = new SegmentB();

string C = SegA.OverrideMe();
string D = SegB.OverrideMe();

Console.WriteLine(C);
Console.WriteLine(D);
}
}

public abstract class Segment
{
public static string GetString()
{
return "StaticString";
}

public virtual string OverrideMe()
{
return "Base + ";
}
}

public class SegmentA : Segment
{
public override string OverrideMe()
{
return base.OverrideMe() + "SegmentA String";
}

}

public class SegmentB : Segment
{
public override string OverrideMe()
{
return base.OverrideMe() + "SegmentB String";
}

}
}

This gives output of :
-----------------------
StaticString
StaticString
Base + SegmentA String
Base + SegmentB String

If you need to override a method in a subclass, it needs to be marked as
'virtual' in the base class, like I have done here with the method
OverrideMe(), but you are not actually calling a method of the subclass,
you are simply calling the static method held in the base class. Even
though the syntax is SegmentA.GetString() you are actually calling
Segment.GetString()

I hope this is of help.

Best Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract method in non-abstract class Chris Zopers Microsoft ASP .NET 2 8th Aug 2008 12:40 PM
Re: static abstract? Mark Rae Microsoft C# .NET 0 4th Jan 2007 05:51 PM
what is abstract class and abstract method N.RATNAKAR Microsoft C# .NET 4 19th Oct 2006 08:23 PM
Static data access method vs non-static method CW Microsoft ASP .NET 1 31st Mar 2004 06:34 PM
abstract static? Chris Capel Microsoft C# .NET 29 19th Oct 2003 12:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:25 PM.