none
Calling class and set strings RRS feed

  • Question

  • Hello,

    I've a problem in my code. I have a single class and a class from a form. In the single class there is a sub with byval strings that I want to edit in the class of the form, but I want also to call that sub from the form class. Ive tried a lot, but still getting errors. Now, I can only run OR edit strings. I've tried to make a second sub in the single class, and I can call that sub from the form class, but I cant call the strings from that sub.

    This is what I have.

    Single class (class2)

    Public Sub abcd(byval string1 as string)

    code....(I want to use string1)

    End sub

    form class:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim efgh As New Class2
            efgh .abcd(TextBox1.Text)
            Call efgh .abcd()
        End Sub

    With regards,

    Martin

    Friday, January 12, 2018 11:34 PM

All replies

  • Hello,

    I've a problem in my code. I have a single class and a class from a form. In the single class there is a sub with byval strings that I want to edit in the class of the form, but I want also to call that sub from the form class. Ive tried a lot, but still getting errors. Now, I can only run OR edit strings. I've tried to make a second sub in the single class, and I can call that sub from the form class, but I cant call the strings from that sub.

    This is what I have.

    Single class (class2)

    Public Sub abcd(byval string1 as string)

    code....(I want to use string1)

    End sub

    form class:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim efgh As New Class2
            efgh .abcd(TextBox1.Text)
            Call efgh .abcd()
        End Sub

    With regards,

    Martin

    Martin,

    The most common way would be with a property. Set up a backing field then set up a property which can read and write to that backing field.

    You might also want to have it test to see whether or not the value is null or empty.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, January 13, 2018 12:03 AM
  • Here is an example, note there is a default and overridden constructor. Note how we can set the single property several different ways.

    Here is the deal, when something fails, in this case do a Google search on say "VB.NET working with classes", traverse through the results, learn from them. The best way to learn is really to understand (in this case classes, properties, methods, events and scope) the topic at hand. Even consider seeing what resources are available for C# (yes C#) as many times there is great information there that can be used via a C# to VB.NET converter.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sample As New Class2(TextBox1.Text)
        sample.SomeMethod()
        sample.MyText = ""
        sample.SomeMethod()
    
        sample = New Class2 With {.MyText = TextBox1.Text}
        sample.SomeMethod()
    End Sub

    Public Class Class2
        Public Property MyText As String
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal pMyText As String)
            MyText = pMyText
        End Sub
        Public Sub SomeMethod()
            If Not String.IsNullOrWhiteSpace(MyText) Then
                MessageBox.Show(MyText)
            Else
                MessageBox.Show("MyText not test")
            End If
        End Sub
    End Class
    
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 13, 2018 12:21 AM
    Moderator
  • Thank you! I will search the internet to learn more from classes!

    With regards,

    Martin

    Saturday, January 13, 2018 9:22 PM
  • Hello,

    I've a problem in my code. I have a single class and a class from a form. In the single class there is a sub with byval strings that I want to edit in the class of the form, but I want also to call that sub from the form class. Ive tried a lot, but still getting errors. Now, I can only run OR edit strings. I've tried to make a second sub in the single class, and I can call that sub from the form class, but I cant call the strings from that sub.

    This is what I have.

    Single class (class2)

    Public Sub abcd(byval string1 as string)

    code....(I want to use string1)

    End sub

    form class:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim efgh As New Class2
            efgh .abcd(TextBox1.Text)
            Call efgh .abcd()
        End Sub

    With regards,

    Martin

    A good example how classes not should be used. A class may not have dependency to its caller or the caller should be passed to the class. 

    In this case there is not any reason to make a separated class. 


    Success Cor

    Sunday, January 14, 2018 8:52 AM
  • But you can of course use a function and then it becomes suddenly complete different. 

    Functional programming means avoiding programming with Subs (or better void in C type languages). Some have made a kind of religion from it. That is not always needed. 

    However, your problem would then be solved by

    Dim myHelperClass as new HelperClass MyTextBox.Text = MyHelperClass.ChangeText(MyTextBox.Text) Class HelperClass Public Function ChangeText(TheString as String) as String dim X = TheString 'With changes Return X 'to make it more clear

    End Class

    Done in this message so be aware of typos.



    Success Cor





    Sunday, January 14, 2018 10:55 AM
  • But you can of course use a function and then it becomes suddenly complete different. 

    Functional programming means avoiding programming with Subs (or better void in C type languages). Some have made a kind of religion from it. That is not always needed. 

    Just some clarification for anyone else reading this... I'm sure Cor understands this, the phrasing just isn't clear.  Functional Programing isn't specifically about using "function" methods, its about how the program is structured.

    Functional programming means that you try to treat all methods like mathematical functions, meaning the method always returns the same values with the same input parameters - there is no "state" for the method or its containing class.  You also try to keep all of your custom types immutable.

    So technically you could follow a functional programing paradigm using Sub(ByRef param), assuming the only change Sub() makes is to param. Not that this would be clean or clear, but it could be done.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, January 14, 2018 1:21 PM
    Moderator