【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?

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

A 回答 (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に違和感が。。。

以上。参考まで。
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

>これ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

お礼日時:2007/02/27 00:56

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!