Results 1 to 9 of 9

Thread: Object Reference and Casting

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    Mérida, Venezuela
    Posts
    3

    Object Reference and Casting

    Hi everyone I'm starting on VB.net and have encounter some problems:

    1._ I have two Forms (Form1 and Form2), on Form1 I have a public variable; Form1 calls Form2. My problem is that I need to change that public variable from Form2. I have (in Form2) a variable declared as Form1, but I don't know how to make that variable point to the existing instance of Form1.

    2._ Is there a legal way to cast from an object to a button. 'Cause I have something like this:

    Dim btn as button;
    btn=sender '(which is an object);

    When I use strict off I can do it fine but I get an error if I turn strict on.

    Thanks for the help.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    VB Code:
    1. Dim btn as button = DirectCast(Sender , Button)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    For the variable part why don't you put it in a module ?

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    Mérida, Venezuela
    Posts
    3
    I guess I'm going to declare it in a module, but just for knowing do you know how to do it, 'cause there's got to be a way...

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    By passing it in the constructor I think .

  6. #6
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    1. Do not use a module. That sure sounds like the old (wrong) way of just making everything global. That is bad programming. Here is how you do it the right way, via a reference to Form1.

    Declare a variable, call it FormOne in form2 at the class level.

    Private FormOne as Form1

    In form2s constructor (the New method, expand the generated code section to see it), you need to add something like this

    public sub New(frm1 as form1)
    ' put this at bottom of method
    FormOne = frm1

    In Form1 you create a Form2 like this:

    Dim frm2 as new Form2(Me)
    Frm2.show

    Now you have a reference to your Form1 object that instantiated Form2, so you can reference controls like this:

    FormOne.someControl.someProperty = someValue
    or
    FormOne.someVariable = someValue

    2. If you are talking about the sender object in events, just say something like this, assuming the sender is a button of course

    dim btn as button = sender

    now you can call all methods and use properties of a button via your btn object. I believe VB will cast it for you using that syntax, but the other way works just as well
    Mike Stammer

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    1) Make a public property in Form1 that changes the variable.

    Then in form2, you can simply change that property by coding:
    VB Code:
    1. Form1.someProperty = someValue

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MikeStammer
    1. Do not use a module. That sure sounds like the old (wrong) way of just making everything global. That is bad programming.
    heheh . .

  9. #9
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    Originally posted by nemaroller
    1) Make a public property in Form1 that changes the variable.

    Then in form2, you can simply change that property by coding:
    VB Code:
    1. Form1.someProperty = someValue
    This will not work, ever, without a reference to form1. .NET is not VB6. In VB6 this is how it was done, but not any more.
    Mike Stammer

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