如何为字符串赋值? [英] How do I assign a value to a string?

查看:148
本文介绍了如何为字符串赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我创建了一个应用程序(在Windows Form App.Net中),用户输入信息,然后在列表框中以第二种形式出现。但是,当应用程序运行并且用户转到第二个表单时,信息不会出现在任何位置。我收到错误,说我的lbResult和我的结果在分配值之前正在使用。我不确定如何为字符串赋值,而我的教科书对这个特殊问题的帮助很大。任何有关这方面的帮助将不胜感激。提前致谢!



  Dim  lbResults 正如 ListBox 
Dim 结果作为 字符串
Dim strName 作为 字符串 ' 将Hello和txtName声明为字符串以简化代码
strName = Hello& txtName.Text

lbResults.Items.Add(Results)
如果 radCompliment.Checked = True 然后 ' if / then根据用户选择的内容显示消息的分支语句。
结果= strName& 你很聪明

其他
MessageBox.Show( 请输入信息

结束 如果

如果 radMotivation.Checked = True 那么

结果= strName& 保持专注,永不放弃!
Else
MessageBox.Show( 请输入信息
结束 如果

如果 radGreeting.Checked = True 那么
结果= strName& 今天你好吗?

Else
MessageBox.Show( 请输入信息
结束 如果

如果 radJoke.Checked = True 那么
结果= strName& ; 计算机在午餐时间做了什么?有一个字节!
否则
MessageBox.Show( 请输入信息
结束 如果

如果 radFact.Checked = True 那么
结果= strName& 您是否知道Banannas是弯曲的,因为当它们长大时会被太阳吸引。
Else
MessageBox.Show( Please输入信息
结束 如果

结束 Sub





我尝试了什么:



我首先尝试没有 Dim Results as String 位,但发现我需要将所有用户输入放在一个位置,然后尝试将其放入列表框中,如果这有意义的话。我已经在网上和我的教科书/笔记中寻求帮助,但没有太多运气。我理解大多数编码与数字有关,但我认为用文本而不是数字制作我的应用程序会让人感到困惑。

解决方案

看看你的代码:

 Dim lbResults As ListBox 
Dim Results As String
Dim strName As String'将Hello和txtName声明为字符串以简化代码
strName = 你好& txtName.Text

lbResults.Items.Add(Results)

您声明变量 lbResults 结果,但在任何时候你都没有给它们一个值。所以,当你到达这一行时:

 lbResults.Items.Add(结果)

lbResults和结果都是 Nothing 并且您收到编译错误,表明您使用的是未初始化的值。这是一件好事 - 因为如果你没有那么当你运行代码时你会得到一个空引用错误因为 lbResults 中没有ListBox来获得项目属性!



可能,您不想声明 lbResults 在该方法中 - 它将掩盖表单上的那个,这样您的用户无论如何都不会看到任何结果。因此,删除声明行,看看该错误是否消失。如果它被未定义的变量错误替换,那么你需要把它放回去,创建一个实例,然后手动将它添加到你的表单中,以便用户看到任何东西。



您还可以在创建结果字符串时将每个结果值添加到ListBox中 - 或者也不会看到它。

喜欢这个:

如果radCompliment.Checked = True则
结果= strName& 你很成功
lbResults.Items.Add(Results)
Else
MessageBox.Show(请输入信息)
结束如果

(但是你也需要它在各种条件下。)


很难看出这个代码是否在方法中?无论哪种方式,你都谈论两种形式。您的表单无法看到彼此的变量。共享数据需要存在于数据存储中


首先:

我更喜欢您提供代码的实际使用方式。



第二名:

我想你正在使用几个复选框。在这种情况下,您应该使用每个Checkbox中的每个CheckedChange-Event调用您的方法(你有吗?)。



基本上给你特定的帮助你必须给更多信息(以及更好的代码)。


Hi there! I have created an application (in Windows Form App. Net) where the user inputs information and it comes out in a second form in a list box. However, when the app runs and the user goes to the second form, the information does not appear anywhere. I am getting errors saying that my "lbResult" and my "Result" are being used before being assigned a value. I am not sure how to assign a value to a string, and my textbook does help me much on this particular matter. Any help on this would be greatly appreciated. Thanks in advance!

Dim lbResults As ListBox
Dim Results As String
Dim strName As String 'declare Hello and txtName as string in order to simplify code
strName = "Hello " & txtName.Text

lbResults.Items.Add(Results)
If radCompliment.Checked = True Then 'if/then branch statement to make message appear depending on what user chooses. 
Results = strName & "You are FABULOUS"

Else
MessageBox.Show("Please input information")

End If

If radMotivation.Checked = True Then

Results = strName & "Stay focused and never give up!"
Else
MessageBox.Show("Please put in information")
End If

If radGreeting.Checked = True Then
Results = strName & "How are you today?  "

Else
MessageBox.Show("Please put in information")
End If

If radJoke.Checked = True Then
Results = strName & "What did the computer do at lunch time? Had a byte!"
Else
MessageBox.Show("Please put in information")
End If

If radFact.Checked = True Then
Results = strName & "Did you know that Banannas are curved because when they grow they gravitate towards the sun."
Else
MessageBox.Show("Please put in information")
End If

End Sub



What I have tried:

I tried at first without the Dim Results as String bit, but found I needed to put all of my User input in one place before trying to put it in the list box, if that makes sense. I have looked online and in my textbook/notes for help on this but have not had much luck. I understand most coding when it is with numbers, but I think making my application with text instead of numbers has made it confusing.

解决方案

Look at your code:

Dim lbResults As ListBox
Dim Results As String
Dim strName As String 'declare Hello and txtName as string in order to simplify code
strName = "Hello " & txtName.Text

lbResults.Items.Add(Results)

You declare variables lbResults and Results, but at no point do you give them a value. So when you get to this line:

lbResults.Items.Add(Results)

both lbResults and Results are Nothing and you get a compilation error that you are using an uninitialised value. That's a good thing - because if you didn't then when you ran the code you would get a "null reference error" because there is no ListBox in lbResults to have an Items property!

Probably, you don't want to declare lbResults in that method at all - it will mask the one that is on your form so your user will not see any results anyway. So remove the declaration line and see if that error goes away. If it is replaced by "undefined variable" errors, then you need to put it back, create an instance, and manually add it to your form in order for the user to see anything.

You can also need to add each Results value to your ListBox when you create the Results string - or it won't be seen either.
Like this:

If radCompliment.Checked = True Then 
   Results = strName & "You are FABULOUS"
   lbResults.Items.Add(Results)
Else
   MessageBox.Show("Please input information")
End If

(but you'll need it in every condition as well).


Hard to see if this code is inside a method? Either way you talk about two forms. Your forms can't see each other's variables. Shared data needs to live in a data store


At first :
I prefer that you provide the code how it is really used by you.

2nd:
I suppose that you are using severall Checkboxes. In this case you should call your method (do you have one ?) with every CheckedChange-Event from each Checkbox.

Basicly to give you specific help you have to give much more information (and a better code).


这篇关于如何为字符串赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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