VB2005で、Structureの配列を返すプログラムを以下のように書きたいのですが、そもそもVB6しか使ったことが無いもので、以下のような素数の結果を返すこのプログラムの書き方はVB2005らしいでしょうか?
Module Module1
Public Structure SosuuStatus
Public num As Integer
Public status As String
End Structure
Class Sosuu
Function SosuuCheck(ByVal st As Integer, ByVal ed As Integer) As SosuuStatus()
Dim i As Integer, j As Integer
Dim sosuu(0 To ed - st) As SosuuStatus
Dim cnt As Integer = 0
For i = st To ed
sosuu(cnt).num = i
sosuu(cnt).status = "" '初期化
If 1 = i Then
sosuu(cnt).status = "素数ではない"
ElseIf 0 = (i Mod 2) Then
sosuu(cnt).status = "素数ではない"
Else
For j = 3 To Math.Sqrt(ed)
If 0 = (i / j) Then
sosuu(cnt).status = "素数ではない"
End If
Next j
End If
If sosuu(cnt).status = "" Then
sosuu(cnt).status = "素数である"
End If
cnt = cnt + 1
Next i
SosuuCheck = sosuu
End Function
End Class
End Module
No.1ベストアンサー
- 回答日時:
>VB2005らしい
とは、また抽象的ですね--;
個人的な趣味も混ざっている事前提で私感を。。。
(一応いろいろな会社の方のソースを見てます。)
>Dim i As Integer, j As Integer
あまり一行では書かないです。
Dim i As Integer 'ループ元変数
Dim j As Integer '素数判定用変数
とコメント込みで複数行に分けて書きます。
>Dim sosuu(0 To ed - st) As SosuuStatus
Dim sosuu(ed - st) As SosuuStatus
0Toしか使わないです。規約の関係ですが。。。
>SosuuCheck = sosuu
return sosuu
なんとなくですが。
あとは、
>If 0 = (i / j) Then
これIf 0 = (i Mod j) Thenですよね?
元のソース、素数を返しません。^^;
本当に個人的な感想なんですが、Moduleの内側のclassに違和感が。。。
以上。参考まで。
回答ありがとうございます。
>これIf 0 = (i Mod j) Thenですよね?
これは失礼しました。
VBでreturnが使えるとは、驚きですね。
今悩んでいるのは、 StructureやClassの初期化でなかなか感覚がつかめず難儀しています。
書籍でマスターしようかと考えているのですが、VB.NETのオブジェクト指向について一押しの本をご存知の方教えてください。
以下、form.vb部です。
フォームにボタンと、リストボックス(lstMsg)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim test_sosuu As New Sosuu
Dim sosuu() As SosuuStatus
sosuu = test_sosuu.SosuuCheck(1, 100)
Dim i As Integer
For i = LBound(sosuu) To UBound(sosuu)
lstMsg.Items.Add(CStr(sosuu(i).num) & " " & sosuu(i).status)
Next i
End Sub
End Class
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
Web画面の文字をVB6で取得したい
-
【ExcelVBA】インデックスが有...
-
VBA 別ブックから条件に合うも...
-
Excel VBA 文字列のセルを反映...
-
VB.net(VB)で、フォームにExcel...
-
配列のペースト出力結果の書式...
-
エクセルのマクロについて教え...
-
[VB.net] ボタン(Flat)のEnable...
-
vbaにてseleniumを使用したedge...
-
メールの件名をデコードしたい
-
VBA 別ブックからコピペしたい...
-
VBA実行後に元のセルに戻りたい
-
エクセルのマクロについて教え...
-
エクセルのマクロについて教え...
-
エクセルのマクロについて教え...
-
エクセルのマクロについて教え...
-
Excel-VBAのmsgBox()の不思議
-
郵便番号検索APIにてget Elemen...
-
VBA 入力箇所指定方法
-
エクセルのマクロについて教え...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
VBA 別ブックからコピペしたい...
-
Vba ファイル書き込み時に書き...
-
Excel_VBAについて質疑です。(...
-
VBAの間違い教えて下さい
-
VBA コードどこがおかしいですか?
-
VBA 円グラフ 特定条件に一致し...
-
VBA 別ブックから条件に合うも...
-
pdfファイルの複数添付 引数の型
-
【ExcelVBA】インデックスが有...
-
ExcelVBAマクロで実行した時の疑問
-
Vba UserformからExcelシートの...
-
VBA初心者です。次のVBAコード...
-
Outlookの「受信日時」「件名」...
-
Excel 範囲指定スクショについ...
-
vbs ブック共有を解除
-
配列のペースト出力結果の書式...
-
Excel VBAで値を変えながら、pd...
-
VB.net(VB)で、フォームにExcel...
-
vbaにてseleniumを使用したedge...
-
ExcelVBA シート名を複数セルか...
おすすめ情報