Results 1 to 11 of 11

Thread: [RESOLVED] C# equivalent of ByVal from VB.NET ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] C# equivalent of ByVal from VB.NET ?

    Hi,

    I have a code for resizing borderless form in VB.NET which works fine. Problem is that I cannot translate It to C#. This is original code from VB.NET:

    Code:
    Public Property resizeDir(ByVal frm As Form) As Class1.MoveDirection
            Get
                Return Size_Changed
            End Get
            Set(ByVal value As MoveDirection)
                Size_Changed = value
                Select Case value 'Change cursor
                    Case MovieDirection.Left
                        frm.Cursor = Cursors.SizeWE
                ...
                    Case Else
                        frm.Cursor = Cursors.Default
                End Select
            End Set
        End Property
    And this is what online converter returns me in C#:

    Code:
    public Class1.MoveDirection resizeDir {
    	get { return Size_Changed; }
    	set {
    		Size_Changed = value;
    		switch (value) {
    			//Change cursor
    			case MoveDirection.Left:
    				frm.Cursor = Cursors.SizeWE;
    				break;
    			...
    			default:
    				frm.Cursor = Cursors.Default;
    				break;
    		}
    	}
    }
    Problem are lines (ByVal frm As Form) and Set(ByVal value As MoveDirection), converter didn't translate that. Is there any equivalent for this in C# ?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: C# equivalent of ByVal from VB.NET ?

    It didn't translate them because it doesn't need to. They aren't necessary in C#.

    -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??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: C# equivalent of ByVal from VB.NET ?

    Quote Originally Posted by techgnome View Post
    It didn't translate them because it doesn't need to. They aren't necessary in C#.

    -tg
    Ok, but how to correct code then ? It doesn't work, I have errors on all frm and value. To me It looks like references are missing for those.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# equivalent of ByVal from VB.NET ?

    C# properties can't have arbitrary parameters. Your VB code is basically bad and doesn't really make sense as a property anyway. If it is a property of a form then why would you need to pass in a Form object? It would make sense if it was a property of a form and you removed the Form parameter and then set Me.Cursor in the setter. As it is, there is no C# equivalent of that code, which is why the translator couldn't generate it. Make that change and the translator will work fine.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: C# equivalent of ByVal from VB.NET ?

    I wouldn't say It's bad code. It's a slight modification of code from this post:
    http://www.vbforums.com/showthread.p...orderless+form

    The code in question is in last post (which is my post) If you care to take a look - in a module. Modification is only to use code for multiple windows, everything else is same. So I really don't see why this shouldn't work in C# too.

    P.S.: If you want to test It - It has to be borderless form.
    Last edited by LuckyLuke82; Aug 3rd, 2017 at 02:24 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# equivalent of ByVal from VB.NET ?

    Quote Originally Posted by LuckyLuke82 View Post
    I wouldn't say It's bad code.
    It's bad code. Noone has any business implementing a property like that.
    Quote Originally Posted by LuckyLuke82 View Post
    It's a slight modification of code from this post:
    If the original code was OK then it's a bad modification, otherwise the original code was bad. The issue is the implementation of the property. It's bad. Either that code should be implemented as a property or it should be a property of a form or a class that has a reference to a form instance already. Passing a form to a property like that is just plain bad.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: C# equivalent of ByVal from VB.NET ?

    Then how should It be constructed to use for multiple windows and avoid all that code in each form ?

    Thanks for info, I will try to change code.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# equivalent of ByVal from VB.NET ?

    Take a look at this CodeBank thread of mine:

    http://www.vbforums.com/showthread.p...ved&highlight=

    There are a couple of classes there that provide functionality for an arbitrary form, much as you're trying to do. As you can see, those classes require you to create an instance and pass the form you want to affect as an argument to the constructor. That form gets assigned to a private field and then referred to via that in other members. You can do the same thing. You can then implement your property pretty much as is but without the Form parameter. In the setter, you would refer to the form via that private field instead of the now-missing parameter. That would mean that the C# implementation you already have would work as is.

    I would guess that you could also implement such a class as a component and then be able to add it to a form in the designer and not have to explicitly create an instance and pass the form to the constructor. I've not tried that though, so I'm not sure of the details.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: C# equivalent of ByVal from VB.NET ?

    Oh my god,

    all that work to get same effect. I got lucky and modified different code for resizing my form, It's even shorter than one I use in VB.NET, so I don't need to translate code from this thread anymore. I will try to avoid situations like this in C# as much as possible, until I get use to It.

    Though your code is very useful, specially the one to immobilise form. One question here, It also concerns this thread - why did you use Sealed Class instead of normal - is It safer because It cannot be inherited or what's the trick?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# equivalent of ByVal from VB.NET ?

    Quote Originally Posted by LuckyLuke82 View Post
    Though your code is very useful, specially the one to immobilise form.
    I'm not sure whether you read more into my directing you to that CodeBank thread than I intended but what those classes actually do isn't really relevant to my point. They are simply examples of classes that you can create an instance of within a form and have the form pass itself as an argument and then that instance can act on that form. That specifically seemed relevant to this thread.
    Quote Originally Posted by LuckyLuke82 View Post
    One question here, It also concerns this thread - why did you use Sealed Class instead of normal - is It safer because It cannot be inherited or what's the trick?
    If you specifically intend for a class not to be inherited then you should declared it sealed/NotInheritable. That's all there is to it. It didn't really serve much of a purpose in the case of code shared in the CodeBank because I was providing the source so anyone could create there own version of the class that wasn't sealed/NotInheritable. If I'd provided a compiled assembly though, that would have been another matter.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: C# equivalent of ByVal from VB.NET ?

    f you specifically intend for a class not to be inherited then you should declared it sealed/NotInheritable. That's all there is to it.
    Thanks for answer.

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