473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I thought this wasn't allowed in C# - invoking static members from a subclass

Today I discovered that a syntax that I thought was forbidden in C# but
allowed in VB.NET (and I _don't like_ that it's allowed in VB.NET) is
actually allowed in C#. Which confused me. The syntax in question is
referring to static members of a class *from a subclass*.

(Now, I know for sure there's a similar difference, in that VB.NET
allows/ed you to invoke static members from an instance member, whereas
C# has always required you to give the class name. This is different)

My usual example for this has always been

.... = Bitmap.FromFile(FileName)

I hate seeing this (or things like it) in VB.NET because it's
completely misleading. FromFile is a method of *Image* and it returns
an *Image*, but when you _read_ 'Bitmap.FromFile' you _think_ - oh
that's going to give me a bitmap from a file. hang on, why am I getting
an assignment error?'. Another problem is this: open your MSDN Library,
go to the Index, and type Bitmap.Fromf ... wait a minute ... confusion
results!

But it seems C# is perfectly happy with this (not even a warning)?! So
where did I get the idea it wasn't? And why is this allowed?

--
Larry Lard
Replies to group please

Mar 2 '06 #1
8 1543
I agree it can be confusing, but if you are aware that FromFile is
implemented in the abstract Image class, and not in the derived Bitmap class
it's not that big of a deal.

Bitmap myPic = Bitmap.FromFile(@"C:\MyBitmap.bmp") as Bitmap;

You do get a compile error is do you do not convert the returning Image
instance into a bitmap. Take out the "as Bitmap" part and you'll get a
compile error. That's warning enough I think.

Will do the trick.

"Larry Lard" wrote:
Today I discovered that a syntax that I thought was forbidden in C# but
allowed in VB.NET (and I _don't like_ that it's allowed in VB.NET) is
actually allowed in C#. Which confused me. The syntax in question is
referring to static members of a class *from a subclass*.

(Now, I know for sure there's a similar difference, in that VB.NET
allows/ed you to invoke static members from an instance member, whereas
C# has always required you to give the class name. This is different)

My usual example for this has always been

.... = Bitmap.FromFile(FileName)

I hate seeing this (or things like it) in VB.NET because it's
completely misleading. FromFile is a method of *Image* and it returns
an *Image*, but when you _read_ 'Bitmap.FromFile' you _think_ - oh
that's going to give me a bitmap from a file. hang on, why am I getting
an assignment error?'. Another problem is this: open your MSDN Library,
go to the Index, and type Bitmap.Fromf ... wait a minute ... confusion
results!

But it seems C# is perfectly happy with this (not even a warning)?! So
where did I get the idea it wasn't? And why is this allowed?

--
Larry Lard
Replies to group please

Mar 2 '06 #2
I find this notation confusing as well, and I can't see any reason for
including it in the language.

Does anyone know the reasoning behind this one?

Mar 2 '06 #3
Hi,


My usual example for this has always been

... = Bitmap.FromFile(FileName)

I hate seeing this (or things like it) in VB.NET because it's
completely misleading. FromFile is a method of *Image* and it returns
an *Image*, but when you _read_ 'Bitmap.FromFile' you _think_ - oh
that's going to give me a bitmap from a file. hang on, why am I getting
an assignment error?'. Another problem is this: open your MSDN Library,
go to the Index, and type Bitmap.Fromf ... wait a minute ... confusion
results!


This particular case is confusing, the first question is why a base abstract
class return an instance of a derived class?

Of course as FromFile cannot be virtual (as it's static) you cannot let the
derived class implement it.

IMO FromFile should had been declared in Bitmap and not in Image.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 2 '06 #4
Hi,

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I find this notation confusing as well, and I can't see any reason for
including it in the language.

Does anyone know the reasoning behind this one?


The only reason I can think of is to allow the class (and its descendand)
to call the static method without a reference to the class:

class A{
static void M(){}

void aMethid(){ M(); } //allow this

Which I also find confusing, IMO static members should always been invoked
from the type that defined them

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 2 '06 #5
> Of course as FromFile cannot be virtual (as it's static) you cannot let the derived class implement it.

At first blush this doesn't make any sense, but sure enough, the
compiler doesn't like this:

public class A { public static void foo() { } }

public class B : A { public static void foo() { } }

Now, my little brain says that there should be no problem here: static
members are not inherited, so I've just declared two distinct methods,
A.foo() and B.foo(). However, sure enough, the compiler complains that
B.foo() required the keyword "new" because it "hides inherited member
A.foo()". Now, is that nuts, or what? Static members aren't inherited,
but they are... sort of.

Jon Skeet pointed this out to me a couple of months ago. It still
doesn't make a lick of sense to me.

Does anyone know the rationale for including this oddity in the way
that .NET handles inheritance? I imagine it was deliberate....

Mar 2 '06 #6

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:um**************@TK2MSFTNGP12.phx.gbl...
Hi,

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I find this notation confusing as well, and I can't see any reason for
including it in the language.

Does anyone know the reasoning behind this one?


The only reason I can think of is to allow the class (and its descendand)
to call the static method without a reference to the class:

class A{
static void M(){}

void aMethid(){ M(); } //allow this

Which I also find confusing, IMO static members should always been invoked
from the type that defined them


It is consistent with non-static methods and allows static methods to be
moved to a base class without breaking code or requiring you to know the
ancestry.

Mar 3 '06 #7
HI,


The only reason I can think of is to allow the class (and its descendand)
to call the static method without a reference to the class:

class A{
static void M(){}

void aMethid(){ M(); } //allow this

Which I also find confusing, IMO static members should always been
invoked
from the type that defined them


It is consistent with non-static methods and allows static methods to be
moved to a base class without breaking code or requiring you to know the
ancestry.


I'm not very sure it's a good thing , after all thery are not the same as
instance method.

It does break the code if you call them from outside a derived class.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 3 '06 #8
Hi,


Jon Skeet pointed this out to me a couple of months ago. It still
doesn't make a lick of sense to me.


Do you have that post around? or a link to it . I would love to read it
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
2
by: Polo | last post by:
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...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Alex | last post by:
Hello. I want to manage a class hierarchy with a static member (a vector) in which some strings are stored depending on the Class: class Base { // Abstract .... typedef std::vector<std::string>...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
2
by: Jonathon J. Howey | last post by:
Hi, Suppose I have the following pseudo-class-ish code: class A { member1; member2; public static Method() { return member1; }
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.