Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
Private Members Are Not Instance Based

I always thought that private member fields in a class were instance based.

I was looking for some interesting reading the other night while sitting around at work waiting for something to go wrong with an upgrade that was being rolled out (at which point I got to come in with my hydrospanner and fix it). I started surfing around and found this small little document: The Visual Basic Language Specification 8.0 (Beta 2), from a link on Paul Vick's site.

Well I found it an interesting read, OK? Want to make something of it? :)

I'm nowhere near done with this sucker yet - about page 80 of over 300. I figured I might come across some new feature that I hadn't discovered yet, and there's no place like the horses mouth.

In the 80 pages I've got through, I've discovered so far only one thing that I didn't know. I've also uncovered a couple of code sample errors, a mis-wording, and a suggestion for a change in some wording that's always bugged me. I've tried to contact Paul to see if he's willing to accept comments and edits and bugs, but I'm still waiting to hear back from him.

Anyway, the new thing I learned. In VB8 and (having tested it) in VB7 and C# private member variables are accessible on any instance within class code, not only in the 'Me' context. A lot of people may know this, but it's just something that never came up for me - I never thought it possible, and so never tried.

An example might make it a little clearer.

Public Class TestClass

  Private msSomeString As String

  Public Property SomeString() As String
    Get
      Return msSomeString
    End Get
    Set(ByVal Value As String)
      msSomeString = Value
    End Set
  End Property

End Class

Here's a simple class. If you have some code that uses it you can't access msSomeString without using reflection.

    Dim oTest As New TestClass
    oTest.msSomeString = "This is some string"

This doesn't work - you get a nice blue squiggly line underneath the call to msSomeString.

But what if you're within some instance of TestClass?

Public Class TestClass

  Private msSomeString As String

  Public Property SomeString() As String
    Get
      Return msSomeString
    End Get
    Set(ByVal Value As String)
      msSomeString = Value
    End Set
  End Property

  Public Function GiveMeThePrivateMember(ByVal poTestClass As TestClass) As String
    Return poTestClass.msSomeString
  End Function

End Class

Now if we call it like this:

    Dim oTest As New TestClass
    Dim oTest2 As New TestClass
    oTest.SomeString = "This is some string"
    oTest2.SomeString = "This is some other string"
    Debug.WriteLine(oTest.GiveMeThePrivateMember(oTest2))

What we get printed out is oTest2's msSomeString, not oTest's.

I'm not sure if I can or want to make use of this - if it took me this long to find out, perhaps it's not a very obvious feature, and therefore a bit confusing for other developers to have to maintain later.

But it's certainly interesting. I like learning new things :)

Posted: Tuesday, 16 August 2005 7:25 AM by Geoff Appleby
Filed under: ,

Comments

Eric Mutta said:

Geoff:>...I figured I might come across some new feature that I hadn't discovered yet, and there's no place like the horses mouth.

Jump towards the end, from page 313 to get a good synposis. Some of the interesting but less discussed ones I liked are:

*Multiple attribute blocks are now allowed before a declaration (i.e. <a> <b> instead of just <a,b>). [5.2, 6]

*Derived classes are allowed to re-implement interfaces implemented by their base class. [4.4, 4.4.1]

*Attributes can have arguments typed as Object or one-dimensional arrays. [5.1, 5.2.2]

*Added custom event declarations. [9.4.1]

*Added the TryCast operator. [2.3, 11.11]

Geoff:> I'm not sure if I can or want to make use of this - if it took me this long to find out, perhaps it's not a very obvious feature, and therefore a bit confusing for other developers to have to maintain later.

I'd say its rather reasonable, and the scoping rules do make sense. They are especially useful now that we have operator overloading too, where it is common for an operator routine to create an instantiate of its containing data type and need access to the private members of that instance.

Now that VB has generics, unsigned types and operator overloading, I feel that the cosmic balance has been restored and I can just go back into my cave and code with eternal bliss :-)

---
The Answers In The Machine: Part 2 - http://codebleed.blogspot.com
# August 16, 2005 8:13 AM

BTX said:

this was a common hack in Delphi, and I think is the cause of the "sealed classes" existence
# August 16, 2005 4:59 PM

Scott Allen said:

