tags:

views:

41

answers:

1

hi, i am having a strange problem, i have a static class like this

  public static class BlogDataAccess
{
    private static Blog _Blg;
    public static Blog Blg
    {
        get
        {
           _Blg = new Blog ();
           return _Blog ;
        }
    }

}

then in my page i do the following

  var DataContext= new DataClasses();
  BlogDataAccess.Blg.ArticleTitle ="Title";
  DataContext.Blog.InsertOnSubmit(BlogDataAccess.Blg);
  DataContext.SubmitChanges();

the record get inserted but with null value of the ArticleTitle field thanks in advanced.

+4  A: 

Each time you're accessing BlogDataAccess.Blg, you're creating a new object. I think you mean to implement the lazy-instantiation like this instead:

public static class BlogDataAccess
{
    private static Blog _Blg;
    public static Blog Blg
    {
        get
        {
            if(_Blg == null)
                _Blg = new Blog();

            return _Blg;
        }
    }

}
lc
wow, just wow man you have saved me hours, it worked like charm thanks a lot really :) is there any down side of this technique ?
Well, it assumes you'll need one and only one instance of the object available. The only down-side of lazy instantiation is if the object's construction takes a long time. You might see some lag the first time it is accessed.
lc
so as you saw it is a blog module and maybe 3 users are working on adding, editing article, so will this make a problem ?
No, no, should be fine. Unless of course your Blog() constructor tries to read the whole thing from the database into memory or something.
lc
no man, it is as i told you , i will give user control panel to manage blog entries, and then will use the db to show the articles in the front end.thanks again really :)