Greetings everyone,

This is my first post, so I hope I don't break any rules by mistake...

I am an experienced programmer trying to learn VB.NET. The problem I'm having is that it seems like strings have to be initialized differently than other types and I don't know what I'm doing wrong.

Can anyone identify the problem in this bit of code, and show me how to fix? I've commented the line causing trouble.

Thanks,
Bill

Public Class Class1

    Private intNum() As Integer
    Private dateString() As String

    Public Sub New(ByRef s As String())

        intNum(s.Length - 1) = New Integer      'No error.
        dateString(s.Length - 1) = New String   'Overload resolution failed because no accessible 'New' accepts this number of arguments.

    End Sub

End Class
Member Avatar for Unhnd_Exception

I don't know you get by with your first line of no error.
You need to initialize the array first before you assign it a value.

New String takes parameters in its constructor. If you want an empty
string use string.Empty

Private intNum() As Integer
    Private dateString() As String

    Public Sub New(ByRef s As String())

        ReDim intNum(s.Length - 1)
        ReDim dateString(s.Length - 1)

        'This will assign an empty string to the last index of the array.
        dateString(s.Length - 1) = String.Empty
    End Sub

End Class

Hi, thanks for the reply.

What I am trying to do is declare my vars in the class, then set their size and initialize them in the constructor. I'm taking this from one of my Java programs that I am trying to convert to VB as an exercise to help me learn the language. Here's a piece of the Java code:

public class StockDataRecord {

    // Declare vars and set their access level
    private String[] date;
    private float[] open;
    private float[] high;
    private float[] low;
    private float[] close;
    private int[] volume;
    private float[] adjClose;


    // Constructor
    StockDataRecord(String[] s) {

        // Set size of each array
        date = new String[s.length];
        open = new float[s.length];
        high = new float[s.length];
        low = new float[s.length];
        close = new float[s.length];
        volume = new int[s.length];
        adjClose = new float[s.length];

        // Set initial values
        for (int n = 0; n < s.length; n++) {
            StringTokenizer st = new StringTokenizer(s[n], ",");
            date[n] = st.nextToken();
            open[n] = Float.parseFloat(st.nextToken());
            high[n] = Float.parseFloat(st.nextToken());
            low[n] = Float.parseFloat(st.nextToken());
            close[n] = Float.parseFloat(st.nextToken());
            volume[n] = Integer.parseInt(st.nextToken());
            adjClose[n] = Float.parseFloat(st.nextToken());
        }

    } // End constructor

There's more to it but you can see how I'm declaring the class vars and then setting their size and value in the constructor.

In your code sample your are re-dimensioning the variables before their size is set. This seems odd, and I'm not sure it's necessary to do this because I get no errors with the following code, so maybe this is just the preferred technique in VB?

Public Class StockDataRecord

    Private dateString() As String
    Private open() As Double
    Private high() As Double
    Private low() As Double
    Private close() As Double
    Private volume() As Integer
    Private adjClose() As Double

    Public Sub New(ByRef s As String)

        dateString(s.Length - 1) = String.Empty   'Why should this be different from all the others?
        open(s.Length - 1) = New Double
        high(s.Length - 1) = New Double
        low(s.Length - 1) = New Double
        close(s.Length - 1) = New Double
        volume(s.Length - 1) = New Integer
        adjClose(s.Length - 1) = New Double

    End Sub


End Class

What's really bugging me is this line:

dateString(s.Length - 1) = String.Empty

Why should it be different from the way other array types are handled? Why not

dateString(s.Length - 1) = New String

Thanks,
Bill

Hi Shahan,

Thanks for the reply. When I try it your way I get error squiggles under New String, and the error, "Overload resolution failed because no accessible 'New' accepts this number of arguments" when I hover the mouse over the squiggles.

That's how this thread started. Unhnd Exception provided a solution like this: dateString(s.Length - 1) = String.Empty

So, the question now is, is that really the correct technique?

Thanks,
Bill

The idea behind my previous post is that the datatypes under ValueType has some default value(if we not provided when declare it) and "String" is a RefType, so we need to handle it ourself for a default value.

Unhnd_Exception has provided you the correct technique.

Member Avatar for Unhnd_Exception

This:

Public Class StockDataRecord   

    Private dateString() As String
    Private open() As Double
    Private high() As Double
    Private low() As Double
    Private close() As Double
    Private volume() As Integer
    Private adjClose() As Double

    Public Sub New(ByRef s As String)

        dateString(s.Length - 1) = String.Empty   'Why should this be different from all the others?
        open(s.Length - 1) = New Double
        high(s.Length - 1) = New Double
        low(s.Length - 1) = New Double
        close(s.Length - 1) = New Double
        volume(s.Length - 1) = New Integer
        adjClose(s.Length - 1) = New Double

    End Sub


End Class

Should Be This:

Public Class StockDataRecord

    Private dateString() As String
    Private open() As Double
    Private high() As Double
    Private low() As Double
    Private close() As Double
    Private volume() As Integer
    Private adjClose() As Double

    Public Sub New(ByRef s As String)

        ReDim dateString(s.Length - 1)
        ReDim open(s.Length - 1)
        ReDim high(s.Length - 1)
        ReDim low(s.Length - 1)
        ReDim close(s.Length - 1)
        ReDim volume(s.Length - 1)
        ReDim adjClose(s.Length - 1)

    End Sub


End Class

Thank you Unhnd and Shahan. I am using the most recently posted solution without error.

Regards,
Bill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.