473,785 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Members

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

Nov 15 '05
43 2147
I also agree with Jon. This is just one of many reasons why it is hard
to communicate with management that c# is better, and that you shouldn't
start new projects with vb.net just because you're "comfortabl e" with
it. And you know that if you try to explain this they simply won't
understand.
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
SomeVeryLongI ndeedAndAnnoyin gToReadTypeName .Method()

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.)


Nov 15 '05 #11
Cor
Hi Jon,

As often discussed C# and VB.net have both advantages but not in the code
itself.

The advantage VB.net that it is somehow like a natural language

The advantage of C# that it is more strict.

Both are good idea's but they conflict with each other.

That does not make in my opinion things absolute more better or worse.
It is just the point of view what you find important decide that.

And because I am writing this from the VB.language group I did make the
comparising with a natural language like English.
Cor
Nov 15 '05 #12
Cor
OHM,

See my comment at Jon.

Cor
Nov 15 '05 #13
Cor <no*@non.com> wrote:
As often discussed C# and VB.net have both advantages but not in the code
itself.

The advantage VB.net that it is somehow like a natural language

The advantage of C# that it is more strict.

Both are good idea's but they conflict with each other.

That does not make in my opinion things absolute more better or worse.
It is just the point of view what you find important decide that.

And because I am writing this from the VB.language group I did make the
comparising with a natural language like English.


Note that no-one in this thread (that I'd seen before reading this
post, at least) has claimed that C# is absolutely better in all ways
than VB.NET - only that in *this* case, it's a flaw in VB.NET.

In my view natural language has nothing to do with it - a type should
be seen as very distinct from an *instance* of the type, but VB.NET is
allowing you to be misled. Why do you think that is an advantage?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Cor
Hi Jon,

I did not say it is an advantage, I disagree that it is a disadvantage.

In VB.net you can use more different methods/functions (whatever) to do the
same thing if you want that.

But that is maybe exactly the difference between the people who use the
language.

I have the idea, that for C# people often everything has to be direct
visible in true or false.

(What by the way I always say it the basic of programming, but that is
another discussion).

Cor
In my view natural language has nothing to do with it - a type should
be seen as very distinct from an *instance* of the type, but VB.NET is
allowing you to be misled. Why do you think that is an advantage?

Nov 15 '05 #15
Cor <no*@non.com> wrote:
I did not say it is an advantage, I disagree that it is a disadvantage.

In VB.net you can use more different methods/functions (whatever) to do the
same thing if you want that.
Having multiple ways of doing the same thing is fine. However, in this
case you can mislead yourself into thinking that something is an
instance member when it's a static member. How can that *not* be a bad
thing? Do you not think that code which is misleading is bad? Isn't it
a good idea for the compiler to make sure you don't mislead people
wherever it can?
But that is maybe exactly the difference between the people who use the
language.

I have the idea, that for C# people often everything has to be direct
visible in true or false.
It really *is* black and white in this case - a static member simply
*isn't* an instance member. Why do you want to hide that information
from programmers, possibly leading to confusion?

If you don't believe it leads to confusion, do a Google search for
"Thread.current Thread().sleep" . Look at how many people have been
confused into thinking that Thread.sleep() is an instance method in
Java when it's not. If they ever try to call sleep "on" a different
thread, they're going to spend ages trying to work out why it's not
working as they expect it to. How can that be anything other than a bad
thing?
(What by the way I always say it the basic of programming, but that is
another discussion).


I'm afraid I didn't understand that sentence.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #16
Cor
Hi Jon,

We will not agree now. However, in past I had the same idea as you.

However, VB.net has the strange thing, that it is the first programming
language that I have seen, which real acts in some degree as natural
languages, I like that.

Disadvantage is than that it has the same as every natural language, it can
be good or bad used, and therefore it can give a lot of misunderstandin g.

But that is always with words you saw it, I wrote

(What by the way I always say it the basic of programming, but that is
another discussion).
What is by the way, as I always say, the basic of programming, but that is
another discussion

