Solved

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

Posted on 2006-11-27
8
436 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
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 85

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
 

Author Comment

by:gahilo
ID: 18030932
Option Strict is already Off
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 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

20 Experts available now in Live!

Get 1:1 Help Now