Solved

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

Posted on 2006-11-27
8
428 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
  • 5
  • 3
8 Comments
 
LVL 85

Accepted Solution

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

Author Comment

by:gahilo
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 

Author Comment

by:gahilo
Comment Utility
Option Strict is already Off
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
Forget that problem, I am sometimes stupid...
0
 

Author Comment

by:gahilo
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now