dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Testing an Array

  1. #1
    Join Date
    Nov 2009
    Posts
    18

    Testing an Array

    I have the following code

    if (Int32.TryParse(this.txtAssemblyNo.Text.Trim(), out trap))
    {
    while (txtAssemblyNo.Text.Trim().Length < this.txtAssemblyNo.MaxLength)
    {
    txtAssemblyNo.Text = "0" + txtAssemblyNo.Text.Trim();
    }
    }



    I need to change the test to check if an array called cSync[] which has 6 subsets has a numeric value. What I have so far is,

    string cSyncStr = cSync[0];
    if (Int32.TryParse(cSyncStr.Trim(), out trap))
    {
    while (cSyncStr.Trim().Length < this.txtAssemblyNo.MaxLength)
    {
    cSyncStr = "0" + cSyncStr.Trim();
    }
    cSync[0] = cSyncStr;
    }



    I'm not sure yet how to get the value from the txtAssemblyNo.MaxLength since it is in another class. Any help is greatly appreciated.

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Testing an Array

    I am having a very hard time understanding:
    1) What you're trying to achieve
    2) Your variable names are very oddly named, and I'm having a hard time following what exactly they mean...

    Can you write a simple example, and name your stuff so it makes sense to people not involved with the details of your work, and also use [ code] [ /code] tags around your code in the forums so we can read it easier.

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Testing an Array

    Quote Originally Posted by JVisconti View Post
    I'm not sure yet how to get the value from the txtAssemblyNo.MaxLength since it is in another class.
    I don't see this another class. where do you store its instance and where are you trying to access it

    post more code if possible.

    ... and please use the code tags or otherwise I don't know if I want to read it agian
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Testing an Array

    Quote Originally Posted by JVisconti
    I have the following code
    Code:
    if (Int32.TryParse(this.txtAssemblyNo.Text.Trim(), out trap))
    {
       while (txtAssemblyNo.Text.Trim().Length < this.txtAssemblyNo.MaxLength)
       {
          txtAssemblyNo.Text = "0" + txtAssemblyNo.Text.Trim();
       }
    }
    Right...from what I can see you are trying pad the numeric data with leading zeros. So basically you want a fixed length string representing numeric data padded with leading zeros depending on the size of the number. If that's what you want there are easier ways to achieve it.

    I'm not sure what you are trying to do with the second bit of code. Looks very similar to the first except for some changes in the variables...
    Last edited by nelo; January 8th, 2010 at 12:01 PM. Reason: Corrected formatting...

  5. #5
    Join Date
    Nov 2009
    Posts
    18

    Re: Testing an Array

    I apologize, I don't usually post too much on forums.

    The first part of the code is testing if txtAssemblyNo.Text.Trim() is numeric or alpha. If the length of txtAssemblyNo is less than the MaxLength property set for it, then it gets zero padded to match the MaxLength property setting.

    I need to do the same for the array.

    the txtAssemblyNo is defined and used in a different class. I simply need to find a way to test the cSync[] array to see if it is numeric or alpha, and if not, then zero pad the value in that subset to 15 characters.

  6. #6
    Join Date
    Nov 2009
    Posts
    18

    Re: Testing an Array

    to nelo, you basically got what I'm trying to do. The biggest part is to zero pad until the length of the numeric matches the maxlength of txtAssemblyNo. The maxlength needs to be 15. The second bit of code was my attempt at testing the array in the same format.

  7. #7
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Testing an Array

    Ok. Thanks for clarifying that. Here's how I would do it:
    Code:
    public class CustomFormatter
    {
       public string Format(string input)
       {
          // If we don't pass a valid number
          // the method returns the original string.
          // This could be changed to return an empty string.
          string outputValue = input;
          int convertedValue;
          if (Int32.TryParse(input.Trim(), out convertedValue))
          {
             // I've hardcoded the padding to create a 15 character output.
             output = String.Format( "{0:D15", convertedValue);
          }
          return output;
       }
    }
    Something like this would do. The next step would be to enable a flexible length for the output string. I'll let you work that out... You don't have to do it in a separate class but it can make it easier to test just this functionality independently of application which is a useful thing. So where ever you need this functionality you just create an instance of the class and call the method.

    With regards to testing the array it is not clear from the context what the array is. Sometimes it looks like an array of strings (in which case you want to test each element of the array) but other times it looks like an array of characters. Which one is it? If it is an array of strings then the method above will work in exactly the same way. You just iterate through the array and call the method for each one. If it is an array of characters you just convert it to a string and then call the method above.
    Last edited by nelo; January 8th, 2010 at 01:42 PM. Reason: Additional information...

  8. #8
    Join Date
    Nov 2009
    Posts
    18

    Re: Testing an Array

    Ok, thank you for that information. It is a character array with 6 elements. I have everything working except converting it back to an array. I have

    Code:
    cSync[0] = cSyncStr.ToCharArray();
    I get the error:

    Cannot implicitly convert type 'char[]' to 'char'

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Testing an Array

    You have an array of chars named cSync. It is not an array of array of char, so you cannot assign an array to element, you may only assign a single char.

  10. #10
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Testing an Array

    What BigEd871 is saying is that you want something like this...
    Code:
    cSync = cSyncStr.ToCharArray();
    You've probably sorted out the problem anyway...

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)