何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 35971
  • トラックバック - 222
ニュース
  • IE7以前では、表示がおかしい。div の解釈に問題があるようだ。
    IE8の場合は、「互換」表示を OFF にしてください。
  • 検索エンジンで来られた方へ:
    お望みの情報は見つかりましたか? よろしければ、コメント欄にどのような情報を探していたのか、ご記入ください。
It's ME!
  • はなおか じった
  • 世界遺産の近くに住んでます。
  • Microsoft MVP for Visual Developer ASP/ASP.NET 10, 2004 - 9, 2011
広告

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

その他

わんくま同盟

同郷

 

メモ: VBScript と Visual Basic .NET 以降のものの変数の扱い(システム管理な雑記 -- Sleeve notes of a sysadmin --)より:

関数やサブプロシージャを作るときにちょっとポイントになると思うため、メモ。

既定では、渡し方(ByVal/ByRef) を指定していない変数は、 VBScript では ByRef つまり、モトの変数をそのまま「参照」して使う。Visual Basic .NET 以降の Visual Basic では、 ByVal で扱う。モトの変数のローカルコピーをメモリ空間上で新しく作って使う。リファレンスにしている MSDN ライブラリによると参照渡しでも値渡しでも、パフォーマンスには大きな差は生じないという。動きの面でたいして違わない場合は気にしないでもよいということかな。(CPU とメモリは節約できれば、したほうがいいと思うけど。)

なるほど。なかなかおもしろいので、取り上げさせていただきます。

これは、変数そのものが扱っている内容が変更されていることによるものです。そのことが実感できる実験してみましょう。

適当な VB.NET の Windows Form アプリケーションを用意します。フォームには TextBox を1つだけ置きます。そして、フォームのコンストラクタ(New サブプロシージャ)に、次のように書き加えます。

Public Sub New()
    MyBase.New()
    ' この呼び出しは Windows フォーム デザイナで必要です。
    InitializeComponent()
    ' InitializeComponent() 呼び出しの後に初期化を追加します。
    ' 次の行を追加する
    AddHandler Application.Idle, AddressOf ByValAndByRef
End Sub

では、実験用のコードを追加します。

Public Class HogeClass
    Public Value As Integer
End Class
Private Sub ByValAndByRef(ByVal sender As Object, ByVal e As EventArgs)
    RemoveHandler Application.Idle, AddressOf ByValAndByRef
    Dim hoge As New HogeClass
    hoge.Value = 1
    TextBox1.Text = hoge.Value.ToString()    ' [A]
    ByValFunc(hoge)
    TextBox1.Text += hoge.Value.ToString()   ' [B]
    ByRefFunc(hoge)
    TextBox1.Text += hoge.Value.ToString()   ' [C]
End Sub
Sub ByValFunc(ByVal hage As HogeClass)
    hage.Value = 2
    hage = New HogeClass
    hage.Value = 3
End Sub
Sub ByRefFunc(ByRef hage As HogeClass)
    hage.Value = 4
    hage = New HogeClass
    hage.Value = 5
End Sub

ByValFunc および ByRefFunc では、同じことをしています。引数に値を入れて、新しいオブジェクトを作り、それに別の値を入れています。[A], [B], [C] で表示される値によって、何を参照しているかわかります。

実行する前に、どうなるか予想してみてください。表示されるものの候補は、
124
125
134
135
の4通りあります。さて、どうなるでしょうか?

あ・・・、ここで切ったら、もう1エントリ作れる(笑)

投稿日時 : 2007年5月26日 0:32
コメント
  • # re: VB マイグレーション ByRef と ByVal
    アクア
    Posted @ 2007/05/27 0:19
    もう1エントリ作ってください。^^
  • # re: VB マイグレーション ByRef と ByVal
    Jitta
    Posted @ 2007/05/28 21:46
    ごめん。東京#8に続く。
    (ここでも書くか!?)
  • # re: VB マイグレーション ByRef と ByVal
    Jitta
    Posted @ 2007/12/18 22:48
    続き
    http://blogs.wankuma.com/jitta/archive/2007/05/28/78598.aspx
    http://blogs.wankuma.com/jitta/archive/2007/05/31/78993.aspx
  • # Wonderful beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept
    Wonderful beat ! I would like to apprentice while
    Posted @ 2019/04/07 21:40
    Wonderful beat ! I would like to apprentice while you amend your web site, how could i subscribe
    for a blog web site? The account aided me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright
    clear concept
  • # I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide on your guests? Is going to be again regularly to inspect new posts
    I just could not go away your website prior to sug
    Posted @ 2019/07/16 23:05
    I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide
    on your guests? Is going to be again regularly to
    inspect new posts
  • # I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide on your guests? Is going to be again regularly to inspect new posts
    I just could not go away your website prior to sug
    Posted @ 2019/07/16 23:06
    I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide
    on your guests? Is going to be again regularly to
    inspect new posts
  • # I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide on your guests? Is going to be again regularly to inspect new posts
    I just could not go away your website prior to sug
    Posted @ 2019/07/16 23:07
    I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide
    on your guests? Is going to be again regularly to
    inspect new posts
  • # I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide on your guests? Is going to be again regularly to inspect new posts
    I just could not go away your website prior to sug
    Posted @ 2019/07/16 23:08
    I just could not go away your website prior to suggesting that I actually loved the standard info an individual provide
    on your guests? Is going to be again regularly to
    inspect new posts
  • # グッチバッグコピー
    vdzqzqe@ocn.ne.jp
    Posted @ 2021/11/23 9:42
    新品財布の通販専門店

    ●在庫情報随時更新!
    ◆お客さんたちも大好評です:
    ◆新品種類がそろっています。
    ◆S/ SS/ N/ 品質 シリアル付きも有り 付属品完備!
    ◆品質がよい、価格が低い、実物写真!
    ◆当社の商品は絶対の自信が御座います。
    ◆100%品質保証 !
    満足保障100%!
    広大な客を歓迎してご光臨!
タイトル
名前
Url
コメント