Showing posts with label default. Show all posts
Showing posts with label default. Show all posts

Saturday, March 24, 2012

Stripping Needless Data From HttpContext.Current.Request.Form

I found this nice example of printing data to the default printer of the server when the submit button is clicked. But not sure how to strip out the names of the controls, etc.

Here is the code for those interested:

1Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)23 Dim yPos As Single = 2504 Dim leftMargin As Single = ev.MarginBounds.Left5 Dim topMargin As Single = ev.MarginBounds.Top6 Dim printFont = New Font("Arial", 10)78 Dim sb As StringBuilder = New StringBuilder()910 ' Page title and date/time.11 sb.Append("Maintenance Request")12 sb.Append(Environment.NewLine)13 sb.Append("DateTime: " + DateTime.Now.ToString() + Environment.NewLine)1415 ' Iterate submitted form fields and get field names.16 Dim fieldValue As String17 Dim fieldName As String1819 ' Exclude viewstate and submit button.20 For Each fieldName In HttpContext.Current.Request.Form21 Response.Write("Field Name: " & HttpContext.Current.Request.Form(fieldName) & "<br>")22 If fieldName = "__VIEWSTATE" Or fieldName = "Submit" Then2324 Else25 ' Get the field values.2627 fieldValue = HttpContext.Current.Request.Form(fieldName)2829 ' Add the field names and values to the page.30 ' Break the field values into 50 character segments so it will fit on the paper.31 ' Currently, this only accounts for fields of l50 characters or less.32 ' ISSUE: breaks in the middle of words instead of spaces3334 If fieldValue.Length > 100 Then35 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)36 sb.Append(" " + fieldValue.Substring(50, 50) + Environment.NewLine)37 sb.Append(" " + fieldValue.Substring(100, fieldValue.Length - 100) + Environment.NewLine)3839 ElseIf fieldValue.Length > 50 Then40 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)41 sb.Append(" " + fieldValue.Substring(50, fieldValue.Length - 50) + Environment.NewLine)42 Else43 sb.Append(fieldName + ": " + fieldValue + Environment.NewLine)4445 End If4647 End If4849 Next50 ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, yPos, New StringFormat())5152 End Sub
 
There has to be a better way. Any ideas?
Figured it out. Instead of using HttpContext.Current.Request.Form, I am just using the explicit names of the controls and appending the values to the stringbuilder. If anyone needs help with this, please feel free to contact me.Big Smile

Thursday, March 22, 2012

Struct Default Constructor Question

When I create a new object of type struct, I want objects to be created for each member variable in that struct. For example:

private struct Strings {
public string s1;
public string s2;
}

Strings myStrings = new Strings();

Based on what I read, the default constructor for the struct should automatically initalize s1 and s2 to string.Empty implicitly. However, doing something like int size = myStrings.s1.Length throws a NullReferenceException. I don't understand why myStrings.s1 does not exist. Aren't s1 and s2 created automatically when I create a new object called myStrings??

Hello anonim:

> Based on what I read, the default constructor for the struct should automatically initalize s1 and s2 to string.Empty implicitly.

I'm afraid this is not correct. There's no implicit initialization of objects anywhere and your strings won't be created automatically.

So here is how one commonly would write it:

private struct Strings {
public string s1;
public string s2;

public Strings() {
s1 = String.Empty;
s2 = String.Empty;
}
}

Strings myStrings = new Strings();

Feel free to ask more.

HTH. -LV


Ludovico, thanks for your reply.

Per the MSDN [link]:

"Unlike a class, a struct is not permitted to declare a parameterlessinstance constructor. Instead, every struct implicitly has aparameterless instance constructor that always returns the value thatresults from setting all value type fields to their default value andall reference type fields to null. A struct can declare instance constructors having parameters."

So based on that, you cannot create a parameterless constructor for any struct (public Strings() { ...} does not compile - try it).

The statement above says all value type fields are set to their default value, and all reference type fields are set to null. Based on the fact that the implicit constructor does not initialize the string variables in my struct, I can safely assume that these string variables are reference types, correct? So, what I must do is to create a constructor with parameters and use that to instantiate and initialize the struct member variables.

Thanks for your help.

anonim:

> Unlike a class, a struct is not permitted to declare a parameterless instance constructor.

Yes, you are right, sorry...

> The statement above says all value type fields are set to their default value, and all reference type fields are set to null. Based on the fact that the implicit constructor does not initialize the string variables in my struct, I can safely assume that these string variables are reference types, correct?

Yes: String is a class, so strings are reference types, not value types.

> So, what I must do is to create a constructor with parameters and use that to instantiate and initialize the struct member variables.

Right again!

Cheers. :) -LV


Thank you much!