• 締切済み

VB2005で、Structureの配列を返すプログラムを以下のように書きたい

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

  • hdkoa
  • お礼率0% (1/117)

みんなの回答

回答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に違和感が。。。 以上。参考まで。

hdkoa
質問者

お礼

回答ありがとうございます。 >これ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

  • VB6のTYPE文をVB.NETのStructureに変えるとき

    VB6のTYPE文をVB.NETのStructureに変えるとき 下記VB6のコードをVB.NETのStructureに変える場合 Type kouzou1 i As Integer j As Integer a As String * 20 b As String * 50 End Type を下記にしてみたのですが *20,*50のところは、どのように表現するのでしょうか。 Structure kouzou1 Dim i As Integer Dim j As Integer Dim a As String * 20 <- ステートメントの終わりを示してくださいのエラーになる。 Dim b As String * 50 <- ステートメントの終わりを示してくださいのエラーになる。 End Structure お教え下さい。

  • VBプログラム エラーが出ます

    Public Class Form1 Private number As Integer Private rows As Integer Private columns As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Not Integer.TryParse(TextBox1.Text, rows) OrElse _ Not Integer.TryParse(TextBox2.Text, columns) OrElse _ Not Integer.TryParse(TextBox3.Text, number) Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If Me.Bounds = New Rectangle(10, 10, 1300, 800) Dim cnt As Integer = 0 For k As Integer = 1 To number For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() cnt += 1 tb.Name = "tb" + cnt.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + 80 tb.Left = (j - 1) * 60 + 70 * (columns * (k - 1)) + 10 tb.Width = 50 Next Next Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sum As Double Dim cnt As Integer = 0 For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() cnt += 1 : If cnt > rows * columns Then cnt = 1 tb.Name = "tbA" + cnt.ToString Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + (80 + 40 * rows) tb.Left = (j - 1) * 60 + 10 tb.Width = 40 sum = 0 For k As Integer = 1 To number sum += Double.Parse(CType(Me.Controls("tb" + (cnt + (rows * columns * k-1))).ToString(), TextBox).Text)    エラー Next tb.Text = sum.ToString() Next Next End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Do Until Me.Controls.Count <= 6 For Each tbD As Object In Me.Controls If CType(tbD, Control).Name Like "tb*" Then Me.Controls. Remove(tbD) Next Loop End Sub End Class エラーと書いている箇所 どう直したらよいのでしょうか。 縮小変換しないで呼び出されるアクセス可能なitemがないためオーバーロードの解決に失敗しました と出ます。 あとwindowsの画面の大きさを入力される値が小さければ少し小さいwindowsになるように変えたいのですがどうしたよいのでしょう。

  • VBのプログラム

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Not Integer.TryParse(TextBox1.Text, rows) OrElse _ Not Integer.TryParse(TextBox2.Text, columns) OrElse _ Not Integer.TryParse(TextBox3.Text, number) Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If Me.Bounds = New Rectangle(10, 10, 1300, 900) Dim cnt As Integer = 0 For k As Integer = 1 To number For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() cnt += 1 tb.Name = "tb" + cnt.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 20 + 70 tb.Left = (j - 1) * 50 + 60 * (columns * (k - 1)) + 5 tb.Width = 55 Next Next Next End Sub これはボタンを押すと テキストボックスに入力された値分だけテキストボックスが出てくるというものです。 例えば 3 4 4とテキストボックスに値が入力された場合 3行4列が4個分テキストボックスが出てきます。 しかし6 6 6と入力した場合画面に入りきらなくなってしまいます。 大きな値を入力しても画面に入れたいです。 これはスクロールなどしてできるようにしたいです。 どのようなソースでできるのでしょうか。

  • Fortranの素数のプログラム

    5000万までの素数を求めるプログラムなのですが、私の作ったプログラムは実行時間26秒くらいかかります。 先生が言うには10秒台が出るとのことですが、私は頑張っても時間を短くすることができません。 下に私の作ったプログラムを載せますので短くする方法を教えて下さい。 integer table(2:50000000),pno(50000000),cnt,m,i,j m=sqrt(50000000.) do 10 i=2,m do 10 j=i*2,50000000,i table(j)=1 10 continue do 20 i=2,50000000 if(table(i).eq.0)then cnt=cnt+1 end if 20 continue write(6,610)cnt 610 format('sosu no goukei =',i8) end

  • VB添削

    このプログラムは 例えば3 3 4とテキストボックスに数字が打ち込まれると 3×3行列が4個分 のテキストボックスがでてきます。 ここに数字を打ち込んでいき、ボタン2を押すと3×3のテキスト トボックスが出てくると同時に足し算した結果が出てくるようにしたいです。 以下のプログラムはできたところまで作成しています。 どこを直せばよいのでしょうか。 Public Class Form1 Private number As Integer Private rows As Integer Private columns As Integer Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i As Integer = 1 To 3 AddHandler Me.Controls("TextBox" & i).TextChanged, AddressOf TextBox_TextChanged Next End Sub Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) If System.Text.RegularExpressions.Regex.IsMatch(CType(sender, TextBox).Text, "[^0-9]") Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) With CType(sender, TextBox) .Text = .Text.Substring(0, .Text.Length - 1) .SelectionStart = .Text.Length End With End If Dim cnt1 As Integer Dim cnt2 As Integer Dim cnt3 As Integer If Integer.TryParse(TextBox1.Text, cnt1) And Integer.TryParse(TextBox2.Text, cnt2) And Integer.TryParse(TextBox3.Text, cnt3) Then For k = 1 To cnt3 For i = 1 To cnt1 For j = 1 To cnt2 Dim tb As TextBox = New TextBox() tb.Name = "tb" + i.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 28 + 55 tb.Left = (j - 1) * 30 + 40 * (cnt2 * (k - 1)) + 10 tb.Width = 25 Next Next Next End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Bounds = New Rectangle(10, 10, 1350, 800) Me.AutoScroll = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sum As Double Dim cnt As Integer = 0 For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() cnt += 1 : If cnt > rows * columns Then cnt = 1 tb.Name = "tb" + cnt.ToString Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + (80 + 40 * rows) tb.Left = (j - 1) * 60 + 10 tb.Width = 40 sum = 0 For k As Integer = 1 To number sum += Double.Parse(CType(Me.Controls("tb" + (cnt + (rows * columns * (k - 1))).ToString()), TextBox).Text) Next tb.Text = sum.ToString() Next Next End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub End Class

  • 順列のプログラムについて(VB)

    下記が、組み合わせを求めるプログラムです。 これのどこを変更すれば、順列を列挙するプログラムになりますか(ただし、重複順列でない。例、5P2=5*4=20通り)?教えていただけませんか? Public Class Form1 Dim w0, w1, count As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "コンビネーションサーチ" Label1.Text = "個の中から" Label2.Text = "個とる組リスト" Button1.Text = "計算" TextBox1.Text = "6" TextBox2.Text = "3" TextBox3.Text = "" TextBox3.ScrollBars = ScrollBars.Vertical count = 0 End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, m As Integer n = TextBox1.Text m = TextBox2.Text w1 = " }" w0 = "{ " TextBox3.Text = "" count = 0 combisearch(1, n, m) MsgBox("組み合わせ個数は、" & count & "個です。") End Sub Sub combisearch(ByVal st, ByVal ed, ByVal depth) Dim i, temp If depth - 1 < 1 Then For i = st To ed TextBox3.Text &= w0 & i & w1 & vbCrLf count += 1 Next Else For i = st To ed temp = w0 w0 &= i & " " combisearch(i + 1, ed, depth - 1) w0 = temp Next End If End Sub End Class

  • VB6.0 →VB2005 のアップグレードでエラー表示

    VB6.0の標準モジュールでは Private Type typset a As String b As String c As Integer End Type Public make() As typset と記述してエラーもなく正常なのですが、VB2005にアップグレードしたときに、 Option Strict Off Option Explicit On Module Module1 Private Structure typset Dim a As String Dim b As String Dim c As Integer End Structure Public make() As typset←▼エラー▼            End Module 【エラー内容】 'make' は、module 'Module1' をとおして型 'typset' を namespace 'WindowsApplication1' で公開することはできません。 structure部をPublicなどにしてもエラーは消えますが、参照先に影響が出てしまいます。 根本的な解決方法が分かりません。 教えていただけないでしょうか?

  • VB 2008: Do Whie...Loop文について

    Function FileGetChar(ByVal f As String, ByVal p As Integer) As String   Dim i As Integer = 1   Dim j As Integer = 0   Dim l As Integer   Dim n As Integer = FreeFile()   Dim c As Char   If File.Exists(f) Then     FileOpen(n, f, OpenMode.Random, OpenAccess.Read, , 1)     l = FileLen(f)     Do While (i + j <= l)       FileGet(n, c, i + j)       j = j - (Math.Abs(Asc(c)) > 255)       i = i + 1       If i > p Then         Exit Do       Else         c = ""       End If     Loop     FileClose(n)   End If   Return c End Function [イミディエイトウインドウ] ? FileGetChar("D:\Temp\Test.txt",1) "1" ? FileGetChar("D:\Temp\Test.txt",2) "2" と、一応は動作しています。 l------->ファイル長 i+j----->読み込みのカレントポジション p------->読み込み指示ポジション 今、悩んでいるのはDo...Loop文中のIf Else End If の追放。 何か妙手があれば教えて頂きたい。

  • macroについて教えてください

    こんにちは。以前こちらでPrivate SubについてMacroを教えていただきました。(あの後ログインパスワード等が不明になりお礼も出来ませんでしたが。。。回答頂いた方すみませんでした。) 下記がそのMacroですが、今回また少し変えることになり どのように変えていいのか分かりません。 前回は1~5はグレー、6~10は茶色・・・という形にしたのですが 今回は進捗率での管理をしたく、80%以下は白、80~90%は赤、90~100%は青としたいと思っています。 Private Sub Worksheet_Change(ByVal Target As Range) Dim iColors As Variant Dim rw As Long Dim CellCnt As Integer Dim col As Integer Dim col2 As Integer Dim i As Integer Dim j As Integer Dim k As Integer Dim c As Variant Dim ar() As Variant Dim Sh1 As Worksheet Set Sh1 = Worksheets("小児科Dr") col = Target.Cells(1).Column '制限された列 If Not (col = 4 Or col = 8 Or col = 12 Or col = 16) Then Exit Sub iColors = Array(16, 16, 16, 16, 16, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54) CellCnt = Target.Count ReDim ar(CellCnt - 1) For Each c In Target If c.Value <> "" Then If IsNumeric(c.Value) Then i = c.Value If i >= 11 Then i = 10 End If If i > 0 And i < 11 Then j = iColors(i - 1) Else j = 2 End If ar(k) = j k = k + 1 End If End If Next c rw = Target.Row Select Case col Case 4: col2 = 2 Case 8: col2 = 8 Case 12: col2 = 14 Case 16: col2 = 20 'Sh1.Cells(rw + 2, 13).Resize(Int(Target.Count / 3), 3).Interior.ColorIndex = j End Select InsideColors Sh1, rw, col2, CellCnt, ar() Set Sh1 = Nothing End Sub Private Sub InsideColors(sh As Worksheet, _ rw As Long, _ col As Integer, _ cnt As Integer, _ ar As Variant) 'sh[シート],rw[行], col[列],cnt[セル個数],iColor[色指数] Dim i As Integer Dim j As Integer Dim n As Integer Dim k As Integer If cnt Mod 5 > 0 Then '範囲行数 i = (cnt + 5 - (cnt Mod 5)) / 5 Else i = cnt / 5 End If rw = Int((rw - 1) / 5) + 1 '行再設定 j = ((rw - 1) Mod 5) + 1 '列設定 For n = j To cnt sh.Cells(rw + 2, col).Resize(i, 5).Cells(n).Interior.ColorIndex = ar(k) k = k + 1 Next n End Sub 毎回他の人を頼ってしまい、申し訳ないのですがお願いします。 また、前回分からなかったので1~5を指定するときに5回同じカラー番号を書いたのですがこちらも良かったら手直し方法を教えていただければ助かります。 宜しくお願いします。

  • 構造体 要素2次元配列の初期化方法

    Module Public Structure Elem Public nr As Integer Public type As Integer Public side()() As Double ' side(Nr.)(x|y|z)== normal vector Public Sub initialize() Dim side(100)() As Double End Sub End Structure Public e_enq As Elem() = New Elem(100) {} End Module  '****** 以下ボタンなどで本文内で動作させている *********** Dim i, j, k As Integer 1 ReDim e_enq(100).side(50)(2) 2 For k = 0 To 10 With e_enq(k) For j = 0 To 10 .side(j)(0) = 0.0  '*** .side(j)(1) = 0.0 .side(j)(2) = 0.0 Next j End With Next k 1 の方法での記述で newが必要とのエラーコメント 2 の方法でも '***部でやはり newが必要とのエラーコメントが出る  sideが1次元ならば、動作するのですが、2次元にするとエラーが発生します。  現在、プログラムが進展しなくて困っています。  どなたか、お詳しい方がいらしゃれば、どうかご指導をお願いいたします。

専門家に質問してみよう