• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Passing values new to vb.net

Hi all,

Just trying to get my head around vb.net, done some reading about classes and have a play now.

i have a question, i have this code below, i want it to display the strg string. i can get it to display the strSetupMessage message  by saying Console.WriteLine(SetupGo.strSetupMessage) but for some reason it will not display the other class if i say (SetupGo.strg).

Just having a play and want to know, thank in advance for you help.
 

Module Module1
    Sub Main()
        Dim SetupGo As New Setup


        Dim strtest As String

        Console.WriteLine(SetupGo.strg)
        Console.ReadLine()



    End Sub

End Module

-----

Public Class Setup
    Public strSetupMessage As String
    Public strg As String



    Sub New()

        ' set the defaults:
        strSetupMessage = "White"



       
     
    End Sub



    Sub New(ByVal Value As String)
        strg = "hello"
        strg = Value
    End Sub


End Class
0
pigmentarts
Asked:
pigmentarts
  • 2
2 Solutions
 
pigmentartsAuthor Commented:
Sorry for got to say, i know how to get it to display text, but i want to understand why i can not display the other string in the class

  Sub New(ByVal Value As String)
        strg = "hello"
        strg = Value
    End Sub

when i call it in the module.

sorry if this is a easy question but we all have to start somewhere  
0
 
ptakjaCommented:
If I understand you right, you want the strg to be displayed. With the code you have, strg is never initialized, which is why nothing comes out. Just because you have 2 New methods (subs) doesn't mean one supercedes the other. You need to explicity call one when you create your class:


Module Module1
    Sub Main()
        Dim SetupGo As New Setup("Hi There")   '<<<< This calls your 2nd New method (aka "Constructor")

        Dim strtest As String

        Console.WriteLine(SetupGo.strg)
        Console.ReadLine()

    End Sub

End Module


In this example I am passing the string "Hi There" into your Setup class when I instantiate the class. VB.NET looks at the argument signature (the number of arguments and their data types) and figures out that you want to call the one that accepts 1 string argument.

Now, when you go to write the value by executing

Console.WriteLine(SetupGo.strg)

This will retrieve the value.  Notice though that you set strg = "hello" and then you set it equal to Value which is the value I  passed it.  You will never see the string "hello", unless you pass it in.

Hope that helps.


0
 
koruzicaCommented:
This is becouse you never initalise that string.

You are using a following constructor:

Dim SetupGo As New Setup

for constructing your class. and there all you set is strg and strSetupMessage stays uninitalised.

So if you create your class this way:

Dim SetupGo As New Setup("someText")

you would have strSetupMessage available but not strg !

If you have any other questions, please ask.

Simon
0
 
ptakjaCommented:
Oh, one other thing, you need to make your New sub's Public or you won't be able to see them...

Public  Sub New()
        ' set the defaults:
        strSetupMessage = "White"
        strg = "New() Called with no arguments"
End Sub

Public Sub New(ByVal Value As String)
        strg = Value
End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now