动态字段名称和对象错误 [英] Dynamic Field Names & Object Error

查看:192
本文介绍了动态字段名称和对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些MatLab代码转换为VB.NET.我不是行业程序员,但是我已经自学了一些.不幸的是,我是一个非常新的VB .NET.我有两个问题.

1.我在For循环的If语句中遇到错误,请注意特定的行.这是代码:

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



错误:对象引用未设置为对象的实例.

我认为这与声明PropNames AS有关,但实际上并没有将其声明为新变量.

无论如何-如果有人在那里有想法,我将不胜感激.

2)在MatLab中,您可以使用另一个变量的字符串值动态命名结构化数组:



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)



输出将是"Hello World".不幸的是,我找不到在VB中执行此操作的方法.有人 知道一种简单的方法吗?

提前致谢!

Mike



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

推荐答案

您已将PropNames()声明为String
它是一个空数组.
您正在使用的下一个实例是PropNames(p)= xxx
您正在分配一个空数组.

将PropNames声明为一定数目的字符串的数组
例如:Dim PropNames(10)As String-将容纳10个字符串

您稍后可以使用"ReDim"语句来调整数组的大小.在文档中查看
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


这篇关于动态字段名称和对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