如何引用数组中的对象 [英] how to refence objects in an array

查看:120
本文介绍了如何引用数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在我的asp.net应用程序中,我将用户上传的excel文件的内容放置在gridview中.
在此文件中,有一个用户填写的表格样式表格.所以我必须验证
如果用户上传的文件与我提供给他们的模式相匹配.

为此,我检查表单的所有列是否都存在.
因此,我大约有40个变量代表每个列的索引.
我有一个字符串tyhat数组,其中包含文件需要的每一列的名称.

我想做的是使用数组将值放入每个变量中,而不是使用巨大的Select Case

The thing is in my asp.net app I place the content of a excel file uploaded by the users in a gridview.
in this file there is a table style form that the user fills out. So I have to verify
if the file uploaded by the user matches the pattern I provided them.

To do so I check if all the columns of the form are there.
So I have around 40 variables that represent the index of each column.
and I have a array of string tyhat contains the name of every columns the file needs.

What I want to do is use a array to put the value in each variable instead of using a huge Select Case

'HeaderPattern contains every needed column name ->  Dim HeaderGabarit() As String = {all the columns name}
'DataGridView1 contains every cells of the excel file
'IndexArray Should contains all the variables that represent the index of each columns (thats where I need help
'since I regular integer array can't contains variables but only their values)-> Dim IndexArray() As Integer = {all the variables}
'indexFound contains the actual index of every columns
For i = 0 To DataGridView1.HeaderRow.Cells.Count - 1
Dim indexFound As Integer = Array.IndexOf(HeaderPattern, DataGridView1.HeaderRow.Cells(i).Text.ToLower())
       If indexFound <> -1 Then
             IndexArray(indexFound) = i
             End If
next 


因此,我尝试查找gridview中存在的每个列名(与Excel文件完全相同的内容)
在包含每个必需列名称的数组中.当两个数组中都存在一列时,我在gridview中获取索引并将其放入匹配变量(在IndexArray中),但是变量的实际值(在类的开头声明)不变.

例如,在c ++中,这将是一个快速修复,因为我要做的就是使用整数指针而不是常规变量来包含我的列索引,但是vb中不存在指针.

如何更改vb.net中数组或列表中引用的变量的值?

希望它的明确表示感谢!


So I try to find every column name that exist in my gridview (The exact same content as the Excel file)
in A array that contains every required column name. When a column exist in both arrays, I take the index in the gridview and put it in the matching variable (in IndexArray ) but the actual value of the variable (declare at the begining of the class) does not change.

In c++ for example it would be a quick fix since all I would have to do is use pointers of integer instead of regular variables to contain my columns indexes but pointers does not exist in vb.

How can I change the value of a variable referenced in a array or list in vb.net?

hope its clearer thanks!

推荐答案

我认为您无法获得如何做到"的答案,因为您没有具有实际目标的真实问题.您的请求是基于总体上对编程的一些误解.您需要先学习相关概念,然后才需要了解您真正需要的方法.现在,您还没有.不用担心,您可以学习它.

首先,没有变量数组"这样的概念.数组是总是对象数组.这不仅适用于.NET,而且适用于C,C ++以及可能适用于所有(或几乎所有)平台和语言.在这里,我以最一般的含义使用工作对象",包括原始对象和任何值对象(例如structenum),而不仅是引用对象.下一步,您需要了解引用对象和值对象以及变量"和成员"(某种类型)的概念之间的区别.不,您还不了解它们,需要学习.引用类型的对象可以具有多个引用它的变量.您可以将其视为对象的几种不同名称.

一个对象根本不能有名字;并适用于数组就是这种情况.对象intArray[3]是一个永远不会有任何名称的整数对象,因为具有相同值的整数变量只是不同对象的名称,但是如果您具有stringBuilderArray[3]的引用元素类型(例如System.Text.StringBuilder),则该元素可以由某些变量引用,因此object.ReferenceEquals(stringBuilderArray[3], someStringBuilder)可以为true.了解等效性和参照等效性.您需要了解引用,可以将其视为托管指针.从物理上讲,它们并不是真正的指针,因为它们可以在运行时进行物理重定位.

将这些想法视为只是您需要了解的提示.该论坛不是举办有关计算机科学的广泛讲座的合适场所,但是您需要自己学习和理解一些内容.

—SA
I don''t think you can get an answer to your question "how to do that" because you don''t have a real question with a real goal. Your request is based on some misunderstanding of programming in general. You need to learn related concepts first, and then only approach to understanding what you really need. Right now, you don''t. Not to worry, you can learn it.

First of all, there is no such concept as "array of variables". An array is always an array of objects. And this is true not only for .NET, but also for C, C++ and probably for all (or almost all) platforms and languages. Here, I use the work "object" in most general meaning, including primitive object and any value objects (like struct or enum), not only reference objects. For a next step, you need to understand the difference between reference and value object and the concepts of "variable" and "member" (of some type). No, you don''t understand them, just yet, need to learn it. An object of a reference type can have more then one variables referencing it. You can think of it as of several different names for the object.

An object can have no name at all; and this is applied An array is such a case. The object intArray[3] is an integer object which never has any name, because a integer variable with the same value is simply a name for different object, but if you have stringBuilderArray[3] of the reference element type like System.Text.StringBuilder, an element can be referenced by some variable, in this sense, object.ReferenceEquals(stringBuilderArray[3], someStringBuilder) can be true. Learn about equivalence and referential equivalence. You need to get understanding of references, which can be considered as managed pointers. Physically, they are not really pointers, because they can be physically relocate during run time.

Consider these idea as just hints on what you need to understand. This forum is not an adequate place for spacious lectures on computer science, but there are things you need to learn and understand by yourself.

—SA


您必须阅读以下内容:
http://www.go4expert.com/forums/showthread.php?t=4187 [ ^ ]

http://www.dotnetperls.com/array-vbnet [ 数组类 [ ^ ]

希望您觉得它有用.
You must read about this:
http://www.go4expert.com/forums/showthread.php?t=4187[^]

http://www.dotnetperls.com/array-vbnet[^]

Keyword: ReDim.

Or just use the Array class[^]

Hope you find it useful.


这篇关于如何引用数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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