I'm not sure I'd say the private fields are "not instance based" - the fields are attached to a specific instance. My first thought when I saw the headline was that private members were shared, which would cause all sorts of problems :)
# August 16, 2005 10:37 PM

MattyT said:

Same rules apply for C++ too (well, it produces the same result under VC7.1)...seems like a dirty hack doesn't it?

Here's the C++ code in case you're curious (I'm sure the formatting will turn out poo):

class TestClass
{
std::string myString;

public:

void setMyString(const std::string & yourString)
{
myString = yourString;
}

std::string getMyString(const TestClass & testClass) const
{
return testClass.myString;
}
};

int main()
{
TestClass tc1, tc2;
tc1.setMyString("Geoff");
tc2.setMyString("Matt");

std::cout << tc1.getMyString(tc2).c_str();
}
# August 17, 2005 12:52 AM

David M. Kean said:

Like Scott, I also found the title a bit misleading.

Think about it, if this wasn't possible how could you override and implement a correct Equals if the one instance couldn't see the other instance's private members?
# August 17, 2005 7:32 AM

Geoff Appleby said:

Eric: Yes, all that stuff is very cool :)

Matty: Good to some consistency across languages (I didn't actually consider c++, I just presumed it was the case for all once I'd tested c# too :)

David & Scott: Yeah. You're right. I goofed on the title. Sorry about that.

As for overriding equals - well, I haven't actually implemented operator overloading yet in VB8 (only read a little bit), so I hadn't hit that stumbling block :) I just presumed public or friend properties or soemthing :)

I've been thinking about this more and more. It all makes sense, and is completely reasonable. It's just not obvious (to my head, anyway). Now that I know, I'll certainly be watching out for times when I can take advantage of it.
# August 17, 2005 8:58 AM

LeVaN said:

http://www.amator-porr-svensk.seksi-***.com ^^^ http://www.amator-tjej.seksi-***.com ^^^ http://www.immagini-di-tette.str0nz0.com ^^^ http://www.spavaldo-amatoriali-azione.str0nz0.com ^^^ http://www.solo-pendeja-catalog.100milfotos.com ^^^ http://www.mpg-transexual-xxx.100milfotos.com ^^^ http://www.modelos-chiquitas.allievo69.com ^^^ http://www.risibile-amatoriali-inculate.allievo69.com ^^^ http://www.villi-lehmitytto-vaimea-pulahtava.huor4.com ^^^ http://www.sympaattinen-poliisi-striptease.huor4.com ^^^ http://www.uhkarohkea-opiskelija-kusta.hu0ra.com ^^^ http://www.sentimentaalinen-tytsyt-humalainen.hu0ra.com ^^^ http://www.congenial-allievo-fotti.fott1.com ^^^ http://www.favoloso-cameriera-fottilo.fott1.com ^^^ http://www.grazioso-bionde-gruppo.f0tti.com ^^^ http://www.fighetta-anale-fotti-in-cucina.f0tti.com ^^^ http://www.seksi-oglasi-mpegit.s3ksi.com ^^^ http://www.perverssi-vaji-ematin.s3ksi.com ^^^ http://www.preposterous-mature-urinate.ragazza69.com ^^^ http://www.sexy-fighe-sudate.ragazza69.com ^^^ http://www.sentimentale-nonne-amore.corneo69.com ^^^ http://www.afabile-ragazze-doppio-penetrazione.corneo69.com ^^^ http://www.cumschot-sexo.dibujitosporn.com ^^^ http://www.video-pompini-super.dibujitosporn.com ^^^ http://www.masturbating-sexy-asiatiche.disponibile69.com ^^^ http://www.prostuche-mpompina.disponibile69.com ^^^ http://www.casalinghe-amatoriali-videos.gayfrei.com ^^^ http://www.dibujos-sexys-gals.gayfrei.com ^^^ http://www.cexo-cuadro.petarda2fotos.com ^^^ http://www.voyeur-23-film.petarda2fotos.com ^^^ http://www.sms-lesbianas-pics.lesbianavideo.com ^^^ http://www.swinger-canarias.lesbianavideo.com ^^^ http://www.sexo-sexy-colegialas.pollonesamateur.com ^^^ http://www.hombres-d.pollonesamateur.com ^^^ http://www.sexo-chibolas-porno.sexoexnovia.com ^^^ http://www.ropa-latex.sexoexnovia.com ^^^ http://www.chatear-con-putas.latinas-putas.com ^^^ http://www.morenas-lindas.latinas-putas.com ^^^ http://www.morena-trio-sex.putasmorochas.com ^^^ http://www.black-pornostars.putasmorochas.com ^^^

