Question using me in modules?

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have a form and I am calling a method from a module in that form. However, how to do i call the object into the method, since Me doesn't work.

Example:
in the module:
VB.NET:
Function printname()
Console.WriteLine("Form called with is titled:" + me. text)
return me.text
 End Function

in the form:
VB.NET:
Private Sub buttonfindname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonfindname.Click
 Console.WriteLine("this form is titled:" + printname())
    End Sub

also, how do i delete a thread i made; i accidentally posted in the wrong section a while ago :p
 
'Me' always refers to the current instance of the current type. If you want to refer to something using 'Me' then you need to be inside that things own code. Think about it. If someone else says 'Me' does it mean you? Of course not. It means them. They are referring to themselves. The same goes in code. Any time a class contains Me it is referring to itself.

Not only that, you don't create an instance of a module anyway, so 'Me' is never valid in a module.

If you want a method in a module to refer to an object then you have to pass that object to the module somehow. That might involve assigning it to a property or passing it to the method as an argument. If your module method wants to get the Text of a form then you should pass that form to the method as an argument. If the form is calling the method then the form needs to pass itself to the method as an argument. How does a form refer to itself? Using 'Me'. The form needs to pass 'Me' to the method as an argument.
 
So the only why to pass the object into the method is by adding it into the parameter? I'm used to java where you can call class using the 'this' keyword. IE: Me.printname();
VB.NET:
Function printname(ByVal passedclass As System.Object)
Console.WriteLine("Form called with is titled:" + passedclass . text)
return passedclass .text
 End Function
VB.NET:
Private Sub buttonfindname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonfindname.Click
 Console.WriteLine("this form is titled:" + printname(Me))
    End Sub
 
Because Modules are essentially the equivalent of a static class in java, there is no concept of "me" or "this" because there can never be any other than just that one in the computer's memory. You hence refer to it by its given name, rather than me:

VB.NET:
Public Module MyModule
  Public Txt as String = "Some text"

  Public Sub PrintName()
    Console.WriteLine("Module text:" & [B]MyModule.Txt[/B])
  End Sub
End Module

There can only ever be one MyModule. In having to write MyModule.Text instead of Me.Text you're demonstrating that you understand this concept. I'm sure the VB guys could have made Me work, but it's nonsensical. Often modern programming languages arrange their syntax in a certain way/force you to do things in a certain way, as a demonstration that you understand what you're doing


If you want to print the text of a form, of course you must pass it as a parameter:

VB.NET:
Public Module MyModule
  Public Txt as String = "Some text"

  Public Sub PrintFormText(f as Form)
    Console.WriteLine("Form text:" & [B]f.Text[/B])
  End Sub
End Module

One thing that may be confusing to you: Old VB6 automatically made an instance of a form called exactly the same name as the class itself so you'd use it in ways that looked static, but they were operating on an instance


Some other things I need to tidy up in your code:

Naming Conventions! Drop the java!
_thisIsAMemberVariable
ThisIsASub_FunctionOrProperty
IDIsA2LetterAcronym 'use 2 caps
TcpIsAThreeLetterAcronym '3 letters and above, treat like word

NEVER use + to concatenate strings, ALWAYS use &

Always give your Functions a return type

Program with Option Strict and Option Explicit set to ON in project prefs
 
I'm sure the VB guys could have made Me work, but it's nonsensical.
No they couldn't. As I said, Me refers to the current instance. There are no instances of modules so Me has no meaning. It is exactly the same as 'this' and static classes in C#. It's a long time since I've touched Java but I would imagine that 'this' and static classes behave the same in Java as they do in C#, so it's no different.
One thing that may be confusing to you: Old VB6 automatically made an instance of a form called exactly the same name as the class itself so you'd use it in ways that looked static, but they were operating on an instance
VB 2005 re-introduced the concept of the default instance for forms. You can access the default instance using just the form class's name, so it works like VB6. That's syntactic sugar though, because Form1.Show() is just a shortcut for My.Forms.Form1.Show().

That said, default instances cause as many issues as they solve so I generally recommend not using them at all. Forms are objects like any other so it is best to treat them like any other object.
 
Think of a Module as a Class where all members is declared Shared. For a Shared method there is no current class instance, so Me keyword is not valid.
false74 said:
IE: Me.printname()
This can only be valid if Printname method is a member of current class instance.
 
Wow, thanks guys! I never really understood modules real well, and I'm sure there's a lot more to them, but this was real helpful.

Think of a Module as a Class where all members is declared Shared. For a Shared method there is no current class instance, so Me keyword is not valid.
That makes perfect sense, I'm still playing with vb.net and java, so I'm trying to get the equivalents here, thanks.

NEVER use + to concatenate strings, ALWAYS use &
Can you explain why it is bad to use + and not & signs? I assume the + takes precedence when dealing with two int values, so it would try and add them together first instead of printing them both. If that's why, couldn't you always use the .tostring method?

Public Sub PrintFormText(f as Form)
Console.WriteLine("Form text:" & f.Text)
End Sub
When you pass the 'f' --the form into this method, it's creating essentially another pointer to that form/object correct? So no memory space is "wasted", or would it better if this method was part of the forms class, so you would not have to pass it in?
 
Can you explain why it is bad to use + and not & signs?
This article explains: Concatenation Operators in Visual Basic
Basically, using & operator is more readable when adding string because that is it sole purpose, and less errorprone if you don't handle types properly.
When you pass the 'f' --the form into this method, it's creating essentially another pointer to that form/object correct? So no memory space is "wasted",
Yes, Form type is a class, ie a reference type, so it is just a reference to existing object that is passed to this method.
 
Back
Top