将参数传递为字符串

将参数传递为字符串

本文介绍了将参数传递为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedValue.ToString() <> " " Then
            Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())
            fillstate(Country_id)
            ComboBox3.SelectedIndex = 0

        End If



    End Sub
    Private Sub fillstate(ByVal Country_id As Integer)
        Dim adpt As New SqlDataAdapter
        Dim ds As New DataSet
        Dim con As New SqlConnection
        con.ConnectionString = str
        cmd = New SqlCommand("SELECT S_id,S_name from state where C_id=@C_id", con)
        cmd.Parameters.AddWithValue("@C_id", Country_id)
        adpt.SelectCommand = cmd
        adpt.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            ComboBox2.ValueMember = "S_id"
            ComboBox2.DisplayMember = "S_name"
            ComboBox2.DataSource = ds.Tables(0)

        End If





以上代码使用参数作为整数ie,country_id作为整数





如果我使用country_id作为字符串我应该更正使其作为参数传递



the above code is used arguments as integer ie,country_id as integer


if i use country_id as string where should i correct to make it pass as arguments

推荐答案

Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())
fillstate(Country_id)

有点......嗯......很奇怪。

Is a little...um...odd.

ComboBox1.SelectedValue

可能已经是一个整数,特别是如果你直接从数据库中读取它。所以...

May be an integer already, particularly if you have read it directly form the DB. So...

ComboBox1.SelectedValue.ToString()

使它成为一个字符串。所以...

Makes it a string. So...

Convert.ToString(ComboBox1.SelectedValue.ToString())

将字符串转换为...一个字符串.. 。

Converts the string to...a string...

Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())

然后使用隐式转换将其更改为整数以传递给您的方法,这需要一个整数...

那么,为什么不首先将Value作为整数传递,或者在其上调用CInt以确保它是一个整数?



播放带有可变类型的音乐椅只是让你的代码更混乱,更不可靠的好方法......

Then uses an implicit conversion to change it to an integer to pass to your method, which takes an integer...
So, why not just pass the Value as an integer in the first place, or call CInt on it to ensure it is an integer?

Playing "musical chairs" with variable types is just a good way to make your code more confusing and less reliable...


这篇关于将参数传递为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:06