在子VB NET中为字符串赋值 [英] Assign value to string within sub VB NET

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

问题描述

我无法获得正在使用数据库中的值更新字符串的sub。我已经将sub设计为动态到对象类型,它可以与所有其他对象一起使用。



是否无法更新这样的字符串?



我尝试过:



我创建了以下子程序,作为基于sql数据库的应用程序中各种对象的填充程序。



I am not able to get a sub that I am working on to update a string with a value from a database. I have designed the sub to be dynamic to object type and it works with all other objects.

Is it not possible to update strings like this?

What I have tried:

I have created the following sub to act as the filler for various objects in my application based on an sql database.

Public Sub SQL_Object_Fill(sp As String, Params As Array, Values As Array, Objs As Array, Cols As Array, Optional type As String = Nothing)
        Dim cmd As New SqlCommand()
        Dim dr As SqlDataReader

        cmd.Connection = cn
        cmd.CommandText = sp
        cmd.CommandType = CommandType.StoredProcedure

        For Each Param As String In Params
            cmd.Parameters.Add(New SqlParameter(Param, Values(Array.IndexOf(Params, Param))))
        Next

        cn.Open()

        dr = cmd.ExecuteReader()

        If dr.HasRows Then

            While (dr.Read())

                For Each obj As Object In Objs
                    If obj.GetType() Is GetType(System.String) Then
                        obj = (dr(Cols(Array.IndexOf(Objs, obj)))).ToString()
                        MsgBox(obj)
                    ElseIf obj.GetType() Is GetType(System.Windows.Forms.ListBox) Then
                        obj.Items.Add(dr(Cols(Array.IndexOf(Objs, obj)).ToString()))
                    ElseIf obj.GetType() Is GetType(MetroFramework.Controls.MetroTextBox) Or obj.GetType() Is GetType(MetroFramework.Controls.MetroTile) Then
                        obj.Text = (dr(Cols(Array.IndexOf(Objs, obj)).ToString()))

                    End If
                Next

            End While

        End If

        cn.Close()

    End Sub





到目前为止这一直有效但我现在处于需要调用函数来设置字符串值的阶段。代码如下:





This has been working so far but I am now at a stage where I need to call the function to set the value of a string. Code below:

Dim Email As String = ""
        SQL_Object_Fill(sp:="[Misc].[Email_Fields]", Params:={"@id", "@Type"}, Values:={Main.id, Type}, Objs:={Email}, Cols:={"Email"})
        MsgBox(Email)



我已经在函数中放了一个msgbox,之后我调用它。它在函数内的消息框中提供了预期的值,但是在调用函数之后没有出现在它中的那个?



我做错了什么?所有其他对象类型都完美地工作。





由于某种原因,这不是将我的对象分配给存储过程中列的值。我在


I have put a msgbox both in the function and after when I am calling it. It comes up with the expected value from the message box within the function but not in the one after the function is called?

What am I doing wrong? All of the other object types work perfectly.


For some reason this is not assigning my object to the value of the column in the stored procedure. I have put a msg box in the

推荐答案

中放了一个msg框。我只知道设置调用过程的值的两种方法。

1设置等于函数



公共函数SQL_Object_Fill(....)作为对象{

返回值}



电子邮件= SQL_Object_Fill(....){

返回值}





2.使用参数的ByRef设置指向变量的指针。

Dim Email as string =



Public Sub SQL_Object_Fill(....,ByRef yourvalue as object)
There are only 2 ways I know of to set a value calling a procedure.
1. setting it equal to a function

Public Function SQL_Object_Fill(....) as object {
return value}

Email = SQL_Object_Fill(....){
return value}

Or
2. using the ByRef on the parameter to set a pointer to your variable.
Dim Email as string = ""

Public Sub SQL_Object_Fill(....,ByRef yourvalue as object)


感谢您的解决方案。我今天做了更多的研究,并将其缩小到对象是一个数组的事实。



基本上在我的代码中,如果objs为数组,那么它不起作用,但如果obj为字符串,那么它确实如此。



接下来的问题是如何对数组中的每个值进行byref?
Thank you for your solution. I've done some more research today and narrowed it down to the fact that the object is an array.

Basically in my code if objs as array then it doesn't work but if obj as string then it does.

The question then becomes how do you do byref for each value in an array?


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

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