[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Retruning 2 dimensional array in VB.NET

Experts,

I have the following function and I am returning 2 fields a groupId and roleId to the dataset.
However, I need to know how to transform this fucntion so that it returns a 2 dimensional array instead one.

I am getting the following error:
UserDC.vb(182): 'ReDim' cannot change the number of dimensions of an array.
'---------------------------------------------------------------------------------
Here's the function below:

    Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer(,)
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups(,) As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop) = Convert.ToInt32(objRow.Item("groupID"))
            intLoop += 1
        Next

        Return aintGroups
    End Function

Thanks
0
yaney00074
Asked:
yaney00074
  • 6
  • 4
1 Solution
 
HillwaaaCommented:
Hi yaney00074,


I think you want something along these lines:

    Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer()()
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups()() As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)(????)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop)(?????) = Convert.ToInt32(objRow.Item("groupID"))
            intLoop += 1
        Next

        Return aintGroups
    End Function

Cheers,
Hillwaaa
0
 
Jeff CertainCommented:
The problem is here:
ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)

You really want:
ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)

0
 
Jeff CertainCommented:
Full rewrite:

  Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer(,)
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups(,) As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop, 0) = Convert.ToInt32(objRow.Item("groupID"))
            aintGroups(intLoop, 1) = Convert.ToInt32(objRow.Item("roleId"))
            intLoop += 1
        Next

        Return aintGroups
    End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yaney00074Author Commented:
Chaosian,

Thanks for your quick feedback.

If you don't mind, can you explain to me, in a step-by-step fashion what's happening?

Thanks
0
 
Jeff CertainCommented:
Sure... you want a 2D array. However, what you really want is an array that has N pairs of integers, where N is the number of rows in your dataset.

ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)  ' changes the array to be N x 2, since the array index is zero-based
(as an aside, if I knew you were using VS2005, I'd recommend a generics-based alternative)

Then you loop through your rows. Each time through, the (N,0) element gets the groupId assigned to it and the (N,1) element gets the role Id. So, for any row with index n, the (n,0) element of aintGroups has the appropriate groupId and the (n,1) element has the row Id.

The rest is your code :)
0
 
yaney00074Author Commented:
Ooh I forgot to ask (hopefully it's not too late)

The calling variable is a 2-dimensional property array.

Now we're having the array assigned to that property.

How concretely would I use it to evaluate a condition.
For example with the first item being the GroupID and the second the roleID within that group
Here's what I get for example:
(0,0) = 175
(0,1) = 1
(1,0) = 23
(1,1) = 3
(2,0) = 46
(2,1) = 3
etc...

0
 
Jeff CertainCommented:
1. What do you mean by "evaluate a condition"?
2. What version of .NET are you using?
0
 
yaney00074Author Commented:
What do you mean by "evaluate a condition"?

What I mean is looping through that 2-D array to check if a particular
Basically, this array is a property of the user object (objUser)  like this:
objUser.groupID

The functionality returns a user object with those properties encapsulated in it.

But the challenges I facing is to know HOW TO EVALUATE THE CONTENT OF that (now) 2D array
by  stripping out the value of the Group it belongs (which is the first element) and the Role (which is the second element)

Please, let me know if I am still not clear

What version of .NET are you using?
.NET 1.1
0
 
Jeff CertainCommented:
The way things sit now, your user can belong to multiple groups, and the return value contains all groups and roles that the user belongs to.

You'll need to do something like this in your user object (assuming that your user object has a property called Groups that contains that array:

Public Function BelongsToGroup(ByVal group As Integer) As Boolean
    BelongsToGroup = False

    Dim i As Integer
    For i = 0 To UBound(Groups, 1) - 1
      If Groups(i, 0) = group Then
        Return True
      End If
    Next
  End Function
0
 
yaney00074Author Commented:
Chaosian,

I am having serious problem getting your functionality to work:

Public Function BelongsToGroup(ByVal group As Integer) As Boolean
    BelongsToGroup = False

    Dim i As Integer
    For i = 0 To UBound(Groups, 1) - 1
      If Groups(i, 0) = group Then
        Return True
      End If
    Next
  End Function

---

How can I adjust this to work and that would to the job:

           For i = 0 To UBound(cUser.GroupID, 1) - 1
                For j = 0 To UBound(cUser.GroupID, 1) - 1
                    Me.lblGroup.Text = "(" & cUser.GroupID(i, 0).ToString & ", " & cUser.GroupID(0, j) & ")" & "<br/>"
                Next
            Next

Question:
How do i loop to display the content of the array?
0
 
Jeff CertainCommented:
For i = 0 To UBound(cUser.GroupID, 1) - 1
                    Me.lblGroup.Text = "(" & cUser.GroupID(i, 0).ToString & ", " & cUser.GroupID(i, 1) & ")" & "<br/>"
            Next
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now