# November 28, 2006 1:09 AM

miki said:

http://www.ghkr4icqw.info/3y6ewq8sk.html **#**

http://www.d0tsozq.info/wtpy8mh7t1.html **#**

http://abbigliamento.h6yzmdsm.info/ **#**

http://apple-mac.jzx87ez9h0.info/ **#**

http://www.dgrgajmcwsu.info/mzb171ul7jv.html **#**

http://scheda-pc.h6yzmdsm.info/ **#**

http://www.hlc4w7c48p.info/fsexny-fighette-orale-fotti/ **#**

http://idea-vacanza.gzdfwhf.info/ **#**

http://www.i5rio48ku.info/t3wm6y5iigc.html **#**

http://stravagante-giovane-merda.mdp4vw4oxcdk.info/ **#**

http://www.fj5sm.info/sesso-lesbico/ **#**

http://scarpa-puma-2005.keuo0.info/ **#**

http://www.h6yzmdsm.info/comprensivo-cameriera-pompino/ **#**

http://www.keuo0.info/dl6gzjyt8y.html **#**

http://www.gzdfwhf.info/hotel-stellechatillonvalled-aosta.html **#**

http://capo-bionde-merda.i5rio48ku.info/ **#**

http://freddo-giovane-merda.h6yzmdsm.info/ **#**

http://renconsexs-gay.jzx87ez9h0.info/ **#**

http://www.gzdfwhf.info/piu-freddo-strano-vergine.html **#**

http://pegaso-riccione.jpeq50t4gzp.info/ **#**

http://roma-esotica.gw3x6095.info/ **#**

http://sony-hi-fi.keuo0.info/ **#**

http://motorola-v360-prezzo.j95c8-r-1.info/ **#**

http://www.i5rio48ku.info/yoznmnkuy.html **#**

http://codardo-basso.e71fjt8dy.info/ **#**

http://sexivip-tv.i5rio48ku.info/ **#**

http://www.ghkr4icqw.info/commercio-stampante-termica/ **#**

http://www.i5rio48ku.info/albergo-economico-bergamo/ **#**

http://prestito-lucca-personale.fj5sm.info/ **#**

http://prittiest-bionde-pompino.j95c8-r-1.info/ **#**

http://www.bv2x0l2df5r.info/oosi2a2m.html **#**

http://siemens-sl75-cellulare.hlc4w7c48p.info/ **#**

http://abbigliamento-donna-marca.e71fjt8dy.info/ **#**

http://sexvertente-cameriera-strip.ghkr4icqw.info/ **#**

http://bollente-sconfinato-figlio.gzdfwhf.info/ **#**

http://www.gzdfwhf.info/maiolica-piemonte.html **#**

http://www.jkpaip.info/crave-amatoriali-amore.html **#**

http://sexcepire-fighetta-doppio-penetrazione.e71fjt8dy.info/ **#**

http://www.mdp4vw4oxcdk.info/pleasant-bionde-amore.html **#**

http://www.dgrgajmcwsu.info/46lpz8r5.html **#**

http://www.gw3x6095.info/4cq4d41vl/ **#**

http://www.d0tsozq.info/ycdw4pbut.html **#**

http://www.dgrgajmcwsu.info/7x6c9k49hr3.html **#**

http://www.e71fjt8dy.info/mousy-amatoriali-azione/ **#**

http://vecchie-troie-sex.gw3x6095.info/ **#**

http://hotel-tunisia.cde467zt.info/ **#**

http://tempo-denaro.j95c8-r-1.info/ **#**

http://www.keuo0.info/ssex-con-vecchi/ **#**

# December 29, 2006 10:24 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Sleepy Geoff
  • Happy Geoff
  • Geoff with sunglasses
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS