Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Saturday, March 31, 2012

Set application variable from a class

Hi
I am trying to set an application variable from a class file in my website.
Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.
However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object
How can i set an application variable from a class
It works fine from a webform
TIA
GrantGot It
HttpContext.Current.Application[ ...
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant
>

Set application variable from a class

Hi

I am trying to set an application variable from a class file in my website.

Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.

However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object

How can i set an application variable from a class
It works fine from a webform

TIA

GrantGot It

HttpContext.Current.Application[ ...

"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant

Thursday, March 29, 2012

Set Class Instance to Nothing During Instantiation?

I'm creating a class (using VB.NET) that must read data from a
database table during instantiation. If the data cannot be read
(connection problem, etc.), I want the instantiation to fail such that
the following "If" statement will evaluate to True:

Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...

What code in the class's constructor (Sub New) would make that happen?You can't. You're constructor can throw an exception...which how you
should do it anyways. if that isn't satisfactory, you can wrap your
constructor call in a static property of your class:

dim objGribble as cGribble = cGribble.CreateInstance()
if objGriggle is nothing then..

where CreateInstance() looks something like:

Public shared Property CreateInstance() as CGribble
get
try
return new cGribble()
catch ex as YourCustomException 'for the love of god don't
swallow this exception
return null
end try
end get
end property

karl

"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.c om...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Or, here's another method. Do a Web Search on "Factory Pattern"

You can implement it kinda like this

Public Class MyClass
Private Sub New()
' You can not directly instantiate this class
End Sub

Public Shared Function GetInstance(param1 as string, param2 as integer,
...) as MyClass
Dim result as new MyClass 'Since its private, the constructor can be
called inside the class
''' Code to possibly load from database
If CanLoadFromDB then
Return result
Else
Return Nothing
End If
End Function

End Class

Instead of :
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...

You'll end up with:
Dim objGribble as cGribble = cGribble.GetInstance()
If objGribble Is Nothing Then ...

Now this will work. If you're totally set about this type of approach, then
go for it, dude! However, if you use the approach that Carl mentioned, you
do get one major benefit. All you know from this approach is that the class
can not be created. Was it because of a database error? Was it because of
a division Error? What about Solar Flares? If you throw an exception
inside of the constructor, you can catch it and inform the user about the
error, as it was thrown, as opposed to the assumption that it was error "X".

"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.c om...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Thanks, Karl and David. These are very interesting approaches, and
they're definitely going into my snippet file! I'm not sure yet which
I'll use for this current project, but I imagine I'll be using them
both in different situations in the future.

Set Class Instance to Nothing During Instantiation?

I'm creating a class (using VB.NET) that must read data from a
database table during instantiation. If the data cannot be read
(connection problem, etc.), I want the instantiation to fail such that
the following "If" statement will evaluate to True:
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
What code in the class's constructor (Sub New) would make that happen?You can't. You're constructor can throw an exception...which how you
should do it anyways. if that isn't satisfactory, you can wrap your
constructor call in a static property of your class:
dim objGribble as cGribble = cGribble.CreateInstance()
if objGriggle is nothing then..
where CreateInstance() looks something like:
Public shared Property CreateInstance() as CGribble
get
try
return new cGribble()
catch ex as YourCustomException 'for the love of god don't
swallow this exception
return null
end try
end get
end property
karl
"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.com...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Or, here's another method. Do a Web Search on "Factory Pattern"
You can implement it kinda like this
Public Class MyClass
Private Sub New()
' You can not directly instantiate this class
End Sub
Public Shared Function GetInstance(param1 as string, param2 as integer,
...) as MyClass
Dim result as new MyClass 'Since its private, the constructor can be
called inside the class
''' Code to possibly load from database
If CanLoadFromDB then
Return result
Else
Return Nothing
End If
End Function
End Class
Instead of :
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
You'll end up with:
Dim objGribble as cGribble = cGribble.GetInstance()
If objGribble Is Nothing Then ...
Now this will work. If you're totally set about this type of approach, then
go for it, dude! However, if you use the approach that Carl mentioned, you
do get one major benefit. All you know from this approach is that the class
can not be created. Was it because of a database error? Was it because of
a division Error? What about Solar Flares? If you throw an exception
inside of the constructor, you can catch it and inform the user about the
error, as it was thrown, as opposed to the assumption that it was error "X".
"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.com...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Thanks, Karl and David. These are very interesting approaches, and
they're definitely going into my snippet file! I'm not sure yet which
I'll use for this current project, but I imagine I'll be using them
both in different situations in the future.

Set css class on list box option

Hi,

I want to programatically set the css class on a single list item. I tried the following code but it does nothing...


<html>
<script language="VB" runat="server"
Sub Page_Load(Src As Object, E As EventArgs)
categories.Items(0).Attributes.Add("class", "option.primary")
End Sub

</script
<style>
.primary {background-color:green;color:red; }
</style
<body>
<form runat="server">
<asp:listbox id="categories" Runat="server" SelectionMode="Multiple" Rows="6" Width="400">
<asp:listItem>Item 1</asp:listItem>
</asp:listbox>
</form>
</body>
</html>

Does anyone know how to achieve this?

WT.use the "cssclass" property of the listbox, then you can set it without doing the attributes.add thing.
<option> elements do not have class attributes, but it *is* strange that it doesn't write it out anyway. If you look at the rendered HTML, it probably reads:

<select name="categories">
<option value="Item 1">Item 1</option
That is because, as said, option elements don't have class attributes. Additionally, you're specifying "option.primary" where really the css class you want to use is "primary". If you want to add the class to the whole categories listbox, you could. And an easier way is to use this syntax:


categories.CssClass = "primary"

Which will work just the same as:


categories.Attributes.Add("class","primary")

Thanks for the replies.

I presume that the listItems not rendering their attributes must be a bug.

Looking at the source for the ListBox control, it doesn't appear to be rendering the attributes of its list items. Thus, I'll override the 'RenderContents' method or something and get the attributes rendered.
I think it is a bug too,

but the class browser indicated that the attributes property of the ListItem is readonly which would explain the attributes not being added to the listitem... maybe it is by design.

Set culture for a DLL (class library) referenced from web application

I am developing a DLL (class library) for a web application of mine.

In the web.config of the web application I have set: culture="en-GB"
uiCulture="en" in order that the date format will be dd/mm/yyyy.

I also changed the regional settings to that date format.

When I am using the Now() function in the DLL I get the date format in
mm/dd/yyyy instead. Why the date format in the DLL is different? Shouldn't
it take the culture settings from the web application he is referenced from?

How can I control the date format of the DLL (class library)?

Thanks for your time

ra294@dotnet.itags.org.hotmail.comYa Ya,

See this article on settig culture.

http://www.dotnetjunkies.com/Tutori...33D2EDD38B.dcik

I don't believe you are setting the culture on the thread and the
application assumes the culture of the server.

HTH MikeL.

"Ya Ya" wrote:

> I am developing a DLL (class library) for a web application of mine.
> In the web.config of the web application I have set: culture="en-GB"
> uiCulture="en" in order that the date format will be dd/mm/yyyy.
> I also changed the regional settings to that date format.
> When I am using the Now() function in the DLL I get the date format in
> mm/dd/yyyy instead. Why the date format in the DLL is different? Shouldn't
> it take the culture settings from the web application he is referenced from?
> How can I control the date format of the DLL (class library)?
>
> Thanks for your time
> ra294@.hotmail.com
>

Set Custom Page properties declaratively

Hi,
I have a base class (which inherits from System.Web.UI.Page) for all the
pages in my application. I have a property defined on this class that I wan
t
to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
Is there any way to do this declaratively? When I try to use the <@dotnet.itags.org. Page
...> directive, it only allows the predefined attributes to be used.
Thanks,
Richard Brown<script language="CSharp" runat="server">
// put your code in here
</script>
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a horse to water,
but you can't make him think.
"Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
> Hi,
> I have a base class (which inherits from System.Web.UI.Page) for all the
> pages in my application. I have a property defined on this class that I
> want
> to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
> Is there any way to do this declaratively? When I try to use the <@. Page
> ...> directive, it only allows the predefined attributes to be used.
> Thanks,
> Richard Brown
>
Thanks for replying so quickly. Unfortunately the example you have provided
does not meet my requirements on two counts:
1. This is not declarative. This is code, and I would add this to the
code-behind class.
2. The <script> tags allow the client to define functions, but not run code
.
If the code requires to be run it needs the old-style <% %> breakout.
However this code is then run during the rendering of the page, which is
invoked from the .Net Framework (i.e,. the client does not have control over
when this code is executed).
I was wondering if there is a way of declaratively setting the properties of
the page class before the OnInit (in much the same way that properties are
set in a server control).
Thanks,
Richard Brown
"Kevin Spencer" wrote:

> <script language="CSharp" runat="server">
> // put your code in here
> </script>
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> You can lead a horse to water,
> but you can't make him think.
> "Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
> news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
>
>

Monday, March 26, 2012

Set Default value for a Property in a user defined class

Hello,
I have created a class in vb .net and would like to add a property tothis class with a default value. The purpose of this would bethat if the user didn't supply a value for the property (or evenreference the property) it would contain the default value. Forexample here is what I have tried:
Public Shared MultipleResults As Boolean = False
My pages don't work properly as the property doesn't seem to be set to False.
Thanks in advance,
Paul Liadis
Penn State University
What do you mean by "pages don't work properly"? Do they crash or are you just not getting the results you expect?
An uninitialized Boolean will return False (which is ambiguous, of course). For example:
Class Test
Public Shared MultipleResults As Boolean = False
Public Shared MultipleResultsUnInit As Boolean
End Class
Sub Button1_Click(sender As Object, e As EventArgs)
Dim t As New Test
Response.Write("<br>Value = " & t.MultipleResults)
Response.Write("<br>Value = " & t.MultipleResultsUnInit)
End Sub

When I run this, I get False for both properties.
Are you perhaps running into the issue that the property value is not being persisted across postbacks?


Sorry about the vague statement "doesn't work". The pages do notcrash. They give results that I did not expect. Here is avery small version of the example you gave, with slight midifications:
Class Test
Public Shared globalCustomColumnWidths As Boolean = False
' many other subroutines and such here
shared sub ASubroutine()
If globalCustomColumnWidths = False Then
TableCellObj.Width = Unit.Percentage(25)
Else
TableCellObj.Width = Unit.Percentage(globalWidthArray(0))
End If
end sub
End Class
What I would like to happen is that when I call my test class, I would like for globalCustomerColumnWidths to
evaluate to false, even if I don't set it to anything. This doesn't seem to be happening. Any thoughts? Also,
what did you mean by "An uninitialized Boolean will return False (which is ambiguous, of course)."?
Thanks for the response.



Sorry, I'm not understanding the relationship between globalCustomColumnWidths and MultipleResults. (?)
>"ambiguous, of course"
That if an uninitialized Boolean returns false, you don't know whether it's uninitialized or has been explicitly set to false.

Set DIV text in code behind

if i want to set the text inside a div (or other properties like "class") from my code behind, as what to i declare it? htmlgenericcontrol? If so, what attribute do i set.

html:
<div id="divtest" runat=server></div>
code behind:
protected System.Web.UI.HtmlControls.WHAT?? divtest;
...
...
divtest.WHAT?? = "seems to work";Html part is fine. After you've created it, look at it in the design view, save the page, then go to the codebehind, you should be able to see an html div control (some name)... set its innerText property.
hmmm...not so sure

I always have to code the "protected htmlcontrol whatever mycontrol" myself in my project. VS never do it for me. SOMETIMES, VS will add this for me for asp.net controls though.

Maybe something to do with the fact I ALWAYS hand code my pages...never drag and drop onto form...

...aaah...stupid me..let me drag some html controls onto the form and see what VS do for me... sorry..always talk to myself huh?
It's a VS 2003 bug that I discovered much to my consternation when VS 2003 had just come out. Surprisingly, it was addressed by nobody. Everyone thought it was a feature rather than a bug, I even asked MS developers about it, and they did fix it in VS 2005 but they mentioned that very few people ever brought it up regarding VS 2003.

The workaround was that you have to save the ASPX page before going to the codebehind so that the control's code was added to the codebehind file.

Tuesday, March 13, 2012

Set property default value

Hello,

I am creating a class where I have various properties.
How to I set a default property value in case the property is not defined by the user.

For example, I have the property:

' Priority
Public Property Priority() As Mail.MailPriority
Get
Return _Priority
End Get
Set(ByVal value As Mail.MailPriority)
_Priority = value
End Set
End Property

I want to set it to Mail.MailPriority.Normal in case the user didn't defined it.

How can I do this?

Thanks,
Miguel

You need to use [DefaultValue(datatype)] for your property definition

For example check thisSet Property Default Value

Thanks


I haven't verified/tested but is the [DefaultValue(datatype)] only used by the Property Browser control ?

An other option is the set your variable in your constructor or in the declaration (Dim a As Integer = 1)


Replace

private _Priority As String

with

private _Priority As String = "Normal"