Solved

Public String to Public Constant ASP .Net 4.0

Posted on 2012-03-16
10
667 Views
Last Modified: 2015-01-15
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);
        }
0
Comment
Question by:atljarman
  • 7
  • 3
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 

Author Comment

by:atljarman
Comment Utility
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
 

Author Comment

by:atljarman
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 

Author Comment

by:atljarman
Comment Utility
I tried both and had this error:

     Operator '+' cannot be applied to operands of type 'string' and 'method group'
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Accepted Solution

by:
atljarman earned 0 total points
Comment Utility
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
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
Comment Utility
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
 

Author Comment

by:atljarman
Comment Utility
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
 

Author Closing Comment

by:atljarman
Comment Utility
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
 

Author Comment

by:atljarman
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 48
Handle null when using linq in this line 1 20
Performance  Html.BeginForm vs jQuery 3 4
XML & .net 5 14
This article will show, step by step, how to integrate R code into a R Sweave document
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now