1

If I have an array called standard and i want the count of that array i would do standard.Count

but

if i was to have a string array called doorType and i add the string standard to the first position of that array but i wanted to reference standard.Count is there a way i can manipulate doorType[0].Count so that it gets the number of elements in the standard array?

public static List<string> doorType = new List<string> 
{
    nameof(standard),
    nameof(original)
};

public static List<string> standard= new List<string>
{
    "1",
    "4",
    "6"
};

How can i make the second line equal?

string xform = doorType[0]  + standard.Count;

string xform = doorType[0]  + doorType[0].Count;

Some extra helpful code

        public static void AddDropDown(string cellInsert, string formula)
        {

            var cell = x.Range[cellInsert];
            cell.Validation.Delete();
            cell.Validation.Add(
               Excel.XlDVType.xlValidateList,
               Excel.XlDVAlertStyle.xlValidAlertInformation,
               Excel.XlFormatConditionOperator.xlBetween,
               formula,
               Type.Missing);
            cell.Validation.IgnoreBlank = true;
            cell.Validation.InCellDropdown = true;
            cell.Value = "Please Select";



        }//end AddDropDown



string xform =
                "=IF(B2=" + DO.quote + doorType[0] + DO.quote + "," + "E1:E" + standard.Count +
                ",IF(B2=" + DO.quote + doorType[1] + DO.quote + "," + "F1: F" + original.Count +
                "," + "I1: I3" + "))))";
            AddDropDown("B4", xform);

To explain more clearly I want to append strings to xform via a for loop using previously made arrays.

This is all for linking C# to excel

13
  • There is no way to do that if doorType stays as a List<string>. doorType should be something like a List<List<string>>. Commented Jan 28, 2020 at 14:50
  • @Sweeper i was starting to think that Commented Jan 28, 2020 at 14:51
  • 3
    What's the question behind the question here? This request seems really strange without any context. If you explain why you're trying to do this specific thing, you might get more useful feedback with a much better approach for what you're trying to do. Commented Jan 28, 2020 at 14:52
  • 1
    @PatrickRoberts Im linking c# code to Excel and making drop downs. i am linking them to previous projects that i have made. Basically I want to make this into a for loop "=IF(B2=" + DO.quote + doorType[0] + DO.quote + "," + "E1:E" + StandardDoor.Count + Commented Jan 28, 2020 at 14:58
  • 1
    @PatrickRoberts I've given it an edit Commented Jan 28, 2020 at 15:06

2 Answers 2

2

Same as Sweeper's comment, or you could create a class DoorType?

public class DoorType
{
   public List<string> DoorTypes { get; set; }
}

Then in your standard list:

List<DoorType> standardList = new List<DoorType>(); 

Then you can add each DoorType to your standardlist.

standardList.Add(doorType); // make sure the doorType object and doorType.DoorTypes exists.

// refer to any items
standardList[0].DoorTypes[x] 

Just a way.

Sign up to request clarification or add additional context in comments.

Comments

1

In C# you can't (easily1) manipulate an object from its name, that's it, you can't manipulate the object standard (and get its count) just because you know the name as a string "standard".

Instead of putting objects names in the doorType list, put references to the objects in it. doorType will not be a list of string but a list of a list of string. Then you may write:

public static List<string> standard = new List<string>
{
    "1",
    "4",
    "6"
};

public static List<string> original = new List<string>
{
    "a",
    "b",
    "c"
};

public static List<List<string>> doorType = new List<List<string>>
{
    standard,
    original
};

And at this point, doorType[0] is a reference to standard and you can write doorType[0].Count to get standard.Count.

Edit:

If you need more information in the element of doorType (like the original name of the referenced list), then you must build a class that will store these information.

Let's name it DoorType:

public class DoorType
{
    public DoorType(List<string> values, string name)
    {
        Values = values;
        Name = name;
    }

    public string Name { get; }
    public List<string> Values { get; }
}

public static List<DoorType> doorTypes = new List<DoorType>
{
    new DoorType(standard, nameof(standard)),
    new DoorType(original, nameof(original))
};

And at this point, doorType[0].Values is a reference to standard and you can write doorType[0].Values.Count to get standard.Count, doorType[0].Name is the string "standard".

And you may write:

string xform = "=IF(B2=" + DO.quote + doorTypes[0].Name + DO.quote + "," + "E1:E" + doorTypes[0].Values.Count

And from here made a Linq call or a foreach loop on the doorTypes collection.


1 In fact you can using reflection, but it's not a good pattern for your task.

4 Comments

If I'm using string.Join(",", doorType.ToArray()); for a normal List how can i manipulate this to be used for the new List<List<string>>?
@Joelad, I edited my answer, but I will not go any further since at this point it's just began to be a "How to do POO when I came from excel - 101" ;-)
@Joelad, if you only use the Count property of Values, then replace List<string> Values by int Count and call new DoorType(standard.Count, nameof(standard))
I got the answer to my query before var arrayL1 = string.Join(",", doorTypes.Select(p => p.Name)); AddDropDown("B2", arrayL1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.