Solved

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

Posted on 2006-11-27
8
469 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
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!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 54
Copying from a network share 3 38
Read top line from CSV file 14 60
Javascript function 3 23
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…

752 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