?
Solved

Public String to Public Constant ASP .Net 4.0

Posted on 2012-03-16
10
Medium Priority
?
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37731673
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
ID: 37732382
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
ID: 37732806
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37733129
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
ID: 37733397
I tried both and had this error:

     Operator '+' cannot be applied to operands of type 'string' and 'method group'
0
 

Accepted Solution

by:
atljarman earned 0 total points
ID: 37733653
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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 800 total points
ID: 37733863
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
ID: 37734497
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
ID: 37756249
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
ID: 40551479
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Progress

764 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