Hello Friends
Please check the code below.
One in C# and other in VB .Net
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.
The Error is
Static member 'ClassInCS.ABC. MyInt' cannot be accessed
with an instance reference; qualify it with a type name
instead
Can anybody explain why there is such a difference between
to language?
Is this advantage to VB??
Or it doesn't fit in rule of OOP?
Regards
Mark.
C# Code
class MyClass{
[STAThread]
static void Main(string[] args){
Console.WriteLi ne(ABC.MyInt);
ABC a;
Console.WriteLi ne(a.MyInt); //ERROR
a = new ABC();
Console.WriteLi ne(a.MyInt); //ERROR
}
}
class ABC{
private static int m_MyInt = 1;
public static int MyInt{
get{
return m_MyInt;
}
set{
m_MyInt = value;
}
}
}
VB Code
Sub Main()
Console.WriteLi ne(XYZ.MyInt)
Dim x As XYZ
Console.WriteLi ne(x.MyInt)
x = New XYZ()
Console.WriteLi ne(x.MyInt)
End Sub
Class XYZ
Public Const MyPConst As Integer = 2
Private Shared m_MyInt As Int32 = 1
Public Shared Property MyInt() As Int32
Get
Return m_MyInt
End Get
Set(ByVal Value As Int32)
m_MyInt = Value
End Set
End Property
End Class 43 2145
Probably to make it less confusing. A static variable doesn't belong to
any instance, but rather to the class.
I think VB.Net does some things differently because it needs to be like VB
whereas C# was designed for .Net Framework?
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mark <ma**@discussio ns.microsoft.co m> wrote: Please check the code below. One in C# and other in VB .Net In C# I am not able to access a static property by an instance variable, but in VB I can do it easily.
Why do you think you *should* be able to access a static property via
an instance variable?
It makes it look like the property is an instance property, so would
possibly confuse the reader. This is one problem with Java, IMO - and
it's led a lot of people to write silly code like:
Thread.currentT hread().sleep(5 000);
when they really mean
Thread.sleep(50 00);
- but the first version makes it look like you could call sleep() on a
particular thread to send it to sleep.
Basically I view this as a flaw in VB.NET, not in C#.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Morton, what you have written is fundementally wrong.
VB.NET and C# are *BOTH* written for the .NET Framework.
The problem here is that Static members in C# are prevented from being
called through an instance of the class they are members of. This *ought*
to be the case for VB.NET as well in my opinion.
What John Skeet said was absolutely correct. In VB.NET you can do this
Dim a As Char = '1'
a.IsDigit(a)
or
Char.IsDigit(a)
In C# you 'Must' do this
char.IsDigit( . . . )
Making it far more readable IMHO.
Regards - OHM
Morten Wennevik wrote: Probably to make it less confusing. A static variable doesn't belong to any instance, but rather to the class. I think VB.Net does some things differently because it needs to be like VB whereas C# was designed for .Net Framework?
--
Best Regards - OHM
O_H_M{at}BTInte rnet{dot}com
The issue here is that Static members in C# are prevented from being
called through an instance of the class they are members of. This ought
to be the case for VB.NET as well in my opinion.
What John Skeet said was absolutely correct. In VB.NET you can do this
Dim a As Char = '1'
a.IsDigit(a)
or
Char.IsDigit(a)
In C# you 'Must' do this
char.IsDigit( . . . )
Making it far more readable IMHO.
Regards - OHM
I stand corrected, VB.NET is of course written for .Net Framework
My point is that VB.NET is a conversion of VB to make it "for .Net
Framework" (Like J#, which could be called "Java.Net")
C# however has no non framework counterpart and where VB.NET can't stray
too far from the original VB the designers of C# were free to make a
programming language tailored for .Net framework, while also getting rid
of many uncertainties found in C/C++ VB and Java.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ My point is that VB.NET is a conversion of VB to make it "for .Net Framework" (Like J#, which could be called "Java.Net")
J# was never intended to be a serious tool for development in .NET in my
opinion. It was simply a way to leverage brand J into NET world after all
that nasty business with Sun. I also think it would be more correct to say
that VB.NET was re-written rather than converted.
C# however has no non framework counterpart and where VB.NET can't stray too far from the original VB the designers of C# were free to make a programming language tailored for .Net framework, while also getting rid of many uncertainties found in C/C++ VB and Java.
C# does not derive from a single not net base. I think if you look at it
it looks like some of the best of Java and C++
--
Best Regards - OHM
O_H_M{at}BTInte rnet{dot}com
Hi OHM,
I respectfully disagree.
From the European languages has the English language something strange.
While languages as German, French and Spanish are very strict and other
languages by instance Dutch have/are adopting in the language not existing
words from all kind of languages (Chinese, French, Italian, Portuguese,
Jadish etc) is English totally different.
It is (as far as I know) a mixture between the old French language, original
Gallic and the old North Sea language.
Therefore, English has many words, which are almost the same or often the
same that even does not look at each other.
In my opinion, that does not make the English language incorrect or other
languages better.
Just my thought.
Cor
Cor <no*@non.com> wrote: I respectfully disagree.
From the European languages has the English language something strange.
While languages as German, French and Spanish are very strict and other languages by instance Dutch have/are adopting in the language not existing words from all kind of languages (Chinese, French, Italian, Portuguese, Jadish etc) is English totally different.
It is (as far as I know) a mixture between the old French language, original Gallic and the old North Sea language.
Therefore, English has many words, which are almost the same or often the same that even does not look at each other.
In my opinion, that does not make the English language incorrect or other languages better.
I don't see what relevance your analogy has. VB.NET allows you to
(perhaps unwittingly) write confusing code in a way that C# doesn't. In
C# you get told if you're trying to access something as if it were an
instance member - what's the downside of that? The only *possible*
benefit of the VB.NET way as far as I can see is that you can use
a.Method()
instead of
SomeVeryLongInd eedAndAnnoyingT oReadTypeName.M ethod()
If you really want to avoid that, you can alias the type name -
although I'd be very wary about doing that.
So, as far as I can see there's a downside to the VB.NET way of doing
things, but no upside. In what way does that *not* make C# better in
this respect? (Presumably the reasoning for VB.NET working that way is
for compatibility reasons, but if so there should at least be an option
to turn it off, just like option strict.)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sorry Cor, I agree with Jon on this.
Regards - OHM
Jon Skeet [C# MVP] wrote: Cor <no*@non.com> wrote: I respectfully disagree.
From the European languages has the English language something strange.
While languages as German, French and Spanish are very strict and other languages by instance Dutch have/are adopting in the language not existing words from all kind of languages (Chinese, French, Italian, Portuguese, Jadish etc) is English totally different.
It is (as far as I know) a mixture between the old French language, original Gallic and the old North Sea language.
Therefore, English has many words, which are almost the same or often the same that even does not look at each other.
In my opinion, that does not make the English language incorrect or other languages better.
I don't see what relevance your analogy has. VB.NET allows you to (perhaps unwittingly) write confusing code in a way that C# doesn't. In C# you get told if you're trying to access something as if it were an instance member - what's the downside of that? The only *possible* benefit of the VB.NET way as far as I can see is that you can use
a.Method() instead of SomeVeryLongInd eedAndAnnoyingT oReadTypeName.M ethod()
If you really want to avoid that, you can alias the type name - although I'd be very wary about doing that.
So, as far as I can see there's a downside to the VB.NET way of doing things, but no upside. In what way does that *not* make C# better in this respect? (Presumably the reasoning for VB.NET working that way is for compatibility reasons, but if so there should at least be an option to turn it off, just like option strict.)
--
Best Regards - OHM
O_H_M{at}BTInte rnet{dot}com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming non-standard C++ or if the
problem lies elsewhere.
To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const...
|
by: Scott J. McCaughrin |
last post by:
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member: VarArray::funct were
an extern, but it is declared in the same file (q.v.). What is the
remedy for this?
=================
|
by: Samee Zahur |
last post by:
Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations). Any static member function like this:
//accessing static member i
static void...
|
by: lovecreatesbeauty |
last post by:
Hello Experts,
Why static data members can be declared as the type of class which it
belongs to?
Inside a class, non-static data members such as pointers and references
can be declared as type of its own class. Non-static data members can
not be declared as type of its own class (excluding pointers and
references).
|
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. They don't
actually protect any encapsulation or anything, and for all the actual
protection they offer, they might as well be public.
For example:
class B {
protected:
| |
by: Mauzi |
last post by:
hi,
this may sound odd and noob like, but what is the 'big' difference
between static and non-static funcitons ?
is there any performace differnce?
what is the best way to use them ?
thnx
mauzi
|
by: Matt |
last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in the IDE or did something else happen to cause this?
I get the following error with the sample code below:
D:\Projects\DotNet\C-Sharp\Test\Class1.cs(12): An object reference is required for the nonstatic field, method, or property...
|
by: dee |
last post by:
OleDbCommand class like many .NET classes has the following description in
its help file:
"Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:
1. I thought dynamic variables are thread-safe since threads have their own
|
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 experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of
ways to do this through prototyping and other techniques, but these
behaviors need to be static and...
|
by: crjjrc |
last post by:
Hi, I've got a base class and some derived classes that look something
like this:
class Base {
public:
int getType() { return type; }
private:
static const int type = 0;
};
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |