embryo
embryo

Reputation: 157

error CS0103: The name ' ' does not exist in the current context

When my view loads, I need to check which domain the user is visiting, and based on the result, reference a different stylesheet and image source for the logo that appears on the page.

This is my code:

@{
    string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

    if (currentstore == "www.mydomain.com")
    {
        <link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
        string imgsrc="/content/images/uploaded/store1_logo.jpg";
    }
    else
    {
        <link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
        string imgsrc="/content/images/uploaded/store2_logo.gif";
    }
}

Then, a little further down I call the imgsrc variable like this:

<a href="@Url.RouteUrl("HomePage")" class="logo"><img  alt="" src="@imgsrc"></a>

I get an error saying:

error CS0103: The name 'imgsrc' does not exist in the current context

I suppose this is because the "imgsrc" variable is defined in a code block which is now closed...?

What is the proper way to reference this variable further down the page?

Upvotes: 12

Views: 244413

Answers (2)

Никита
Никита

Reputation: 1

using System;
using System.Collections.Generic;                    (помогите пожалуйста та же самая
using System.Linq;                                     ошибка PlayerScript.health = 
using System.Text;                                      999999; вот на этот скрипт)                                  
using System.Threading.Tasks;
using UnityEngine;

namespace OneHack
{
    public class One
    {
        public Rect RT_MainMenu = new Rect(0f, 100f, 120f, 100f); //Rect это месторасположение меню по x,y и высота, ширина.
        public int ID_RTMainMenu = 1;
        private bool MainMenu = true;
        private void Menu_MainMenu(int id) //Главное меню
        {
            if (GUILayout.Button("Название вашей кнопки", new GUILayoutOption[0]))
            {
                if (GUILayout.Button("Бессмертие", new GUILayoutOption[0]))
                {
                    PlayerScript.health = 999999;//При нажатии на кнопку у игрока устанавливается здоровье 999999  //Здесь код, который будет происходить при нажатии на эту кнопку
                }
            }
        }
        private void OnGUI()
        {
            if (this.MainMenu)
            {
                this.RT_MainMenu = GUILayout.Window(this.ID_RTMainMenu, this.RT_MainMenu, new GUI.WindowFunction(this.Menu_MainMenu), "MainMenu", new GUILayoutOption[0]);
            }
        }
        private void Update() //Постоянно обновляемый метод, все что здесь будет написанно будет создаваться бесконечно
        {
            if (Input.GetKeyDown(KeyCode.Insert)) //Кнопка на которую будет открываться и закрываться меню, можно поставить другую
            {
                this.MainMenu = !this.MainMenu;
            }
        }
    }
}

Upvotes: -3

mason
mason

Reputation: 32694

Simply move the declaration outside of the if block.

@{
string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string imgsrc="";
if (currentstore == "www.mydomain.com")
    {
    <link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
    imgsrc="/content/images/uploaded/store1_logo.jpg";
    }
else
    {
    <link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
    imgsrc="/content/images/uploaded/store2_logo.gif";
    }
}

<a href="@Url.RouteUrl("HomePage")" class="logo"><img  alt="" src="@imgsrc"></a>

You could make it a bit cleaner.

@{
string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string imgsrc="/content/images/uploaded/store2_logo.gif";
if (currentstore == "www.mydomain.com")
    {
    <link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
    imgsrc="/content/images/uploaded/store1_logo.jpg";
    }
else
    {
    <link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
    }
}

Upvotes: 13

Related Questions