Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:29:34+00:00 2026-06-08T04:29:34+00:00

I am writing a script in which I make many custom data tables, and

  • 0

I am writing a script in which I make many custom data tables, and for this reason I would like to make a function that generates the tables for me. Specifically, I would like the function to create the table and the columns of the table and then return that table so that the calling function can add row elements to the returned table. However, whenever I return a table without rows, I am told that I cannot add rows to it, since the element is empty. Here is what I have tried so far:

Function Generate-Table ($Name, [String[]]$ColumnNames, [Type[]]$DataTypes) {
    $table = New-Object system.Data.DataTable "$Name"

    for ($i = 0; $i -lt $ColumnNames.Length; $i++) {
        $col = New-Object system.Data.DataColumn $ColumnNames[$i],($DataTypes[$i])
        $table.columns.add($col)
    }
    return $table
}

$ColumnNames = @("Name", "Age")
$DataTypes = @([string], [int])
$table = Generate-Table "OutputTable" $ColumnNames $DataTypes

$row = $table.NewRow();
$row.Name = "Bob"
$row.Age = "20"
$table.Rows.Add($row);

$table

Which gives me the error:

You cannot call a method on a null-valued expression.
At C:\Users\mrpadmin\accounting\test.ps1:15 char:21
+ $row = $table.NewRow <<<< ();
    + CategoryInfo          : InvalidOperation: (NewRow:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Now, if I do the same, but replace line 13 with:

. Generate-Table "OutputTable" $ColumnNames $DataTypes

It correctly gives me the following output:

Name    Age
----    ---
Bob      20

Is there any way to return a data table object with no rows? And if not, is there another way to make a function that can return data tables ready to be filled with rows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:29:35+00:00Added an answer on June 8, 2026 at 4:29 am

    I think your problem will be solved by simply using the unary comma operator in front of the table you’re returning:

    return ,$table
    

    Powershell does some interesting automatic unrolling of collections that implement IEnumerable. The comma operator will wrap your table in a single-object array, so when it gets automatically unrolled it will return back to the table. I don’t know of a way to force Powershell to not do this.

    Here is a reference to the operators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a backup script which Copies the data to backup disk. Flushes the
I'm writing a Groovy script which uses third party java code that I can't
I'm currently writing a little commercial PHP Script which would be a VPN (PPTP)
I was working on (for fun) writing a script that would recognize palindromes. So
I am writing a script which will do this: #!/bin/bash mysqldump -u user -p
I'm writing a greasemonkey script to make it so that when a user clicks
I am currently writing a script which parses the ServiceTage, Computername and Username from
I am writing a script which counts all files and folders recursively. I tried
I'm writing a script which is to be executed hourly. It basically works as:
I am writing python script which gets links from website. But when I tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.