帮助多个类之间的通信 [英] Help with communication between multiple classes

查看:43
本文介绍了帮助多个类之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我们为此活动提供的说明:


  1. 创建一个Item Class。它应该能够存储一个字符串,一个整数和一个double。
  2. 创建一个ItemList类。该类应包含Items数组。您可以对初始数组中的项目数进行硬编码,或者在将新项目插入数组之前使用ReDim来增加数组的大小。如果需要,此类应允许扩展
    数组的大小,并且必须包含一个函数,以便为Item数组中的所有项生成一个收据作为字符串。
  3. 创建应用程序接口要求用户输入3个值:文本字符串(部件名称),数量和销售的价格。
  4. 一旦输入3个值,用户就可以按一个按钮。将这些值存储在ItemList中的Item类对象数组中。清除下一个项目的文本框。
  5. 当用户在项目列表中输入N个项目后,他们可以按下UI中的第二个按钮来保存收据。按钮push生成的文本字符串应该调用ItemList类中的函数,该函数将收据创建为字符串,并将
    该字符串保存到名为Receipt_DATE.txt的文件中,其中DATE是今天的日期为YYYY-MM -DD。





该部分我坚持的是如何将我的3个值发送到ItemList类,以及如何将我的购物车数量增加一个。


Public Class Form1



    Private ShoppingCart As New ItemList

   私人柜台整数= 0

    Private Sub Form1_Load(sender As Object,e As EventArgs)处理MyBase.Load

        TextBox1.Text =""

        TextBox2.Text =""

        TextBox3.Text =""



$
   结束分





$ b    Private Sub Button1_Click(发件人为对象,e为EventArgs)处理Button1.Click

        Dim n As String

        Dim q As Integer

        Dim p As Double

        Dim i As Integer

       如果不是TextBox1.Text =""然后




            n = TextBox1.Text

            ShoppingCart.Items(Counter).PartName = n
$


            q = TextBox2.Text

            ShoppingCart.Items(Counter).Quantity = q



            p = TextBox3.Text

            ShoppingCart.Items(Counter).Price = p



            TextBox1.Text =""

            TextBox2.Text =""

            TextBox3.Text =""

$
            Counter = i + 1

            Counter = Label7.Text

            '在这里柜台

       结束如果

   结束子



解决方案


我坚持的部分是如何将我的3个值发送到ItemList类


你不能,因为ItemList类是一组Items,而不是字符串或数字的集合。 您只能在集合中存储某些类型的集合,并且该类型为Item。 所以说明应该是
读取:


"将这些值存储在Item类的新实例中。  将Item类的实例添加到&ItemList中的集合中。清除下一个项目的文本框。"


您定义了Item类,以便将此数据存储为单个对象,您可以将其存储在您创建的集合中。


对于ItemList集合,数组是一个非常糟糕的选择,尽管它是问题隐含的选择。 原因是你遇到的问题 - 你不知道你需要存储多少。  .Net支持没有这个问题的集合
,例如一个简单的List(Of T)很像数组,或者字典(Of T,Key),如果你想通过键访问项目等等。


在这种情况下它是一个数组,因此您将使用ReDim和Preserve选项。  您可以使用数组的当前大小来决定新大小(例如,1个以上)。

https://docs.microsoft.com/en-us/dotnet/ visual-basic / language-reference / statements / redim-statement


https://msdn.microsoft.com/en-us/library/system.array.length(v = vs.110)的.aspx < a href ="https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/r edim-statement">






Here's the instructions we were given for this activity:

  1. Create an Item Class. It should be able to store one string, one integer and one double.
  2. Create an ItemList Class. This class should contain an array of Items. You may hard code the number of items in the initial array or use ReDim to increase the size of the array before you insert new items into the array. This class should allow for expanding the size of the array if necessary and must include a function to generate a receipt as a string for all of the items in the Item array.
  3. Create an application interface that asks the user to enter 3 values: text string (Part Name), a quantity, and a sale's' price.
  4. Once the 3 values are all entered, the user can press a button. Store these values in the array of Item class objects in the ItemList. Clear the text boxes for the next item.
  5. When the user is done entering N items into the ItemList, they may push a second button in the UI to save a receipt. The string of text generated by the button push should call the function in the ItemList class that creates a receipt as a string and save that string to a file with the name Receipt_DATE.txt where DATE is todays date as YYYY-MM-DD.


The part im stuck on is how to send my 3 values to the ItemList class, as well as how to increase my shopping cart count by one.

Public Class Form1

    Private ShoppingCart As New ItemList
    Private Counter As Integer = 0
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""


    End Sub




    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim n As String
        Dim q As Integer
        Dim p As Double
        Dim i As Integer
        If Not TextBox1.Text = "" Then


            n = TextBox1.Text
            ShoppingCart.Items(Counter).PartName = n

            q = TextBox2.Text
            ShoppingCart.Items(Counter).Quantity = q

            p = TextBox3.Text
            ShoppingCart.Items(Counter).Price = p

            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""

            Counter = i + 1
            Counter = Label7.Text
            'counter here
        End If
    End Sub


解决方案

The part im stuck on is how to send my 3 values to the ItemList class

You can't, because the ItemList class is a collection of Items, not a collection of strings or numbers..  You can only store something in the collection that is the right type for the collection, and that type is Item.  So the instruction should read:

"Store these values in a new instance of the Item class.   Add that instance of the Item class to the collection in the ItemList. Clear the text boxes for the next item."

You defined the Item class for the purpose of storing this data as a single object, which is something you can store in the collection you created.

An array is a really bad choice for the ItemList collection, although it is the choice implied by the question.  The reason is the problem you have run into - you do not know ahead of time how many you will need to store. .Net supports collections that don't have this problem, such as a simple List(Of T) which is a lot like an array, or a Dictionary(Of T, Key) if you want to access items by a key, and many more.

In this case it is an array, so you will use ReDim with the Preserve option.  You can use the present size of the array to decide what the new size will be (eg, 1 more).
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/redim-statement

https://msdn.microsoft.com/en-us/library/system.array.length(v=vs.110).aspx



这篇关于帮助多个类之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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