Showing posts with label creating. Show all posts
Showing posts with label creating. Show all posts

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.

Monday, March 26, 2012

Set default Tag Prefix and CSS?

Hello,
I am creating a web server control derived from
datagrid for the first time. Now I would like to set the
prefix of my control by default to SK instead of CC1. Is
that possible? The other question is that my datagrid
should use a default stylesheet. I have set the cssclass
property of all items which I want to change. BUT I am not
setting the link tag in the head section to my css. I dont
want to do that. I would like to do that direclty in my
component. Is that possible?
ThanksAlternatingItemStyle
ItemStyle
HeaderStyle
footerstyle
if u set css for these four element, css will apply for u're componet. plz
see blow , I explained how we hve to use sample class...
<asp:datagrid id="dgActivity" runat="server" AllowPaging="True"
Class="DataGridStyle" Width="100%"
CellPadding="2" PageSize="30" AutoGenerateColumns="False" AllowSorting="True
">
<AlternatingItemStyle
CssClass="DataGridAlterItemStyle"></AlternatingItemStyle>
<ItemStyle CssClass="DataGridItemStyle"></ItemStyle>
<HeaderStyle CssClass="DataGridHeaderStyle"></HeaderStyle>
.DataGridStyle
{
border-color : White;
border-style : solid;
}
.DataGridStyle td
{
border-color : White;
border-style : solid;
}
.DataGridHeaderStyle
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:link
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:hover
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:visited
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
"SK" wrote:

> Hello,
> I am creating a web server control derived from
> datagrid for the first time. Now I would like to set the
> prefix of my control by default to SK instead of CC1. Is
> that possible? The other question is that my datagrid
> should use a default stylesheet. I have set the cssclass
> property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont
> want to do that. I would like to do that direclty in my
> component. Is that possible?
> Thanks
>
Hello SK,
Look at the [assembly:TagPrefix] attribute. You'll need to reference System.Web.UI,
if you're not already.
[assembly:TagPrefix("YourNamespace", "YourPrefix")]
Matt Berther
http://www.mattberther.com

> Hello,
> I am creating a web server control derived from datagrid for the
> first time. Now I would like to set the prefix of my control by
> default to SK instead of CC1. Is that possible? The other question is
> that my datagrid should use a default stylesheet. I have set the
> cssclass property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont want to do
> that. I would like to do that direclty in my component. Is that
> possible?
> Thanks
>

Set default Tag Prefix and CSS?

Hello,

I am creating a web server control derived from
datagrid for the first time. Now I would like to set the
prefix of my control by default to SK instead of CC1. Is
that possible? The other question is that my datagrid
should use a default stylesheet. I have set the cssclass
property of all items which I want to change. BUT I am not
setting the link tag in the head section to my css. I dont
want to do that. I would like to do that direclty in my
component. Is that possible?

ThanksAlternatingItemStyle
ItemStyle
HeaderStyle
footerstyle

if u set css for these four element, css will apply for u're componet. plz
see blow , I explained how we hve to use sample class...
<asp:datagrid id="dgActivity" runat="server" AllowPaging="True"
Class="DataGridStyle" Width="100%"
CellPadding="2" PageSize="30" AutoGenerateColumns="False" AllowSorting="True">
<AlternatingItemStyle
CssClass="DataGridAlterItemStyle"></AlternatingItemStyle>
<ItemStyle CssClass="DataGridItemStyle"></ItemStyle>
<HeaderStyle CssClass="DataGridHeaderStyle"></HeaderStyle
..DataGridStyle
{
border-color : White;
border-style : solid;
}
..DataGridStyle td
{
border-color : White;
border-style : solid;
}
..DataGridHeaderStyle
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";

}
..DataGridHeaderStyle a
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:link
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:hover
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:visited
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}

"SK" wrote:

> Hello,
> I am creating a web server control derived from
> datagrid for the first time. Now I would like to set the
> prefix of my control by default to SK instead of CC1. Is
> that possible? The other question is that my datagrid
> should use a default stylesheet. I have set the cssclass
> property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont
> want to do that. I would like to do that direclty in my
> component. Is that possible?
> Thanks
Hello SK,

Look at the [assembly:TagPrefix] attribute. You'll need to reference System.Web.UI,
if you're not already.

[assembly:TagPrefix("YourNamespace", "YourPrefix")]

--
Matt Berther
http://www.mattberther.com

> Hello,
> I am creating a web server control derived from datagrid for the
> first time. Now I would like to set the prefix of my control by
> default to SK instead of CC1. Is that possible? The other question is
> that my datagrid should use a default stylesheet. I have set the
> cssclass property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont want to do
> that. I would like to do that direclty in my component. Is that
> possible?
> Thanks

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"