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

  • SEARCH
  • Home
  • 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 6737615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:11+00:00 2026-05-26T11:16:11+00:00

I have some Razor code that has conditional logic based on whether a certain

  • 0

I have some Razor code that has conditional logic based on whether a certain array variable is set. I’m having a heck of a time figuring out the best way to handle when it’s null and I’m not happy with my kludgy solution.

Here’s a simple example of what’s happening:

@{
    if (ViewBag.Foo != null)
    {
        double[] bar = new double[ViewBag.Foo.Length];
    }
}

Later on in the code, I’ll have something like this:

@if (ViewBag.Foo != null)
{
    ...some code that uses `bar` goes here ...
}

With that code, I get errors when ViewBag.Foo actually is null. I get an exception thrown complaining about the second section of code that uses bar and it not being in scope . However, in the execution, that second section will always be skipped.

After messing with it for awhile, I just did this instead:

double[] bar;
@{
    if (ViewBag.Foo != null)
    {
        bar = new double[ViewBag.Foo.Length];
    }
}
else
{
    bar = new double[1];
}

With this change, the code works when ViewBag.Foo is null and non-null. There’s gotta be a better way to handle this… anyone?

  • 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-05-26T11:16:12+00:00Added an answer on May 26, 2026 at 11:16 am

    This sort of work does not belong in a view:

    @{
        if (ViewBag.Foo != null)
        {
            double[] bar = new double[ViewBag.Foo.Length];
        }
    }
    

    The sort of problem you had is exactly why this is the case. Your problem came down to the fact that bar was not correctly scoped. If instead this work was happing within a ViewModel, a similar mistake would have immediately given you a compiler error. Instead you don’t find out until you compile and use the application, and the error you are given can often be cryptic and difficult to track down.

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

Sidebar

Related Questions

I have some automatic de-serialization code that will set an object's properties using KVC
I have some C# / asp.net code I inherited which has a textbox which
I have some javascript code that is used in a few different pages in
I have a section of Razor code in one of my views that isn't
Using the razor view engine, I have some code like the following: @Html.TextAreaFor(model =>
I have some MVC3 Razor code. I am not so familiar with Razor but
Using MVC3, C#, and the Razor View Engine: I have a form that has
I have some UI in VB 2005 that looks great in XP Style, but
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
We have some input data that sometimes appears with &nbsp characters on the end.

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.