Public String to Public Constant ASP .Net 4.0

Hi,

I have a public const that I use in a .master.cs file.   I use this file in several applications, and would like to set the value based on the site.  I would like to add text to the const based on that site, but have been successful in doing something like this:

What I would like the value that I reference in the CS code to provide the value for the variable WEBSITE as AAAA BBBB CCCC not AAAA BBBB as current.

I've tried to concatenate the const WEBSITE and the public string H1First values with no success.  I have no idea how to resolve this.  Any thoughts/suggestions?


//Here is a sample of the public string:
 public string H1first()
    {
        if (pS.Contains("/CCCC"))
        {
            return "CCCC";
        }
        else
        {
            return "";
        }

    }

//Here is how the const is currently defined:

    //Name of the Website
    const string WEBSITE = "AAAA  BBBB";



//Here is a reference to where I use the const value

        if (SiteMap.Provider.CurrentNode != null)
        {

            if (SiteMap.Provider.CurrentNode.ResourceKey == "parent")
            {

                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl anchor = new HtmlGenericControl("a");


                if (SiteMap.Provider.CurrentNode.ParentNode.Title != "Home")
                {
                    li = new HtmlGenericControl("li");
                    navUL.Controls.Add(li);

                    anchor = new HtmlGenericControl("a");
                    anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.ParentNode.Url.Replace("/default.aspx", "/"));

                    if ((SiteMap.Provider.CurrentNode.ParentNode.Title != "Home") & (SiteMap.Provider.CurrentNode.ParentNode.Title != ""))
                    {
                        anchor.InnerText = SiteMap.Provider.CurrentNode.ParentNode.Title.Replace("Home", WEBSITE + " Home");
                    }
                    else
                    {
                        anchor.InnerText = WEBSITE + " Home";
                    }


                    li.Controls.Add(anchor);
                }

        //                            if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       

                li = new HtmlGenericControl("li");
                navUL.Controls.Add(li);

                anchor = new HtmlGenericControl("a");
                anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.Url.Replace("/default.aspx", "/"));
                anchor.InnerText = SiteMap.Provider.CurrentNode.Title.Replace("Home", WEBSITE + " Home"); //not here

                anchor.Attributes.Add("class", "current");

                li.Controls.Add(anchor);

                foreach (SiteMapNode node in SiteMap.Provider.CurrentNode.ChildNodes)
                {
                    //if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node["DisplayInSiteMap"] != null && node["DisplayInSiteMap"].Equals("false", StringComparison.CurrentCultureIgnoreCase)))
                    if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       
                    //if (node.ResourceKey != "hide")
                    {
                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", node.Url.Replace("/default.aspx", "/"));
                        //anchor.InnerText = node.Title;
                        anchor.InnerText = node.Title.Replace("Home", WEBSITE + " Home");

                        if (node.Title == DateTime.Today.ToString("MMMMM"))
                            anchor.Attributes.Add("class", "current");

                        li.Controls.Add(anchor);
                    }
                }
            }

            else
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl anchor = new HtmlGenericControl("a");

                if ((SiteMap.Provider.CurrentNode.ParentNode.Url != "") & (SiteMap.Provider.CurrentNode.ParentNode.Url != "~/default.aspx"))
                {
                    navUL.Controls.Add(li);

                    anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.ParentNode.Url.Replace("/default.aspx", "/"));
                    anchor.InnerText = SiteMap.Provider.CurrentNode.ParentNode.Title.Replace("Home", WEBSITE + " Home");

                    li.Controls.Add(anchor);
                }

                foreach (SiteMapNode node in SiteMap.Provider.CurrentNode.ParentNode.ChildNodes)
                {


                    if (node.Title == "Home")
                    {

                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", "../default.aspx");
                        anchor.InnerText = "Advisory Committees & Groups";

                        li.Controls.Add(anchor);
                    }


                    //if (node.ResourceKey != "hide")
                    if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       
                    {
                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", node.Url.Replace("/default.aspx", "/"));
                        //anchor.InnerText = node.Title;
                        anchor.InnerText = node.Title.Replace("Home", WEBSITE + " Home").Replace("For Members", WEBSITE + " Members Only");//recent change

                        if (node.Url == SiteMap.Provider.CurrentNode.Url)
                            anchor.Attributes.Add("class", "current");

                        li.Controls.Add(anchor);
                    }
                }
            }



        }
        else
        {

            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl anchor = new HtmlGenericControl("a");

            li = new HtmlGenericControl("li");
            navUL.Controls.Add(li);

            anchor = new HtmlGenericControl("a");
            anchor.Attributes.Add("href", "default.aspx");
            //anchor.InnerText = "Home";
            anchor.InnerText = WEBSITE + " Home";

            //anchor.Attributes.Add("class", "current");

            li.Controls.Add(anchor);
        }
atljarmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
The value you assign to a const field must be a literal value--and it cannot change once set. If you want something with a bit more flexibility (in that you aren't restricted to just literal values), then use a readonly field. The value you assign to a readonly can be variable, and it must be set from within a constructor (or by directly initializing the field), but once set, it cannot be assigned to again.

Take note:  only the reference is readonly. If you were to use something like a List, the reference to the List would be readonly (meaning you could never say "ref = new List()" once you leave the constructor), but the list could be freely added to or removed from.
0
atljarmanAuthor Commented:
This sounds promisimg. Can you provide an example using the WEBSITE and H1first variables?  I can modify and test out.  I am new to c# coding for asp .mt and never have used a read only field.
0
atljarmanAuthor Commented:
I won't have access to my computer til later today.  Would something like this work?

class SiteVariables
{
    //private static _myVariable = 0;
    public static string WEBSITE
    {
        //get { return _myVariable; }
        get { return "AAAA " + SiteMaster.H1first; }
        set { "AAAA " + SiteMaster.H1first = value; }
    }
}

Then access it in the the value instead of WEBSITE it would be SiteVariables.WEBSITE.

I'm really not sure if this would work, but I really need a functioning example to work from.  If this is close, could you modify and re-attach?

I've tried looking for examples on readonly, and only am finding int examples and none that cocantenate a string value "AAAA " with a public string like the H1first variable.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

käµfm³d 👽Commented:
I suppose you could use just a "getter". I was thinking more along the lines of:

public static readonly string WEBSITE = "AAAA " + SiteMaster.H1first;

Open in new window


If you use the get/set approach, I'd suggest marking the set as private:

class SiteVariables
{
    //private static _myVariable = 0;
    public static string WEBSITE
    {
        //get { return _myVariable; }
        get { return "AAAA " + SiteMaster.H1first; }
        private set { "AAAA " + SiteMaster.H1first = value; }
    }
}

Open in new window

0
atljarmanAuthor Commented:
I tried both and had this error:

     Operator '+' cannot be applied to operands of type 'string' and 'method group'
0
atljarmanAuthor Commented:
I've solve the problem another way, thanks for the attempt.  I could not get your solution to work for some reason.


    private string pS = HttpContext.Current.Request.ApplicationPath.ToLower();


    public string WEBSITE()
    {

        if (pS.Contains("/aaaa"))
        {
            return "AAAA";
        }
        else
        {
            return "";
        }

    }






//Here is a reference to where I use the const value

        if (SiteMap.Provider.CurrentNode != null)
        {

            if (SiteMap.Provider.CurrentNode.ResourceKey == "parent")
            {

                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl anchor = new HtmlGenericControl("a");


                if (SiteMap.Provider.CurrentNode.ParentNode.Title != "Home")
                {
                    li = new HtmlGenericControl("li");
                    navUL.Controls.Add(li);

                    anchor = new HtmlGenericControl("a");
                    anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.ParentNode.Url.Replace("/default.aspx", "/"));

                    if ((SiteMap.Provider.CurrentNode.ParentNode.Title != "Home") & (SiteMap.Provider.CurrentNode.ParentNode.Title != ""))
                    {
                        anchor.InnerText = SiteMap.Provider.CurrentNode.ParentNode.Title.Replace("Home", WEBSITE() + " Home");
                    }
                    else
                    {
                        anchor.InnerText = WEBSITE() + " Home";
                    }


                    li.Controls.Add(anchor);
                }

        //                            if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       

                li = new HtmlGenericControl("li");
                navUL.Controls.Add(li);

                anchor = new HtmlGenericControl("a");
                anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.Url.Replace("/default.aspx", "/"));
                anchor.InnerText = SiteMap.Provider.CurrentNode.Title.Replace("Home", WEBSITE() + " Home"); //not here

                anchor.Attributes.Add("class", "current");

                li.Controls.Add(anchor);

                foreach (SiteMapNode node in SiteMap.Provider.CurrentNode.ChildNodes)
                {
                    //if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node["DisplayInSiteMap"] != null && node["DisplayInSiteMap"].Equals("false", StringComparison.CurrentCultureIgnoreCase)))
                    if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       
                    //if (node.ResourceKey != "hide")
                    {
                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", node.Url.Replace("/default.aspx", "/"));
                        //anchor.InnerText = node.Title;
                        anchor.InnerText = node.Title.Replace("Home", WEBSITE() + " Home");

                        if (node.Title == DateTime.Today.ToString("MMMMM"))
                            anchor.Attributes.Add("class", "current");

                        li.Controls.Add(anchor);
                    }
                }
            }

            else
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl anchor = new HtmlGenericControl("a");

                if ((SiteMap.Provider.CurrentNode.ParentNode.Url != "") & (SiteMap.Provider.CurrentNode.ParentNode.Url != "~/default.aspx"))
                {
                    navUL.Controls.Add(li);

                    anchor.Attributes.Add("href", SiteMap.Provider.CurrentNode.ParentNode.Url.Replace("/default.aspx", "/"));
                    anchor.InnerText = SiteMap.Provider.CurrentNode.ParentNode.Title.Replace("Home", WEBSITE() + " Home");

                    li.Controls.Add(anchor);
                }

                foreach (SiteMapNode node in SiteMap.Provider.CurrentNode.ParentNode.ChildNodes)
                {


                    if (node.Title == "Home")
                    {

                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", "../default.aspx");
                        anchor.InnerText = "Advisory Committees & Groups";

                        li.Controls.Add(anchor);
                    }


                    //if (node.ResourceKey != "hide")
                    if ((!(HttpContext.Current.User.Identity.IsAuthenticated) && node["members"] != null && node["members"].Equals("true", StringComparison.CurrentCultureIgnoreCase)) | (node.ResourceKey != "hide"))
       
                    {
                        li = new HtmlGenericControl("li");
                        navUL.Controls.Add(li);

                        anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", node.Url.Replace("/default.aspx", "/"));
                        //anchor.InnerText = node.Title;
                        anchor.InnerText = node.Title.Replace("Home", WEBSITE() + " Home").Replace("For Members", WEBSITE() + " Members Only");//recent change

                        if (node.Url == SiteMap.Provider.CurrentNode.Url)
                            anchor.Attributes.Add("class", "current");

                        li.Controls.Add(anchor);
                    }
                }
            }



        }
        else
        {

            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl anchor = new HtmlGenericControl("a");

            li = new HtmlGenericControl("li");
            navUL.Controls.Add(li);

            anchor = new HtmlGenericControl("a");
            anchor.Attributes.Add("href", "default.aspx");
            //anchor.InnerText = "Home";
            anchor.InnerText = WEBSITE() + " Home";

            //anchor.Attributes.Add("class", "current");

            li.Controls.Add(anchor);
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
I tried both and had this error:

     Operator '+' cannot be applied to operands of type 'string' and 'method group'
I didn't look at the definition of H1first, so I didn't realize it was a function. You would just need to add parentheses so that the compiler realizes you want to call the function:

public static readonly string WEBSITE = "AAAA " + SiteMaster.H1first();

Open in new window

0
atljarmanAuthor Commented:
I didn't look at the definition of H1first, so I didn't realize it was a function. You would just need to add parentheses so that the compiler realizes you want to call the function:

I tried that too, and it didn't work. I'm really not sure why your solution didn't work in my code.  I'm going to award you some points, as your solution might work for someone else, but it didn't work for me. Thanks again for working through this problem.
0
atljarmanAuthor Commented:
I really appreciate kaufmed's assistance on this issue. His solution might work for someone else, but I took a different approach as I couldn't get it to work in my application.
0
atljarmanAuthor Commented:
Funny enough... I'm searching for a way to add a class to the main UL that is generated and this post comes up.  I'm not sure if there is a way to do it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.