ThePentiumGuy Posted October 31, 2004 Posted October 31, 2004 Hey... As I went over one of my OOP tutorials which I made like a year ago... I began to notice some funky stuff. Are objects always ByRef? Public Sub New(ByVal target As Form, ByVal YourColor As Color) target.BackColor = YourColor End Sub I mean seriously - I wouldn't expect this to happen unles the form argument is byRef instead of byVal. I noticed this too when you say 'byval sender As Object'. -The Pentium Guy Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
Administrators PlausiblyDamp Posted October 31, 2004 Administrators Posted October 31, 2004 Objects are always passed by reference. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ThePentiumGuy Posted October 31, 2004 Author Posted October 31, 2004 Heh, that answers it.. thanks :). Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
*Experts* Nerseus Posted November 1, 2004 *Experts* Posted November 1, 2004 To be more exact, you have to talk about a class versus a struct not just "object" which means different things to different people. Also, it's only the data within a class that's passed ByRef. The variable itself can be passed ByRef or ByVal and it does make a difference. For a struct - any value type really - ByVal and ByRef works more the way you'd expect. Here are two threads that talk about it a little more: http://xtremedotnettalk.com/showthread.php?t=78621 http://xtremedotnettalk.com/showthread.php?t=84175 There was a better thread than these two, but my searching isn't what it used to be - I couldn't find it (only tried looking for ByRef). -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
mskeel Posted November 2, 2004 Posted November 2, 2004 I found this out the hard way recently. The MSDN is sort of misleading with regaurds to byref vs byval. I had always assumed that they acted the same as the traditional C++ value and reference passing, copying memory, speed and preformance, by value can't change the parameter... three questions: 1. Are structs value types and do they act as one would them to if passed by value or refence? (by value => no changes, by reference => changes) 2. Passing byval ensures you can't change the pointer, but is there a way to pass a pointer so that you can't change its contents? Or do you just have to be extremely careful not to change anything? Something equivelent to a const reference pass in C++. 3. I assume this is a .Net problem, not just a VB one, yes? Quote
Administrators PlausiblyDamp Posted November 2, 2004 Administrators Posted November 2, 2004 Structs are value types and classes are reference types. Passing a value type by reference allows changes, by value means no changes. There is nothing in .Net that mimicks the idea of a C++ const reference. Passing a reference type by value means you can change the contents of what the variable points to but not what the variable points to. Passing a reference type by reference means you can change what the variable actually points to. Find a small app attached which demos these using integers and textboxes for for value types and reference types.ValuesAndReferences.zip Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted November 2, 2004 Posted November 2, 2004 And what about static members? Shared, I think in VB... Quote
Administrators PlausiblyDamp Posted November 2, 2004 Administrators Posted November 2, 2004 Shared memebers are not associated with an instance of a class / structure so this doesn't apply to them Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted November 2, 2004 Posted November 2, 2004 Ahh, that's right. sorry, I forgot how shared works. I don' think you can even declare data members as shared anyway so I think my question regaurding static members is irrelevent anyway. Quote
Administrators PlausiblyDamp Posted November 2, 2004 Administrators Posted November 2, 2004 Data members can be shared - it just means the variable only exists once regardless how many class instances are created. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.