0

The ra.Value in the while loop sometimes is DateTime type and unfortunately the .ToString() method is not enough to convert it to string. What I should change on my code, to recognize when ra.Value is DateTime type and convert it to String. Now I have the exception that conversion from type 'DateTime' to type 'String' is not valid.

With ro.Attributes.GetEnumerator
    While .MoveNext
        Dim ra As ReportAttribute = CType(.Current, ReportAttribute)
        Dim lstrColumnReportObj As Integer = ra.Column
        Dim lstrValueReportObj As String = ra.Value.ToString()
        lstrParTable(lstrColumnReportObj) = lstrParTable(lstrColumnReportObj) + lstrValueReportObj
    End While
End With

Thank you.

Alex
  • 4,821
  • 16
  • 65
  • 106
  • 8
    *unfortunately the .ToString() method is not enough to convert it to string* That doesn't make sense. `ToString()` returns a `String`. Always. – sloth Aug 26 '13 at 14:12
  • 2
    Just at a glance I'd say this is probably where your problem is: `lstrParTable(lstrColumnReportObj) + lstrValueReportObj`. Are you sure that `lstrParTable(lstrColumnReportObj)` is not a DateTime? You can't concatenate a DateTime and a string. – Joel Etherton Aug 26 '13 at 14:14
  • @DominicKexel what could be the problem then, according to you? I really don't understand why it's not working and gives me that exception. – Victoria Pappa Aug 26 '13 at 14:14
  • What is "lstrParTable"? – htxryan Aug 26 '13 at 14:15
  • I think you need to specify the *format* with the `ToString` method on a DateTime value. http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx – David Zemens Aug 26 '13 at 14:16
  • 1
    There are many ways to convert a datetime to a string. See here http://stackoverflow.com/questions/3477735/convert-datetime-to-string-formatyyyymmdd – user1477388 Aug 26 '13 at 14:17
  • 1
    Which line gives the error? – SLaks Aug 26 '13 at 14:18
  • @JoelEtherton for sure I have define it as Dim lstrParTable(11) As String :( – Victoria Pappa Aug 26 '13 at 14:18
  • 1
    Some questions: What is `lstrParTable`? Is `OPTION STRICT` set to `ON`? Is `OPTION INFER` set to `ON`? Why do you use `GetEnumerator` and `MoveNext` instead of `For Each`? – sloth Aug 26 '13 at 14:23
  • @DominicKexel I'm a beginner so I don't know how these matter, but I have used Option Explicit On and Option Strict On. If I use For Each it will help? I need sth like "if ra.Value is DateTime type then ra.Value = ra.Value.ToString("d MMM yyyy")" Can I do that? ra.Value sometimes is string, sometimes datetime in the loop. – Victoria Pappa Aug 26 '13 at 14:33
  • The StackTrace within the Exception should tell you what line is causing the error, please let us know which line – msmucker0527 Aug 26 '13 at 14:38
  • @msmucker0527 it's that line that causes the error Dim lstrValueReportObj As String = ra.Value.ToString() – Victoria Pappa Aug 26 '13 at 15:06

1 Answers1

0

The simple solution that you are probably looking for is this:

With ro.Attributes.GetEnumerator
    While .MoveNext
        Dim ra As ReportAttribute = CType(.Current, ReportAttribute)
        Dim lstrColumnReportObj As Integer = ra.Column
        Dim lstrValueReportObj As String = Nothing
        If TypeOf ra.Value Is Date Then
            lstrValueReportObj = ra.Value.ToString("d MMM yyyy")
        Else
            lstrValueReportObj = ra.Value.ToString()
        End If
        lstrParTable(lstrColumnReportObj) = lstrParTable(lstrColumnReportObj) + lstrValueReportObj
    End While
End With

However, this does smack of bad practice and you may want to reconsider your overall design. Usually, when you have to start putting in special logic for certain types, like that, it's usually a sign that you could be doing it in a better way. For instance, what will happen when you start needing custom formatting for other types as well. Once you have more than a handful of exceptions, it's going to get unwieldy and the code will be brittle (prone to bugs).

Also, I feel obligated to mention that hungarian notation is discouraged in .NET. If you are required to use it by your employer, that's fine, but otherwise, I'd recommend reading through the official Microsoft naming guidelines.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105