Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

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*
Posted

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

"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
Posted

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?

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...