Results 1 to 7 of 7

Thread: troubles working with double

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2021
    Posts
    39

    Unhappy troubles working with double

    hello guys i'm trying to compare two columns but the problem is that when the value is double i can't compare it;
    if the value is string then i can work normal with it but if it's double the programme stop working telling me that it had problems converting double to object can you please suggest me how to solve this problem.

    in my columns i have values like cell 1 :2235
    cell2 2685
    cell 3 D2568
    cell 4 E695
    cell 5 3256

    so i can't compare the value that is double to a value that is string
    P.S i only added the part of the program where the problem is .


    Code:
       Dim findme1 As Excel.Range = ws1.Range("A4:A" & Range1.Rows.Count)
                Dim findme2 As Excel.Range = ws2.Range("F5:F" & Range2.Rows.Count)
                Dim MyArray As Object(,) = CType(findme1.Value, Object(,))
                Dim MyArrayy As Object(,) = CType(findme2.Value, Object(,))
    for n=1 to MyArray.length
    for m=1 to MyArrayy.length
    if MyArray(n,1)=MyArrayy(m,1) then  ' can't convert string to doubt
    ....
    end if 
    next m
    next n
    Last edited by highfly884; Oct 6th, 2021 at 09:21 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: troubles working with double

    This seems to be a typo:
    Code:
    if MyArray(n,1)=MyArrayy(m=1) then
    Presumably you wanted: m, 1

    Currently you are only specifying one dimension of a 2 dimensional array.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2021
    Posts
    39

    Re: troubles working with double

    i work with Myarray(n,1) and i did put it in textbox to see the result and does indeed contain the value of the cell

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: troubles working with double

    I'm not sure whether that line would ever work, even if that typo were fixed. You are boxing the doubles into objects. A double is a value type, an Object is a reference type, so what you have in those arrays seem like they would be addresses of the actual values. You would normally compare two references with Is or IsNot, not equals. In any case, you don't want to be comparing the addresses, because they will NEVER be equal, in the case shown. You'd want to compare the underlying doubles. VB may be doing that, if it is implicitly converting from Object back to Double, but relying on that isn't a good idea. You already found that you could convert them to strings and compare them, what happens if you convert them to Double with CDbl() and compare those?
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2021
    Posts
    39

    Re: troubles working with double

    even if i try to use CDbl() it doesn't work and return the same problem (can't convert string into double : this is what it shows me " 'Conversion from string "D404" to type 'Double' is not valid.' ")

    btw to Myarray i add findme1.value so i'm adding the value in the range not the range so i don't thing that th Myarray contains ranges ? or am i range ? can you correct me please

    and do you have any idea how to stock the value of the cell rather than the addresses please ? and thank you so much
    Last edited by highfly884; Oct 6th, 2021 at 11:00 AM.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: troubles working with double

    Of coarse you can't convert "D404" to a double. Is that actually the value you want to convert to a double? If so, you need to strip off the "D" first.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: troubles working with double

    Why not compare all of your values as strings?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width