(That was about true and false)
Cor
Nov 15 '05 #17
Cor <no*@non.com> wrote:
We will not agree now. However, in past I had the same idea as you.

However, VB.net has the strange thing, that it is the first programming
language that I have seen, which real acts in some degree as natural
languages, I like that.
And how would that be damaged by including the same restriction that C#
has about accessing static members?
Disadvantage is than that it has the same as every natural language, it can
be good or bad used, and therefore it can give a lot of misunderstandin g.
You said before that you didn't believe there was an advantage, but
that you also didn't believe there was not an advantage.

Did you do the Google search I suggested in the previous post? If so,
do you not agree that there are many people using Thread.sleep
inappropriately ? If you agree with that, do you not agree that they are
confused? If you agree with that, do you not agree that their being
confused is a bad thing? If you agree with that, do you not agree that
they would not be confused if Java had the same restriction as C# does?
If you agree with that, how can you *not* agree that it's a
disadvantage?

Confusion is a bad thing. A "feature" which promotes confusion is a
disadvantage, in my view. Which part of that do you disagree with?
But that is always with words you saw it, I wrote

(What by the way I always say it the basic of programming, but that is
another discussion).
What is by the way, as I always say, the basic of programming, but that is
another discussion

(That was about true and false)


I still don't understand you. If your point is meant to be that natural
language can easily be misunderstood, then it's certainly true - think
of C# as a language which helps to make sure that your use of language
is easily understood, and VB.NET (in this case) as a language which
allows the above. Now consider that I can't understand what you wrote
above - why is that a *good* thing in your view?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #18
Cor
Hi Jon,
If your point is meant to be that natural
language can easily be misunderstood, then it's certainly true - think
of C# as a language which helps to make sure that your use of language
is easily understood, and VB.NET (in this case) as a language which
allows the above.
C# is not a langueage that is easily understood because it cannot be
misunderstood.
(I think that you mix up true and false with the wrong references)
Now consider that I can't understand what you wrote
above - why is that a *good* thing in your view?


A natural language needs less comments in the program language and that I
find a very *good* thing.
(Comments are often very bad because they are not checked by rules)

Cor
Nov 15 '05 #19
Cor <no*@non.com> wrote:
If your point is meant to be that natural
language can easily be misunderstood, then it's certainly true - think
of C# as a language which helps to make sure that your use of language
is easily understood, and VB.NET (in this case) as a language which
allows the above.
C# is not a langueage that is easily understood because it cannot be
misunderstood.


It is, partly - and it is in this case. The fact that you can't access
static members as if they were instance members means people won't
misunderstand and think they are using instance members.
(I think that you mix up true and false with the wrong references)


Again, please explain what you mean, very carefully. Where did "true"
and "false" come in here? (This thread demonstrates how easily natural
language can become confusing when it's not used carefully.)
Now consider that I can't understand what you wrote
above - why is that a *good* thing in your view?


A natural language needs less comments in the program language and that I
find a very *good* thing.
(Comments are often very bad because they are not checked by rules)


You've completely side-stepped all my points yet again, I notice. Once
more, which of the following do you disagree with?

1) Many people using Java write Thread.currentT hread().sleep(. ..)
instead of Thread.sleep(.. .)
2) Point 1) indicates a lack of understanding
3) This lack of understanding would not occur in C#, as it would be
invalid code - the programmers would *have* to realise that sleep
is a static method.
4) Confusion between static and instance members can lead to bugs in
code.
5) Bugs are bad, therefore anything which promotes confusion is bad.
6) Being able to access static members as if they were instance members
leads to confusion (points 1-3), and is therefore bad.

*Please* address the above - there's no point in having a discussion if
you're going to ignore the arguments you find difficult to refute.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #20

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

Similar topics

3
3614
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...
8
4592
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? =================
15
6594
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...
6
2472
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).
13
7735
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:
3
9759
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
6
1623
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...
11
2256
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
11
3846
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...
8
2889
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; };
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 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...
0
8971
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, 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...
1
7496
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 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...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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...
1
4046
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
2
3645
muto222
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.