Results 1 to 14 of 14

Thread: Sending a variable Byref to a class

  1. #1

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Sending a variable Byref to a class

    I want to be able to send a Richtextbox to one of my classes byref so that updates to the class rtb will update the actual rtb

    Right now I have

    VB Code:
    1. Public Class InfLog
    2.  
    3.     Private Buffer1, Buffer2 As String
    4.     Private Interval As Integer = 500
    5.     Private rtxtbox As RichTextBox
    6.  
    7.     Public Sub New(ByVal Buffer1 As String, ByVal Buffer2 As String, ByRef rtb As RichTextBox)
    8.         Me.Buffer1 = Buffer1
    9.         Me.Buffer2 = Buffer2
    10.         Me.rtxtbox = rtb
    11.     End Sub

    However when I make changes to rtxtBox nothing happens. Can someone please point me in the right direction?
    If wishes were fishes we'd all cast nets.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Graff, I'm quite a newbie, so it's possible I fail completely, but I think you have to act on rtb directly.

    Your ' Me.rtxtbox ' is private and I think it's not possible to refer it out of your class, so probably you need to complete your loop in this way, for example

    rtb.text=Me.rtxtbox.text

    At this time, I think, you will see the changes.

    Problem is: you have to pass your variable from the constructor procedure, to that one will make the changes.

    I have no good practice, anyway. Normally I pass control byref, but directly to the class public method I call and not in the constructor, where, in general, I only pass initializing parameter.

    Below is an example of a class I use to redim form on my two screens.

    VB Code:
    1. Sub New()
    2.  
    3.         End Sub
    4.  
    5.         Public Function FnzRapportoTraSchermoEForm(ByRef F As Form) As Single
    6.  
    7.             Dim orizz As Integer
    8.             Dim vert As Integer
    9.  
    10.             Me.SubRilevaRisoluzione(F, orizz, vert)
    11.             Dim R As Single = CSng(orizz / F.Width)
    12.  
    13.             Return R
    14.  
    15.         End Function
    16.  
    17.         Private Sub SubRilevaRisoluzione(ByRef IlForm As Form, ByRef Orizzontale As Integer, ByRef Verticale As Integer)
    18.  
    19.             Dim desktopSize As Size
    20.             desktopSize = System.Windows.Forms.SystemInformation.PrimaryMonitorSize
    21.             'Dim AreaSchermo As System.Drawing.Rectangle = Screen.GetWorkingArea(IlForm)
    22.             Dim Punto As System.Drawing.Point
    23.             Punto.X = IlForm.Left + (IlForm.Width / 2)
    24.             Punto.Y = IlForm.Top + (IlForm.Height / 2)
    25.             Dim AreaSchermo As System.Drawing.Rectangle = Screen.GetWorkingArea(Punto)
    26.             Verticale = AreaSchermo.Height
    27.             Orizzontale = AreaSchermo.Width
    28.             'MessageBox.Show(Pluto.ToString & "   " & paperino.ToString)
    29.  
    30.         End Sub
    31.  
    32.  
    33.         Public Sub SubVariaSecondoRapporto(ByRef F As Form, ByVal Rapporto As Single)
    34.  
    35.             Dim R As Single = CSng(Rapporto)
    36.             f.Scale(R)
    37.             Me.CambiaIFont(f, R)
    38.             'F.Refresh()
    39.  
    40.         End Sub
    41.  
    42.  
    43.  
    44.         Private Sub CambiaIFont(ByRef F As Form, ByVal R As Single)
    45.  
    46.             Dim Ct As Control
    47.             Dim T As String
    48.  
    49.             For Each Ct In F.Controls
    50.                 If Not Ct.Controls Is Nothing Then
    51.                     'Se qui, contiene controlli di secondo livello (figli)
    52.                     Dim CtF As Control     'Devo ciclare sui suoi controlli figli
    53.                     For Each CtF In Ct.Controls
    54.                         Me.RidimensionaFontDelControllo(CtF, R)
    55.                     Next
    56.                 End If
    57.  
    58.                 ' Qui si maneggiano i controlli di primo livello
    59.                 Me.RidimensionaFontDelControllo(Ct, R)
    60.  
    61.             Next
    62.  
    63.         End Sub
    64.  
    65.  
    66.         Private Sub RidimensionaFontDelControllo(ByRef Ct As Control, ByVal R As Single)
    67.  
    68.             Dim FSize As Single = Ct.Font.Size
    69.             Dim FStile As FontStyle = Ct.Font.Style
    70.             Dim FNome As String = Ct.Font.Name
    71.             Dim NuovoSize As Single = FSize * R
    72.             Dim NFont As New Font(FNome, NuovoSize, FStile)
    73.             Ct.Font = NFont
    74.  
    75.         End Sub
    76.  
    77.  
    78.     End Class

    Good job!
    Live long and prosper (Mr. Spock)

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Even ByVal should result the same (in case you've done it right).

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Pirate is right, because we are speaking about a RichTextBox that is an object 'reference type' and not 'value type'.

    We have to remember, that If we use a variable like a Double, or a String, we have to specify which kind of 'link' we need because it makes a difference.

    Good morning to both, dear friends

    Live long and prosper (Mr. Spock)

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    RichTextBox is a Class , dude.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    What is the point?
    You are underlying that Rtb is a class....perhaps because I said it is an object? If so, that's because when you work with something, it is an object (a class instanced....or instanciated...damned my english....help me! ). A form is a class, but when you try to refer it without an instance (an object) you have an error. You are perfectly right saying richtextbox is a class, but 'YourRtb', placed on your form, is an object derived from its class. It's possible I'm wrong, obviously, so I don't want to say 'that's true!'. But only 'I THINK IT'S TRUE'. If not, I will note my mistake and will become a little more experienced learning by that!
    Anyway we are discussing about details that are not really the 'core' of the thread, also if interesting'.
    Live long and prosper (Mr. Spock)

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Or...perhaps you wanted to say that class is reference type? In this case I agree 100%: Classes are reference types!
    Excuse me...I'm not very sure of my english and sometimes I miss also what should be obvious!
    Live long and prosper (Mr. Spock)

  8. #8
    Member
    Join Date
    Sep 2002
    Location
    Cincinnati, OH
    Posts
    44
    you've gone mad.
    Jim Webster

  9. #9
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. Public Class InfLog
    2.  
    3.     Private Buffer1, Buffer2 As String
    4.     Private Interval As Integer = 500
    5.     Private rtxtbox As RichTextBox
    6.  
    7.     Public Sub New(ByVal Buffer1 As String, ByVal Buffer2 As String, ByRef rtb As RichTextBox)
    8.         Me.Buffer1 = Buffer1
    9.         Me.Buffer2 = Buffer2
    10.         'you problem lies here:
    11.         'rtb is a reference to the rtb....
    12.         'when you say 'Me.rtxtbox = rtb' vb thinks you want to
    13.         'create a copy of the object rtb is pointing to and store it in
    14.         'Me.rtxtbox
    15.         Me.rtxtbox = rtb
    16.     End Sub


    at this very moment, i cant think of away to copy a reference (pointer). Prolly because of lack of sleep....
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by alextyx
    What is the point?
    You are underlying that Rtb is a class....perhaps because I said it is an object?
    I didn't mean to make any confusion but 'Object' is general word for class instances(RTB instance ...) and base datatypes(Int,String,Double..etc) which could be confusing also when talking about value types and reference types .Umm , there's much has to be said though , but I'm just a little lazy ...

  11. #11
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Yes Pirate. Be patience, as I said sometimes I'm lost trying to figure if what I wrote means really what I wanted to and, if not, what could have you understood.....very stressing!

    Just now I'm having a look of a list of reference and value types, to refresh my memory about. I confirm...Classes are reference type!

    Jewbster....I agree with you !

    Live long and prosper (Mr. Spock)

  12. #12

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    Ok so the only solution we really know of is to take it out of the initialization and put it in my methods that will use it when called in the main program?
    If wishes were fishes we'd all cast nets.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I think we've only been looking at the apples and calling the whole fuit salad bad...

    How is this class being dimmed, instanciated, and used?

    Personaly, I cannot see anything wrong with the constructor. I'm wondering if maybe it's wrong at the other end of things.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    In the global area of the form main:
    Dim iLog as Inflog

    In Form_Load event:
    iLog = New InfLog("B1.UBL","B2.UBL", rtbChat)

    Then in the btnEnable_Click event:

    tMain.enable = true 'this is a timer

    tMain_Tick event:

    iLog.refresh()

    at which point the refresh method will parse a log, color code it determined by certain delimiters in the log and put it on the rtbChat control

    I was hoping to avoid passing the rtbChat down 3 methods withing the InfLog class by just refrencing it in as a part of the class so instead of declaring it at the constructor I have to go

    iLog.refresh(rtbChat)

    refresh called the process method

    Process(rtbChat)

    then process calls the parseline method

    ParseLine(rtbChat)

    where finally alll the magic is done.
    If wishes were fishes we'd all cast nets.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width