?
Solved

Retruning 2 dimensional array in VB.NET

Posted on 2006-11-28
11
Medium Priority
?
282 Views
Last Modified: 2008-01-09
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
Comment
Question by:yaney00074
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18031787
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18031788
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18031803
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
Independent Software Vendors: 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!

 

Author Comment

by:yaney00074
ID: 18031898
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
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 2000 total points
ID: 18032021
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
 

Author Comment

by:yaney00074
ID: 18032905
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18032918
1. What do you mean by "evaluate a condition"?
2. What version of .NET are you using?
0
 

Author Comment

by:yaney00074
ID: 18036063
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18036145
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
 

Author Comment

by:yaney00074
ID: 18047780
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18050558
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question