Solved

VB.net - Passing variants by reference when Byref doesn't work in this case - Urgent.

Posted on 2006-11-27
8
473 Views
Last Modified: 2012-05-05
I want to access variants "by reference", because I want to be able to access and modify those variants.  But I haven't found a way to do it yet in the case I am working on.  

I am working with a relatively complex structure of datas. I want to access a particular variant, that is into a sub-structure somewhere, and once I have the right variant, I want to be able to modify it, without having to re-access it every time, since the algorythm to find the variant I want is relatively complex.

My problem : Sorting
-I would like to be able to do something like that :
-In VB.Net 2003

dim Data1 as V_Data1

Public Structure V_Data1
   
    Public Datas2() as V_Data2
    Public NbDatas2 as long
   
    Public function Data2(Index,Sort as boolean) as object
          Data2=Datas2(Index)
          'In fact, this funciton is a lot more complex, because it gets the object needed after sorting all the Datas2()  
               'array in different ways
    End Function

End Structure

Public Structure V_Data2
     Public Name as string
End Structure
   
Public Sub ChangeName
     Data1.Data2(3,SortAscending).Name="YesSir"
End Sub

Thanks,

Gabriel

0
Comment
Question by:gahilo
[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
  • 5
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18020039
You should probably be using a Class instead of a Structure...
0
 

Author Comment

by:gahilo
ID: 18030853
I have never really worked with Classes before.  I started to change my code to use Classes instead of Structures.  It begin to work, but I am actually stopped by the following problem, that comes back to my original problem : I can’t find a way to work on my original object through another variant that contains my original object.

Here is my problem :

Public Class Class1
      Public Text1 as String
End Class

Public Sub Sub1
      Dim Inst1 as New Class1
      Inst1.Text1=”MyText”
      
      Sub2(Inst1)
End Sub


=>And then, this sub doesn’t work :

Public Sub Sub2(Obj as Object) ‘This is because I want that sub to process many  
                                                 ‘different objects(Instances of different classes)
      Select Case Obj.GetType.Name
            Case “Class1”
                  Msgbox(Obj.Text1) ‘=>I get up to here, but this line doesn’t work
      End Select
End Sub

=>While executing my code, I can see that the variant “Obj” really contains the “Inst1” object, but I don’t find the way to access it

=>If I do the following, it works, but I don’t want to do it

Public Sub Sub2(Obj as Object)
      Dim TempInst as New Class1
      Select Case Obj.GetType.Name
            Case “Class1”
                  TempInst=Obj
                  Msgbox(TempÎnst.Text1)
      End Select
End Sub

Why??????????

Thanks,

Gabriel
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18030895
Do you have Option Strict On?

If you turn it OFF, then it will work...of course when you have a variable declared as "Object", then the Intellisense in the IDE won't be able to tell anything about that object.  It should work at run-time though.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:gahilo
ID: 18030932
Option Strict is already Off
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18031009
It's working for me with VB.Net 2005 Express:

Public Class Form1

    Public Class Class1
        Public Text1 As String
    End Class

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Inst1 As New Class1
        Inst1.Text1 = "MyText"

        Sub2(Inst1)
    End Sub

    Public Sub Sub2(ByVal Obj As Object)
        Select Case Obj.GetType.Name
            Case "Class1"
                MsgBox(Obj.Text1)
        End Select
    End Sub

End Class


I get a MsgBox with "MyText" in it...
0
 

Author Comment

by:gahilo
ID: 18031053
I don't know if it can help...  When I look at the Locals window, here is what I have for the "Obj" and "TempInst" trees.

Name                        Value

-Obj                        {Class1}
  --[Class1]                  {Class1}      =>Why is there this line in the tree??
      --Text1                  MyText

-TempInst                        {Class1}
  --Text1                        MyText
0
 

Author Comment

by:gahilo
ID: 18031178
Forget that problem, I am sometimes stupid...
0
 

Author Comment

by:gahilo
ID: 18031438
Thanks, everything is working now.

Another fast question :

All my classes make a "structure" of data.  I want to show all this information into treeviews.  I already have a code that does that, but I want it to be integrated into my classes that contains all my information, and I want the treeviews to self-modify dynamically, when my objects get modified, without always collapsing.  

In other words, I would like to have something like a property in my classes of data that link the data to it's corresponding treenode, and in the other way, link the treenode to it's corresponding instance of class.  I have tried a lot of things, but haven't found a solution for that up to now.

Do you have a cue for me???

Thanks
0

Featured Post

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!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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