将动态值分配给对象

时间:2016-11-28 19:51:10

标签: vb.net

我想创建对象并分配我想要的任何值 示例

Dim obj As New Object
obj.test = "test"
obj.test.test2 = "test2"

或者

Dim obj As New Object
obj("test") = "test"
obj("test")("test2") = "test2"

可以这样做吗?

1 个答案:

答案 0 :(得分:0)

您可以使用ExpandoObject

    <?php
$servername = "localhost";
$username = "root";
$password = "root123";
$dbname = "MyDB";

$conn = new mysqli($servername, $username, $password);


if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully"."</br>";

$sql = "SELECT event_name FROM wp_em_events";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Name :".$row["event_name"];
    }
} else {
    echo "0 results";
}

$conn->close();
?>

无法将相同的属性映射到字符串和ExpandObject(第一个示例的第三行所需)。但您可以添加另一个属性。如果要嵌套它,则需要继续声明ExpandoObjects,例如

Dim obj as new ExpandoObject
obj.test = "test"