とりこらぼ。

Learn from yesterday,
live for today,
hope for tomorrow.

目次

Blog 利用状況

ニュース

プロフィール

  • 名前:とりこびと
    とるに足らない人間です。

  • Wankuma MVP
    for '平々凡々'

Web Site

  • Memo(Of T)

もうひとつの Blog

広告っぽい

書庫

日記カテゴリ

Main プロシージャ (とYシャツと私)

やっとこさ Visual Basic ネタで。ちょっとした思い出話です。(タイトルもなつかしさを出してみました。)

<ATTENTION>
   私は Visual Basic.NET 2003 と Visual Basic 2005 しかまともに(まともでもない)触っていない人間です。
</ATTENTION>

今回のお題はMain プロシージャ。エントリするポイントのことです(?)。え?ステートメントの解説とかじゃないの?やる気あんのか?と思われた方、続けざまにガッカリだよ!と思っていただければ幸いです(だって収拾つかなくなりそうだったから。orz)。

実のところワタクシ、Main プロシージャってVisual Studio 触り始めて幾日か過ぎてから知りました。Visual Basic も .NET も MSDN もサッパリ知らないまま、ぺらっぺらの軽い参考書を片手にゴソゴソいたのですが、

'Main プロシージャ' の 'M' の字も出てこなかったよ、ママン    。゚(゚´Д`゚)゜。

ちなみに、どこで Main プロシージャの存在を知ったかというと・・・

C# と VB.NET の入門サイト:二重起動を確実に禁止する

いわずと知れたじゃんぬねっとさんのところですw


さぁさぁ!初心にかえって、まずはMSDNから。←基本ですよね♪

MSDN:Visual Basic の Main プロシージャ(http://msdn2.microsoft.com/ja-jp/library/ms235406(VS.80).aspx) より。


すべての Visual Basic プログラムには、Main という名前のプロシージャが含まれている必要があります。このプロシージャは、アプリケーションの開始点となり、アプリケーションの総合的な制御を行います。.NET Framework は、アプリケーションをロードして、アプリケーションに制御を渡せる状態になると、Main プロシージャを呼び出します。
最初に実行するコードは Main 内に記述します。Main では、プログラムの起動時に最初に読み込まれるフォームを決定したり、同一アプリケーションが既にシステム上で稼動しているかどうかを調べたり、アプリケーションで使用する一連の変数を作成したり、アプリケーションで必要なデータベースを開いたりできます。

要するに、一番最初は Main というプロシージャから実行されるわけですね。つまり、これがエントリするポイントです、と。(要約してない。)

リンク先は比較的説明が穏やか(?)で分かりやすいので、さくさくっとまとめておきますと、

  • Main プロシージャの要件
    • コンソール アプリケーションとWindows フォーム アプリケーションには Main プロシージャ が必要なの。でもね、Windows フォーム アプリケーションでは Visual Basic コンパイラが自動的に Main プロシージャを生成するので、書かなくてもいいよ。
    • クラス ライブラリには Main プロシージャは必要ないって。
  • Main プロシージャの宣言
    • Main は次の 4 種類あるよ。
      • Sub Main()
      • Sub Main(ByVal cmdArgs() As String)
      • Function Main() As Integer
      • Function Main(ByVal cmdArgs() As String) As Integer
    • クラスで Main プロシージャを宣言する場合は、Shared キーワードが必要なの。モジュールで Main プロシージャを宣言する場合、Shared キーワードはいらないの。

さて、なぜ参考書に載っていないからってMain プロシージャの存在に気づかなかったのでしょう?

Visual Studio .NET 2003 でも Visual Studio 2005 でも、Visual Basic で WindowsApplication のテンプレートを使用すると、Form1.vb というファイルに 'Form1' というクラスが作成されますが、この中に Main プロシージャはありません(Visual Studio 2005 では Form1.Designer.vb というファイルも作成されますが、この中にもありません。)。 これは先ほどの内容のとおり、Visual Basic コンパイラが自動的に生成してくれるからですね。なんとおせっかい便利なんでしょう♪

・・・そりゃ知らなくて当然ですよね?(いや、事前にしっかりチェックしてないからなんですけどね。)

ちなみに C♯では Visual Studio .NET 2003 だとForm1.cs の中に Visual Studio 2005 でも、Program.cs というファイルの中に Program クラスが作成され、そこにMain プロシージャ があるんですね。


というわけで、 Main プロシージャを書いてみました。とにもかくにも基本の基本、"Hello, World!" を出力するプログラム!!!

1つ目は MSDN:Visual Basic バージョンの Hello World! (http://msdn2.microsoft.com/ja-jp/library/3cf7t4xt(VS.80).aspx) にあるモジュールを使用したものですね。


Imports System
Imports System.Windows.Forms

Module Hello
  Sub Main()
    ' なぜか Msgbox だったので MessageBox にしてみたし。     MessageBox.Show("Hello, World!")
  End Sub
End Module

2つ目は 上記をC♯のように、Program クラスにしたもの。

Imports System
Imports System.Windows.Forms

Public Class Program
  Public Shared Sub Main()
    MessageBox.Show("Hello, World!")
  End Sub
End Class

おまけとして フォームを表示するだけの Main プロシージャ。

Imports System
Imports System.windows.Forms

Public Class Hello
  Public Shared Sub Main()
    Application.Run(New Form)
  End Sub
End Class

・・・なつかしいですね♪(甘酸っぱいね。)

とまぁ、今回はこのくらいで勘弁してやるわぁ!(オチがないよ~。)



あ、書いていて、いくつか個人的に気になることがでてきました。今後、エントリとして書くかもしれません(調べても分からなくて書けないかもしれません。)。

  • Main プロシージャを呼ぶ仕組み。
  • なぜプロシージャから始まるの?しかも Module や Shared なのね。(クラスを指定するようなイメージのほうがすっきりしそう。)
  • Visual Basic コンパイラが自動的に Main プロシージャを生成する仕組み。(と、実際どんなプロシージャになっているか。)

<余談>
Visual Studio はとても便利なシロモノですねぇ。私のように Main プロシージャの存在を知らない状態でもそこそこのものを作れてしまいます。これを'便利な世の中とする'のか'業界の今後を危惧する'のかは・・・(つ[カンペ] ここで「自由だ~~!!」という。)
</余談>

# タグ付けがおかしかったので修正しました。

投稿日時 : 2007年3月8日 17:22

Feedback

# re: Main プロシージャ (とYシャツと私) 2007/03/08 17:47 じゃんぬねっと

VB6 からあるので、プログラマであれば知っていると思ったのですが、そういうものでもないみたいですね。
C から入ると 「なぜないんだろ」 という衝動に駆られるので検索して、疑問解決というパターンも多そうですが。

# re: Main プロシージャ (とYシャツと私) 2007/03/08 17:47 じゃんぬねっと

いかんいかん。

s/プログラマであれば/Windows アプリケーション プログラマ/gm

視野が狭くなってる。

# re: Main プロシージャ (とYシャツと私) 2007/03/08 18:03 ぽぴ王子

しむらー!ATTENTION!ATTENTION!

<ATTENTION>
私は Visual Basic.NET 2003 と Visual Basic 2005 しかまともに(まともでもない)触っていない人間です。
</ATTENTION>

やはりそれまで見た、触った、揉んだ…じゃないプログラミングした環境にないものだと、そこから想像するのは難しいかもしれませんね。

C プログラマだと「あるのが当たり前」なので、なぜないんだろう?という疑問がわきますが、VB6 でフォームしか触ったことがないんだよーという人の場合は「なにそれ?うまいの?」という話になっても仕方がないのかなと。
ただ、それだとプロのプログラマとしては失格の烙印を押されても仕方がないかなあとは思ったり思わなかったり。

知らないこと自体は別に恥ではないので、これから覚えていけばいいわけですが。

# re: Main プロシージャ (とYシャツと私) 2007/03/08 18:47 とりこびと

コメントありがとうございます。

------------------------------------------------------

>じゃんぬねっとさん

そうですね。私の場合、プログラムに関わりあうことがあまりに突然の出来事であったため、予習が足りなかったんだと思っています。(本当は書籍のアタリが悪かったと思ってしまいたいw)

いまでもWindows アプリケーション プログラマを名乗れない身なのでがんばりまする。

------------------------------------------------------

>ぽぴ王子さん

ん~。Visual Basic って知らなくても云々ってところが最近懐疑的なんですよね。(あ、IDE経由での話です。)

IDEが'やってくれていること'を知っていればどうとでもなる話でしょうが、その'やってくれていること'を知るためにC♯のプロジェクトを作ってみている自分がちょっと切ないですね・・・。

# re: Main プロシージャ (とYシャツと私) 2007/03/08 21:56 ながせ

Visual C++を使っていたころに、MFCアプリケーションを作るとWinMain()がどこにあるのかわからず、探し回った思い出もあります^-^;

# re: Main プロシージャ (とYシャツと私) 2007/03/09 9:19 とりこびと

ながせさん、はじめまして♪コメントありがとうございます。

ってことはVisual Basicだけの悪いクセでもないんですね。(C++って0.0001%くらいしか触ったことないんです。orz)

# re: Main プロシージャ (とYシャツと私) 2007/03/09 13:12 ぽぴ王子

> IDEが 'やってくれていること' を知っていればどうとでもなる話

そーですねー
VB(6までのやつ)だと、主にそういった方向に倒そうとしている部分が見られていたと思います。
なんというか、あまり知らなくてもなんとかなるよ!白鳥の足の動きを知らなくても優雅な姿は
見せられるよ!みたいな。
ただ、それで満足している分は全然オッケーで、そもそもそういう思想なんだからって感じで
考えてればいいわけなんですけど、それだけで飽き足らなくなってきた人とか、かゆいところに
手が届かないんだよ!白鳥がどうやって泳いでいるか、その動きから知りたいんだよ!なんて
考え出すと、ちょっと物足りなく思えたり…って感じはあるかも。
VB.NET だとそれなりに進化してる(逆に言えばそれに伴ってオブジェクト指向的な制約も増えてる)
と思うんですが、でも VB はこうあるべき!みたいなのがあって、それに準拠するには「Main
関数がなくても実行できること」みたいなのがあるのかも…なんて思ってみたり。
(ユーザーに Main 関数を意識させないこと、って感じ?)

WinMain() の話はそれとは別ですが(笑)

VB よりも C# が好き、って人の場合はそういうところもあるかもしれないですね。
結局「人それぞれ」につながっちゃうんですけど。

そういやεπιστημηさんが前にそんな感じの書いてましたね。
(C++ 使いから見た VB っぽい感じで)

# Visual Basic コンパイラ と Main プロシージャ。その1。 2007/03/09 15:10 とりこびと ぶろぐ。

Visual Basic コンパイラ と Main プロシージャ。その1。

# re: Main プロシージャ (とYシャツと私) 2007/03/09 15:21 とりこびと

ぽぴ王子さん、コメントありがとうございます。

最近ほんとにVisual Studioを分解したい衝動に駆られているわけですがw

C♯だとプロジェクトのプロパティで指定した内容がちゃんとコードに落ちてくるんですね。(NameSpace など)
そのあたりはすこしうらやましいです。

Main 関数がなくても実行できること = ユーザーに Main 関数を意識させないこと

コンパイラが手伝ってまでする必要があることなのでしょうか・・・。
IDEはユーザーに Main 関数を意識させないけれど、やはりコンパイラはMainが必須でお願いしたいですね。

今のところ、知りたがりな自分が少しうれしくなります。

# re: Main プロシージャ (とYシャツと私) 2018/08/28 11:44 wwwe

http://www.yeezyboost-350v2.us.com
http://www.lebron15.net
http://www.yeezyboost-350.uk
http://www.kyrie4.org
http://www.yeezyboost350v2shoes.us.com
http://www.goldengooseoutlet.us
http://www.jordansforcheap.us.com
http://www.air-max.us.com
http://www.adidasnmds.com
http://www.kobebasketballshoes.us.com

# Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you aided me. 2018/10/07 11:51 Heya i am for the first time here. I found this bo

Heya i am for the first time here. I found this board and I find It truly useful & it helped me
out a lot. I hope to give something back and help
others like you aided me.

# I loved as much as you'll receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly 2019/04/03 21:31 I loved as much as you'll receive carried out righ

I loved as much as you'll receive carried out right here.
The sketch is attractive, your authored material stylish.
nonetheless, you command get got an shakiness over that you wish
be delivering the following. unwell unquestionably come further formerly again as
exactly the same nearly very often inside case you shield
this increase.

# Ahaa, its fastidious discussion regarding this paragraph at this place at this webpage, I have read all that, so at this time me also commenting at this place. 2019/04/08 5:06 Ahaa, its fastidious discussion regarding this pa

Ahaa, its fastidious discussion regarding this paragraph at this
place at this webpage, I have read all that, so at this time me also commenting at this place.

# An outstanding share! I have just forwarded this onto a co-worker who has been conducting a little research on this. And he actually bought me lunch simply because I discovered it for him... lol. So allow me to reword this.... Thanks for the meal!! But 2019/05/13 10:46 An outstanding share! I have just forwarded this o

An outstanding share! I have just forwarded this onto a co-worker who has been conducting a
little research on this. And he actually bought me lunch simply
because I discovered it for him... lol. So allow me to reword this....
Thanks for the meal!! But yeah, thanx for spending time
to talk about this subject here on your website.

# What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of more and more. 2019/07/17 10:15 What's up, every time i used to check weblog posts

What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of
more and more.

# What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of more and more. 2019/07/17 10:16 What's up, every time i used to check weblog posts

What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of
more and more.

# What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of more and more. 2019/07/17 10:17 What's up, every time i used to check weblog posts

What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of
more and more.

# What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of more and more. 2019/07/17 10:18 What's up, every time i used to check weblog posts

What's up, every time i used to check weblog posts here early in the dawn, since i like to gain knowledge of
more and more.

# I think this is among the most vital info for me. And i'm glad reading your article. But want to remark on some general things, The website style is great, the articles is really excellent : D. Good job, cheers 2019/07/24 11:47 I think this is among the most vital info for me.

I think this is among the most vital info for me.
And i'm glad reading your article. But want to remark
on some general things, The website style is great, the articles is really excellent : D.
Good job, cheers

# I think this is among the most vital info for me. And i'm glad reading your article. But want to remark on some general things, The website style is great, the articles is really excellent : D. Good job, cheers 2019/07/24 11:48 I think this is among the most vital info for me.

I think this is among the most vital info for me.
And i'm glad reading your article. But want to remark
on some general things, The website style is great, the articles is really excellent : D.
Good job, cheers

# I think this is among the most vital info for me. And i'm glad reading your article. But want to remark on some general things, The website style is great, the articles is really excellent : D. Good job, cheers 2019/07/24 11:49 I think this is among the most vital info for me.

I think this is among the most vital info for me.
And i'm glad reading your article. But want to remark
on some general things, The website style is great, the articles is really excellent : D.
Good job, cheers

# I think this is among the most vital info for me. And i'm glad reading your article. But want to remark on some general things, The website style is great, the articles is really excellent : D. Good job, cheers 2019/07/24 11:50 I think this is among the most vital info for me.

I think this is among the most vital info for me.
And i'm glad reading your article. But want to remark
on some general things, The website style is great, the articles is really excellent : D.
Good job, cheers

# I am sure this article has touched all the internet viewers, its really really pleasant post on building up new weblog. 2019/08/14 8:01 I am sure this article has touched all the interne

I am sure this article has touched all the internet viewers,
its really really pleasant post on building up new weblog.

# sAblcSaORh 2021/07/03 2:47 https://amzn.to/365xyVY

Im obliged for the post.Really looking forward to read more. Much obliged.

# Illikebuisse etcuj 2021/07/05 4:23 pharmaceptica.com

chloroquinolone malaria https://pharmaceptica.com/

# erectile breast tissue 2021/07/07 3:03 hydrochlroquine

hydroxychloroquine https://plaquenilx.com/# hydrocychloroquine

# re: Main ?????? (?Y?????) 2021/07/08 0:14 hydrochloquine

used to treat malaria chloro https://chloroquineorigin.com/# hydrocychloroquine

# re: Main ?????? (?Y?????) 2021/07/14 2:50 hydroxychloroquine for malaria

natural chloroquine https://chloroquineorigin.com/# what is hcq drug

# aohroarvipvf 2021/12/03 13:44 dwedayxkrh

https://hydrochloroquinefil.com/ hydroxychloroquine dose

# xkxhkxabsbkh 2022/05/10 9:43 tfesxm

hydochloroquine https://keys-chloroquineclinique.com/

# accpabbrneec 2022/05/18 11:52 rxvlbcma

is erythromycin a penicillin http://erythromycin1m.com/#

タイトル
名前
Url
コメント