[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Retruning 2 dimensional array in VB.NET

Posted on 2006-11-28
11
Medium Priority
?
283 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
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!

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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