Macro substitution / substitution of variable with value 宏替换 / 用值替换变量

huangapple go评论62阅读模式
英文:

Macro substitution / substitution of variable with value

问题

尝试替换从父表单(即ThisForm)接收到的值,该值等于BsGroupMaster

当将BsGroupMaster替换为ThisForm时出现错误。有人能帮助我,以便显示变量值。

Public Sub Eof(ByVal ThisForm As String)
    If BsGroupMaster.MyDataTbl.Rows.Count = 0 Then  ' 数据文件为空
        MessageBox.Show("数据文件为空,没有记录。")

    ElseIf BsGroupMaster.MyRowPosition = (BsGroupMaster.MyDataTbl.Rows.Count - 1)
        MessageBox.Show("检查是否到达文件末尾,不存在下一条记录。", "", MessageBoxButtons.OK, MessageBoxIcon.Information)

    ElseIf BsGroupMaster.MyRowPosition < (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then ' 存在记录
        BsGroupMaster.MyRowPosition = BsGroupMaster.MyRowPosition + 1
        BsGroupMaster.showRecords()
    End If
End Sub
End Module

以前我使用Visual Fox-pro,那里有一个简单的命令来将变量替换为值,您只需在变量前面加上&amp;,例如:

&amp;LastForm

它将显示存储在变量中的“BsGroupForm”值。

英文:

Trying to substitute value received from parent form namely ThisForm which is equal to BsGroupMaster.

When BsGroupMaster is replaced with ThisForm there is error. Can someone help me out so that the variable value is displayed.

    Public Sub Eof(ByVal ThisForm As String)
        If BsGroupMaster.MyDataTbl.Rows.Count = 0 Then  &#39; Data file is empty
            MessageBox.Show(&quot;Data file is empty, There are no records. &quot;)

        ElseIf BsGroupMaster.MyRowPosition = (BsGroupMaster.MyDataTbl.Rows.Count - 1)
            MessageBox.Show(&quot;Check End of file reached, No next record exists.&quot;, &quot;&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information)

        ElseIf BsGroupMaster.MyRowPosition &lt; (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then &#39; There are records
            BsGroupMaster.MyRowPosition = BsGroupMaster.MyRowPosition + 1
            BsGroupMaster.showRecords()
        End If
    End Sub
End Module

Previously I worked with Visual Fox-pro there was simple command to substitute the variables with values you just have to put an &amp; in front of the variable example:

&amp;LastForm

it will display the "BsGroupForm" the value stored in the variable.

答案1

得分: 2

When I jumped over to MS Access (and VBA), and that of vb.net: I have not ONCE in 20 years of writing code ever missed the macro substitution feature of FoxPro.

Why? Well, .net is an object-oriented system and as a result, I can't think of a case when I needed to substitute a variable in this way.

So, for example, you can get a control from a form like this:

' get value of a control on this form
Dim strMyControl = "txtCity"

Debug.Print($"Value of City TextBox (txtCity) = {txtCity.Text}")
Debug.Print($"Value of City TextBox (by string var) = {Me.Controls(strMyControl).Text}")

So, note how I was able to use a "string variable" to get that control on the screen. No substitution was required (for the first line of syntax where I hard coded txtCity.Text).

So, in your case?

Well, you don't show the calling code - and that we really need to see!

In place of passing the string value, why not pass the table (which I assume is a data table).

So, the code then becomes like this:

Public Sub Eof(ByVal BsGroupMaster As bsGroupClass)

If BsGroupMaster.MyDataTbl.Rows.Count = 0 Then ' Data file is empty
MessageBox.Show("Data file is empty, There are no records. ")

ElseIf BsGroupMaster.MyRowPosition = (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then
MessageBox.Show("Check End of file reached, No next record exists.",
"",
MessageBoxButtons.OK,
MessageBoxIcon.Information)

ElseIf BsGroupMaster.MyRowPosition < (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then ' There are records
BsGroupMaster.MyRowPosition = BsGroupMaster.MyRowPosition + 1
BsGroupMaster.showRecords()
End If

End Sub

In other words, what is the "type" of BsGroupMaster? Is that a class, or a form, or what?

So, the above explains why in 20+ years of writing in VB, I have NEVER missed the macro substitution feature.

In a nutshell, it means you're approaching the software design the wrong way, and with a "different" design, you find you have several workarounds for this missing feature.

It's not that the above explanation is good, or bad, or right, or wrong. It is the way modern compiled languages systems work.

The other main issue is that the .net framework does not allow code to be modified AFTER it's been compiled.

FoxPro code expressions were thus not compiled, or in fact, it has some provisions for such non-compiled code to run. However, it's not a practical solution for modern software systems.

Anyway, if you show how this call is being made. You might say perhaps have to pass two values, such as

Public Sub Eof(ByVal BsGroupMaster As bsGroupClass, MyTable as datatable)

However, without knowing what type of object BsGroupMaster is, it's somewhat difficult to suggest which of the two ideas above are best, or even another approach.

However, as noted, I have never missed the macro substitution feature in vb.net, so with a bit more information, I am sure a good workable workaround can be found here.

In most cases, the use of an object, or even some type of collection object, gets you around the "lack of" a substitution feature in the .net framework.

英文:

When I jumped over to MS Access (and VBA), and that of vb.net: I have not ONCE in 20 years of writing code ever missed the macro substitution feature of FoxPro.

Why? Well, .net is a object orientated system and as a result, I can't think of a case when I needed to substitute a variable in this way.

So, for example, you can get a control from a form like this:

    &#39; get value of a control on this form
    Dim strMyControl = &quot;txtCity&quot;

    Debug.Print($&quot;Value of City TextBox (txtCity) = {txtCity.Text}&quot;)
    Debug.Print($&quot;Value of City TextBox (by string var) = {Me.Controls(strMyControl).Text}&quot;)

So, note how I was able to use a "string variable" to get that control on the screen. No substitution was required (for the first line of syntax where I hard coded txtCity.Text).

So, in your case?

Well, you don't show the calling code - and that we really need to see!

In place of passing the string value, why not pass the table (which I assume is a data table).

So, the code then becomes like this:

Public Sub Eof(ByVal BsGroupMaster As bsGroupClass)

    If BsGroupMaster.MyDataTbl.Rows.Count = 0 Then  &#39; Data file is empty
        MessageBox.Show(&quot;Data file is empty, There are no records. &quot;)

    ElseIf BsGroupMaster.MyRowPosition = (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then
        MessageBox.Show(&quot;Check End of file reached, No next record exists.&quot;,
                        &quot;&quot;, MessageBoxButtons.OK,
                        MessageBoxIcon.Information)

    ElseIf BsGroupMaster.MyRowPosition &lt; (BsGroupMaster.MyDataTbl.Rows.Count - 1) Then &#39; There are records
        BsGroupMaster.MyRowPosition = BsGroupMaster.MyRowPosition + 1
        BsGroupMaster.showRecords()
    End If

End Sub

In other words, what is the "type" of BsGroupMaster? Is that a class, or a form, or what?

So, the above explains why in 20+ years of writing in VB, I have NEVER missed the macro substitution feature.

In a nutshell, it means your approaching the software design the wrong way, and with a "different" design, you find you have several workarounds for this missing feature.

It not that the above explain is good, or bad, or right, or wrong. It is the way modern compiled languages systems work.

The the other main issue is that the .net framework does not allow code to be modified AFTER it's been compiled.

FoxPro code expressions were thus not compiled, or in fact it has some provisions for such non compiled code to run. However, it not a practical solution to modern software systems.

Anyway, if you show how this call is being made. You might say perhaps have to pass two values, such as

Public Sub Eof(ByVal BsGroupMaster As bsGroupClass, MyTable as datatable)

However, without knowing what type of object BsGroupMaster is, it's somewhat difficult to suggest which of the two ideas above are best, or even another approach.

However, as noted, I have never missed the macro substitution feature in vb.net, so with a bit more information, I am sure a good workable work around can be found here.

In most cases, use of a object, or even some type of collection object gets you around the "lack of" a substitution feature in the .net framework.

huangapple
  • 本文由 发表于 2023年6月5日 02:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定