Click here to Skip to main content
15,889,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on converting some MatLab code to VB.NET. I'm not a programmer by trade, but I've taught myself some. Unfortunately, I'm very new VB .NET. I have two questions.

1. I'm getting an error in the If statement in the For loop, specific line is noted. Here is the code:

Imports System.Text.RegularExpressions
Imports System
Imports System.IO


Module ConforMIS_Data

    Public Structure Properties
        Public Name As Array
        Public Location As String
        Public File As String
        Public Prop As String
        Public Column As String
        Public Type As String
    End Structure

    
    Public Props As Properties
    Public PropNames() As String



    Sub Main()
        Dim path As String = "X:\default.config"
        Dim config_text() As String = File.ReadAllLines(path)

        Dim p As Integer = 0
        For i As Integer = 0 To config_text.Length - 1
            Dim PropName_RegEx As New Regex("(?<=\[PROPS.).*(?=\])")
            Dim PropName As Match = PropName_RegEx.Match(config_text(i))
            If (PropName.Length <> 0) Then
                PropNames(p) = PropName.ToString() 
                p = p + 1
                Console.WriteLine(PropName.ToString())
            End If
        Next

        Props.Name = PropNames
        Console.WriteLine(Props.Name)
        Console.Read()

    End Sub


Error: Object reference not set to an instance of an object.

I think it has something to do with declaring PropNames AS, but not actually declaring it as a new variable.

Anyhow -- if anyone has thoughts there, I would really appreciate it.

2) In MatLab, you can dynamically name structured arrays with the string value of another variable:

A = "Home"
Welcome.(A) = "Hello World!"
Disp(Welcome.Home)


The output would be "Hello World". Unfortunately, I can't find a way to do this in VB. Anyone know a simple way?

Thanks in advance!

Mike
Posted

1 solution

You've declared PropNames() As String
It's an empty array.
The next instance where you're using is PropNames(p)= xxx
You're assigning to an empty array.

Declare PropNames as an array of a definite number of strings
Eg: Dim PropNames(10) As String - will hold 10 strings

You may later on use the 'ReDim' statement to resize arrays. Check it out in the documentation
 
Share this answer
 
Comments
Michael Beasley 30-Nov-10 12:43pm    
That worked out. I used

ReDim Preserve PropNames(p+1)

I put that right before adding a string in the If statement.

Thanks for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900